Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'SocalNick/feature/json-strategy-support…
Browse files Browse the repository at this point in the history
…-jsonp'
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Strategy/JsonStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public function selectRenderer(ViewEvent $e)
// application/json Accept header found
return $this->renderer;
}
if (0 === strpos($mediaType, 'application/javascript')) {
// application/javascript Accept header found
if (false != ($callback = $request->query()->get('callback'))) {
$this->renderer->setJsonpCallback($callback);
}
return $this->renderer;
}
}
}

Expand Down Expand Up @@ -148,6 +155,10 @@ public function injectResponse(ViewEvent $e)
$response = $e->getResponse();
$response->setContent($result);
$headers = $response->headers();
$headers->addHeaderLine('content-type', 'application/json');
if ($this->renderer->hasJsonpCallback()) {
$headers->addHeaderLine('content-type', 'application/javascript');
} else {
$headers->addHeaderLine('content-type', 'application/json');
}
}
}
40 changes: 39 additions & 1 deletion test/Strategy/JsonStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
Zend\View\Model\ViewModel,
Zend\View\Renderer\JsonRenderer,
Zend\View\Strategy\JsonStrategy,
Zend\View\ViewEvent;
Zend\View\ViewEvent,
Zend\Stdlib\Parameters;

/**
* @category Zend
Expand Down Expand Up @@ -65,6 +66,27 @@ public function testJsonAcceptHeaderSelectsJsonStrategy()
$this->assertSame($this->renderer, $result);
}

public function testJavascriptAcceptHeaderSelectsJsonStrategy()
{
$request = new HttpRequest();
$request->headers()->addHeaderLine('Accept', 'application/javascript');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertFalse($result->hasJsonpCallback());
}

public function testJavascriptAcceptHeaderSelectsJsonStrategyAndSetsJsonpCallback()
{
$request = new HttpRequest();
$request->headers()->addHeaderLine('Accept', 'application/javascript');
$request->setQuery(new Parameters(array('callback' => 'foo')));
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertTrue($result->hasJsonpCallback());
}

public function testLackOfJsonModelOrAcceptHeaderDoesNotSelectJsonStrategy()
{
$result = $this->strategy->selectRenderer($this->event);
Expand Down Expand Up @@ -120,6 +142,22 @@ public function testMatchingRendererAndStringResultInjectsResponse()
$this->assertEquals('application/json', $headers->get('content-type')->getFieldValue());
}

public function testMatchingRendererAndStringResultInjectsResponseJsonp()
{
$expected = json_encode(array('foo' => 'bar'));
$this->renderer->setJsonpCallback('foo');
$this->event->setResponse($this->response);
$this->event->setRenderer($this->renderer);
$this->event->setResult($expected);

$this->strategy->injectResponse($this->event);
$content = $this->response->getContent();
$headers = $this->response->headers();
$this->assertEquals($expected, $content);
$this->assertTrue($headers->has('content-type'));
$this->assertEquals('application/javascript', $headers->get('content-type')->getFieldValue());
}

public function testReturnsNullWhenCannotSelectRenderer()
{
$model = new ViewModel();
Expand Down

0 comments on commit 1786961

Please sign in to comment.