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

Commit

Permalink
Merge branch 'hotfix/2615'
Browse files Browse the repository at this point in the history
Introduce AcceptableViewModelSelector, and remove Accept matching from
JsonStrategy and FeedStrategy

Close zendframework/zendframework#2615
Close zendframework/zendframework#2410
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 98 deletions.
30 changes: 5 additions & 25 deletions src/Strategy/FeedStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,13 @@ public function selectRenderer(ViewEvent $e)
{
$model = $e->getModel();

$request = $e->getRequest();
if (!$request instanceof HttpRequest) {
// Not an HTTP request; cannot autodetermine
return ($model instanceof Model\FeedModel) ? $this->renderer : null;
}

$headers = $request->getHeaders();
if (!$headers->has('accept')) {
return ($model instanceof Model\FeedModel) ? $this->renderer : null;
}

$accept = $headers->get('accept');
if (($match = $accept->match('application/rss+xml, application/atom+xml')) == false) {
return ($model instanceof Model\FeedModel) ? $this->renderer : null;
}

if ($match->getTypeString() == 'application/rss+xml') {
$this->renderer->setFeedType('rss');
return $this->renderer;
}

if ($match->getTypeString() == 'application/atom+xml') {
$this->renderer->setFeedType('atom');
return $this->renderer;
if (!$model instanceof Model\FeedModel) {
// no FeedModel present; do nothing
return;
}

return ($model instanceof Model\FeedModel) ? $this->renderer : null;
// FeedModel found
return $this->renderer;
}

/**
Expand Down
33 changes: 5 additions & 28 deletions src/Strategy/JsonStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,36 +83,13 @@ public function selectRenderer(ViewEvent $e)
{
$model = $e->getModel();

$request = $e->getRequest();
if (!$request instanceof HttpRequest) {
// Not an HTTP request; cannot autodetermine
return ($model instanceof Model\JsonModel) ? $this->renderer : null;
}

$headers = $request->getHeaders();
if (!$headers->has('accept')) {
return ($model instanceof Model\JsonModel) ? $this->renderer : null;
}

$accept = $headers->get('Accept');
if (($match = $accept->match('application/json, application/javascript')) == false) {
return ($model instanceof Model\JsonModel) ? $this->renderer : null;
}

if ($match->getTypeString() == 'application/json') {
// application/json Accept header found
return $this->renderer;
}

if ($match->getTypeString() == 'application/javascript') {
// application/javascript Accept header found
if (false != ($callback = $request->getQuery()->get('callback'))) {
$this->renderer->setJsonpCallback($callback);
}
return $this->renderer;
if (!$model instanceof Model\JsonModel) {
// no JsonModel; do nothing
return;
}

return ($model instanceof Model\JsonModel) ? $this->renderer : null;
// JsonModel found
return $this->renderer;
}

/**
Expand Down
28 changes: 12 additions & 16 deletions test/Strategy/FeedStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,39 @@ public function testFeedModelSelectsFeedStrategy()
$this->assertSame($this->renderer, $result);
}

public function testRssAcceptHeaderSelectsFeedStrategy()
/**
* @group #2410
*/
public function testRssAcceptHeaderDoesNotSelectFeedStrategy()
{
$request = new HttpRequest();
$request->getHeaders()->addHeaderLine('Accept', 'application/rss+xml');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertNotSame($this->renderer, $result);
}

public function testAtomAcceptHeaderSelectsFeedStrategy()
/**
* @group #2410
*/
public function testAtomAcceptHeaderDoesNotSelectFeedStrategy()
{
$request = new HttpRequest();
$request->getHeaders()->addHeaderLine('Accept', 'application/atom+xml');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
}

public function testViewModelMatchedAcceptHeaderMatchSelectsFeedStrategy()
{
$this->event->setModel(new FeedModel());
$request = new HttpRequest();
$request->getHeaders()->addHeaderLine('Accept', '*/*');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertNotSame($this->renderer, $result);
}

public function testViewModelAcceptHeaderSelectsFeedStrategyAndSetsFeedtype()
public function testAcceptHeaderDoesNotSetFeedtype()
{
$this->event->setModel(new FeedModel());
$request = new HttpRequest();
$request->getHeaders()->addHeaderLine('Accept', 'application/atom+xml');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertSame('atom', $result->getFeedType());
$this->assertNotSame('atom', $result->getFeedType());
}

public function testLackOfFeedModelOrAcceptHeaderDoesNotSelectFeedStrategy()
Expand Down
45 changes: 16 additions & 29 deletions test/Strategy/JsonStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,34 @@ public function testJsonModelSelectsJsonStrategy()
$this->assertSame($this->renderer, $result);
}

public function testJsonAcceptHeaderSelectsJsonStrategy()
/**
* @group #2410
*/
public function testJsonAcceptHeaderDoesNotSelectJsonStrategy()
{
$request = new HttpRequest();
$request->getHeaders()->addHeaderLine('Accept', 'application/json');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertNotSame($this->renderer, $result);
}

public function testJavascriptAcceptHeaderSelectsJsonStrategy()
/**
* @group #2410
*/
public function testJavascriptAcceptHeaderDoesNotSelectJsonStrategy()
{
$request = new HttpRequest();
$request->getHeaders()->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 testJsonModelMatchedAcceptHeaderMatchSelectsJsonStrategy()
{
$this->event->setModel(new JsonModel());
$request = new HttpRequest();
$request->getHeaders()->addHeaderLine('Accept', '*/*');
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertNotSame($this->renderer, $result);
}

public function testJsonModelJavascriptAcceptHeaderSetsJsonpCallback()
/**
* @group #2410
*/
public function testJsonModelJavascriptAcceptHeaderDoesNotSetJsonpCallback()
{
$this->event->setModel(new JsonModel());
$request = new HttpRequest();
Expand All @@ -82,21 +80,10 @@ public function testJsonModelJavascriptAcceptHeaderSetsJsonpCallback()
$this->event->setRequest($request);
$result = $this->strategy->selectRenderer($this->event);
$this->assertSame($this->renderer, $result);
$this->assertTrue($result->hasJsonpCallback());
}

public function testJavascriptAcceptHeaderSelectsJsonStrategyAndSetsJsonpCallback()
{
$request = new HttpRequest();
$request->getHeaders()->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());
$this->assertFalse($result->hasJsonpCallback());
}

public function testLackOfJsonModelOrAcceptHeaderDoesNotSelectJsonStrategy()
public function testLackOfJsonModelDoesNotSelectJsonStrategy()
{
$result = $this->strategy->selectRenderer($this->event);
$this->assertNotSame($this->renderer, $result);
Expand Down

0 comments on commit 0ebadc5

Please sign in to comment.