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

Commit

Permalink
Merge branch 'hotfix/28'
Browse files Browse the repository at this point in the history
Close #29
Fixes #28
  • Loading branch information
weierophinney committed Dec 14, 2016
2 parents 7a0af81 + b6f5e63 commit 50c9e4d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 47 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.3.2 - TBD
## 1.3.2 - 2016-12-14

### Added

Expand All @@ -18,7 +18,12 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#29](https://github.com/zendframework/zend-expressive-router/pull/29) removes
the patch introduced with [#27](https://github.com/zendframework/zend-expressive-router/pull/27)
and 1.3.1, as it causes `Zend\Expressive\Application` to raise exceptions
regarding duplicate routes, and because some implementations, including
FastRoute, also raise errors on duplication. It will be up to individual
routers to determine how to handle implicit HEAD and OPTIONS support.

## 1.3.1 - 2016-12-13

Expand Down
49 changes: 11 additions & 38 deletions src/Route.php
Expand Up @@ -33,13 +33,13 @@ class Route
* @var bool If HEAD was not provided to the Route instance, indicate
* support for the method is implicit.
*/
private $implicitHead = false;
private $implicitHead = true;

/**
* @var bool If OPTIONS was not provided to the Route instance, indicate
* support for the method is implicit.
*/
private $implicitOptions = false;
private $implicitOptions = true;

/**
* @var int|string[] HTTP methods allowed with this route.
Expand Down Expand Up @@ -99,9 +99,14 @@ public function __construct($path, $middleware, $methods = self::HTTP_METHOD_ANY
if (empty($name)) {
$name = ($this->methods === self::HTTP_METHOD_ANY)
? $path
: $path . '^' . implode(self::HTTP_METHOD_SEPARATOR, $this->retrieveExplicitMethods($this->methods));
: $path . '^' . implode(self::HTTP_METHOD_SEPARATOR, $this->methods);
}
$this->name = $name;

$this->implicitHead = is_array($this->methods)
&& ! in_array(RequestMethod::METHOD_HEAD, $this->methods, true);
$this->implicitOptions = is_array($this->methods)
&& ! in_array(RequestMethod::METHOD_OPTIONS, $this->methods, true);
}

/**
Expand Down Expand Up @@ -155,7 +160,9 @@ public function getAllowedMethods()
public function allowsMethod($method)
{
$method = strtoupper($method);
if ($this->methods === self::HTTP_METHOD_ANY
if (RequestMethod::METHOD_HEAD === $method
|| RequestMethod::METHOD_OPTIONS === $method
|| $this->methods === self::HTTP_METHOD_ANY
|| in_array($method, $this->methods, true)
) {
return true;
Expand Down Expand Up @@ -229,40 +236,6 @@ private function validateHttpMethods(array $methods)
throw new Exception\InvalidArgumentException('One or more HTTP methods were invalid');
}

if (! in_array(RequestMethod::METHOD_HEAD, $methods, true)) {
$this->implicitHead = true;
$methods[] = RequestMethod::METHOD_HEAD;
}

if (! in_array(RequestMethod::METHOD_OPTIONS, $methods, true)) {
$this->implicitOptions = true;
$methods[] = RequestMethod::METHOD_OPTIONS;
}

return array_map('strtoupper', $methods);
}

/**
* Return a list of methods explicitly set when creating the route.
*
* Filters out HEAD and/or OPTIONS if they were set implicitly.
*/
private function retrieveExplicitMethods(array $methods)
{
return array_filter($methods, function ($method) {
if (! in_array($method, [RequestMethod::METHOD_HEAD, RequestMethod::METHOD_OPTIONS], true)) {
return true;
}

if ($method === RequestMethod::METHOD_HEAD && ! $this->implicitHead) {
return true;
}

if ($method === RequestMethod::METHOD_OPTIONS && ! $this->implicitOptions) {
return true;
}

return false;
});
}
}
8 changes: 1 addition & 7 deletions test/RouteTest.php
Expand Up @@ -56,9 +56,7 @@ public function testRouteAllowsSpecifyingHttpMethods()
{
$methods = [RequestMethod::METHOD_GET, RequestMethod::METHOD_POST];
$route = new Route('/foo', $this->noopMiddleware, $methods);
foreach ($methods as $method) {
$this->assertContains($method, $route->getAllowedMethods());
}
$this->assertSame($methods, $route->getAllowedMethods($methods));
}

public function testRouteCanMatchMethod()
Expand Down Expand Up @@ -183,8 +181,6 @@ public function testProvidingArrayOfMethodsWithoutHeadOrOptionsImpliesBoth()
$route = new Route('/test', $this->noopMiddleware, [RequestMethod::METHOD_GET, RequestMethod::METHOD_POST]);
$this->assertTrue($route->implicitHead());
$this->assertTrue($route->implicitOptions());
$this->assertContains(RequestMethod::METHOD_HEAD, $route->getAllowedMethods());
$this->assertContains(RequestMethod::METHOD_OPTIONS, $route->getAllowedMethods());
}

public function headAndOptions()
Expand All @@ -202,8 +198,6 @@ public function testPassingHeadOrOptionsInMethodArrayDoesNotMarkAsImplicit($http
{
$route = new Route('/test', $this->noopMiddleware, [$httpMethod]);
$this->assertFalse($route->{$implicitMethod}());
$this->assertContains(RequestMethod::METHOD_HEAD, $route->getAllowedMethods());
$this->assertContains(RequestMethod::METHOD_OPTIONS, $route->getAllowedMethods());
}

public function testPassingWildcardMethodDoesNotMarkAsImplicit()
Expand Down

0 comments on commit 50c9e4d

Please sign in to comment.