Skip to content

Commit

Permalink
Fixed bug where collections were prematurely matched resulting in 404…
Browse files Browse the repository at this point in the history
…s and fixes for point release API versions. Closes #16.

Signed-off-by: Jason Lewis <[email protected]>
  • Loading branch information
jasonlewis committed Apr 28, 2014
1 parent 28c9225 commit 12054a2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
37 changes: 29 additions & 8 deletions src/Routing/ApiRouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,38 @@ public function option($key, $default = null)
*/
public function matchesRequest($request)
{
if ($this->matchDomain($request))
if ($this->matchesCollectionVersion($request))
{
return true;
if ($this->matchDomain($request))
{
return true;
}
elseif ($this->matchPrefix($request))
{
return true;
}
elseif ( ! $this->option('prefix') and ! $this->option('domain'))
{
return true;
}
}
elseif ($this->matchPrefix($request))
{
return true;
}
elseif ( ! $this->option('prefix') and ! $this->option('domain'))

return false;
}

/**
* Determine if the requested version matches the collection version.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function matchesCollectionVersion($request)
{
if (preg_match('#application/vnd\.\w+.(v[\d\.]+)\+\w+#', $request->header('accept'), $matches))
{
return true;
list ($accept, $version) = $matches;

return $version == $this->version;
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public function requestTargettingApi($request = null)
*/
protected function parseAcceptHeader($request)
{
if (preg_match('#application/vnd\.'.$this->vendor.'.(v\d)\+(json)#', $request->header('accept'), $matches))
if (preg_match('#application/vnd\.'.$this->vendor.'.(v[\d\.]+)\+(\w+)#', $request->header('accept'), $matches))
{
list ($accept, $this->requestedVersion, $this->requestedFormat) = $matches;
}
Expand Down
53 changes: 51 additions & 2 deletions tests/RoutingRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public function testRouterDispatchesInternalRequests()
$this->router->get('foo', function() { return 'bar'; });
});

$this->assertEquals('{"message":"bar"}', $this->router->dispatch(InternalRequest::create('foo', 'GET'))->getContent());
$request = InternalRequest::create('foo', 'GET');
$request->headers->set('accept', 'application/vnd.testing.v1+json');

$this->assertEquals('{"message":"bar"}', $this->router->dispatch($request)->getContent());
}


Expand Down Expand Up @@ -253,7 +256,10 @@ public function testRouterCatchesHttpExceptionsAndCreatesResponse()
$this->router->get('foo', function() use ($exception) { throw $exception; });
});

$response = $this->router->dispatch(Request::create('foo', 'GET'));
$request = Request::create('foo', 'GET');
$request->headers->set('accept', 'application/vnd.testing.v1+json');

$response = $this->router->dispatch($request);

$this->assertEquals(404, $response->getStatusCode());
$this->assertEquals('{"message":"404 Not Found"}', $response->getContent());
Expand Down Expand Up @@ -355,4 +361,47 @@ public function testRequestTargettingAnApiWithNoPrefixOrDomain()
}


public function testRequestWithMultipleApisFindsTheCorrectApiRouteCollection()
{
$this->router->api(['version' => 'v1', 'prefix' => 'api'], function()
{
$this->router->get('foo', function() { return 'bar'; });
});

$this->router->api(['version' => 'v2', 'prefix' => 'api'], function()
{
$this->router->get('bar', function() { return 'baz'; });
});

$request = Request::create('api/bar', 'GET');
$request->headers->set('accept', 'application/vnd.testing.v2+json');

$this->assertEquals('{"message":"baz"}', $this->router->dispatch($request)->getContent());
}


public function testApiCollectionsWithPointReleaseVersions()
{
$this->router->api(['version' => 'v1.1', 'prefix' => 'api'], function()
{
$this->router->get('foo', function() { return 'bar'; });
});

$this->router->api(['version' => 'v2.0.1', 'prefix' => 'api'], function()
{
$this->router->get('bar', function() { return 'baz'; });
});

$request = Request::create('api/foo', 'GET');
$request->headers->set('accept', 'application/vnd.testing.v1.1+json');

$this->assertEquals('{"message":"bar"}', $this->router->dispatch($request)->getContent());

$request = Request::create('api/bar', 'GET');
$request->headers->set('accept', 'application/vnd.testing.v2.0.1+json');

$this->assertEquals('{"message":"baz"}', $this->router->dispatch($request)->getContent());
}


}

0 comments on commit 12054a2

Please sign in to comment.