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/73' into develop
Browse files Browse the repository at this point in the history
Forward port #73

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Mar 21, 2018
2 parents 299b0cd + ee8fcbd commit ea07fa2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 3.0.2 - TBD
## 3.0.2 - 2018-03-21

### Added

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

### Fixed

- Nothing.
- [#73](https://github.com/zendframework/zend-expressive-router/pull/73) fixes
an issue with the `ImplicitOptionsMiddleware` whereby a path match failure was
incorrectly being identified as a method match failure, triggering the
`ImplicitOptionsMiddleware` to attempt to return a response.

## 3.0.1 - 2018-03-19

Expand Down
4 changes: 4 additions & 0 deletions src/Middleware/ImplicitOptionsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $handler->handle($request);
}

if ($result->isFailure() && ! $result->isMethodFailure()) {
return $handler->handle($request);
}

if ($result->getMatchedRoute()) {
return $handler->handle($request);
}
Expand Down
47 changes: 47 additions & 0 deletions src/Test/ImplicitMethodsIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,51 @@ public function testImplicitOptionsRequest(

$this->assertSame($finalResponse->reveal(), $response);
}

public function testImplicitOptionsRequestRouteNotFound()
{
$router = $this->getRouter();

$pipeline = new MiddlewarePipe();
$pipeline->pipe(new RouteMiddleware($router));
$pipeline->pipe($this->getImplicitOptionsMiddleware());
$pipeline->pipe(new MethodNotAllowedMiddleware($this->createInvalidResponseFactory()));
$pipeline->pipe(new DispatchMiddleware());

$finalResponse = (new Response())
->withStatus(StatusCode::STATUS_IM_A_TEAPOT)
->withHeader('foo-bar', 'baz');
$finalResponse->getBody()->write('FOO BAR BODY');

$request = new ServerRequest(
['REQUEST_METHOD' => RequestMethod::METHOD_OPTIONS],
[],
'/not-found',
RequestMethod::METHOD_OPTIONS
);

$finalHandler = $this->prophesize(RequestHandlerInterface::class);
$finalHandler
->handle(Argument::that(function (ServerRequestInterface $request) {
Assert::assertSame(RequestMethod::METHOD_OPTIONS, $request->getMethod());

$routeResult = $request->getAttribute(RouteResult::class);
Assert::assertInstanceOf(RouteResult::class, $routeResult);
Assert::assertTrue($routeResult->isFailure());
Assert::assertFalse($routeResult->isSuccess());
Assert::assertFalse($routeResult->isMethodFailure());
Assert::assertFalse($routeResult->getMatchedRoute());

return true;
}))
->willReturn($finalResponse)
->shouldBeCalledTimes(1);

$response = $pipeline->process($request, $finalHandler->reveal());

$this->assertEquals(StatusCode::STATUS_IM_A_TEAPOT, $response->getStatusCode());
$this->assertSame('FOO BAR BODY', (string) $response->getBody());
$this->assertTrue($response->hasHeader('foo-bar'));
$this->assertSame('baz', $response->getHeaderLine('foo-bar'));
}
}
28 changes: 21 additions & 7 deletions test/Middleware/ImplicitOptionsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ public function testReturnsResultOfHandlerWhenRouteSupportsOptionsExplicitly()
{
$route = $this->prophesize(Route::class);

$result = $this->prophesize(RouteResult::class);
$result->getMatchedRoute()->will([$route, 'reveal']);
$result = RouteResult::fromRoute($route->reveal());

$request = $this->prophesize(ServerRequestInterface::class);
$request->getMethod()->willReturn(RequestMethod::METHOD_OPTIONS);
$request->getAttribute(RouteResult::class)->will([$result, 'reveal']);
$request->getAttribute(RouteResult::class)->willReturn($result);

$response = $this->prophesize(ResponseInterface::class)->reveal();

Expand All @@ -93,13 +92,11 @@ public function testInjectsAllowHeaderInResponseProvidedToConstructorDuringOptio
{
$allowedMethods = [RequestMethod::METHOD_GET, RequestMethod::METHOD_POST];

$result = $this->prophesize(RouteResult::class);
$result->getAllowedMethods()->willReturn($allowedMethods);
$result->getMatchedRoute()->willReturn(false);
$result = RouteResult::fromRouteFailure($allowedMethods);

$request = $this->prophesize(ServerRequestInterface::class);
$request->getMethod()->willReturn(RequestMethod::METHOD_OPTIONS);
$request->getAttribute(RouteResult::class)->will([$result, 'reveal']);
$request->getAttribute(RouteResult::class)->willReturn($result);

$handler = $this->prophesize(RequestHandlerInterface::class);
$handler->handle($request->reveal())->shouldNotBeCalled();
Expand All @@ -111,4 +108,21 @@ public function testInjectsAllowHeaderInResponseProvidedToConstructorDuringOptio
$result = $this->middleware->process($request->reveal(), $handler->reveal());
$this->assertSame($this->response->reveal(), $result);
}

public function testReturnsResultOfHandlerWhenRouteNotFound()
{
$result = RouteResult::fromRouteFailure(Route::HTTP_METHOD_ANY);

$request = $this->prophesize(ServerRequestInterface::class);
$request->getMethod()->willReturn(RequestMethod::METHOD_OPTIONS);
$request->getAttribute(RouteResult::class)->willReturn($result);

$response = $this->prophesize(ResponseInterface::class)->reveal();

$handler = $this->prophesize(RequestHandlerInterface::class);
$handler->handle($request->reveal())->willReturn($response);

$result = $this->middleware->process($request->reveal(), $handler->reveal());
$this->assertSame($response, $result);
}
}

0 comments on commit ea07fa2

Please sign in to comment.