Skip to content

Commit

Permalink
Merge pull request #5 from rstgroup/remember-overrided-method
Browse files Browse the repository at this point in the history
Remember overrided method
  • Loading branch information
snapshotpl authored May 24, 2017
2 parents e28ed6c + dedeca8 commit e82bb0b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/HttpMethodOverrideListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function override(MvcEvent $event)

if ($overridedMethod !== $method) {
$request->setMethod($overridedMethod);
$event->setParam('overrided-method', $method);
}
}
}
5 changes: 3 additions & 2 deletions src/HttpMethodOverrideMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele
$overridedHeader = $this->service->getOverridedMethod($method, $headers);

if ($method !== $overridedHeader) {
$request = $request->withMethod($overridedHeader);
$request = $request
->withMethod($overridedHeader)
->withAttribute('overrided-method', $method);
}

return $delegate->process($request);
}

Expand Down
15 changes: 15 additions & 0 deletions test/HttpMethodOverrideListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ public function testOverride()
$this->assertSame('PUT', $request->getMethod());
}

public function testEventContainsOverridedMethod()
{
$request = new Request();
$request->setMethod('POST');

$request->getHeaders()->addHeaderLine(HttpMethodOverrideService::OVERRIDE_HEADER, 'PUT');

$event = new MvcEvent();
$event->setRequest($request);

$this->listener->override($event);

$this->assertSame('POST', $event->getParam('overrided-method'));
}

public function testNotOverride()
{
$request = new Request();
Expand Down
15 changes: 15 additions & 0 deletions test/HttpMethodOverrideMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ public function testOverride()
call_user_func($this->middleware, $request, $response, $next);
}

public function testRequestHasOverridedMethod()
{
$request = new ServerRequest();
$request = $request->withHeader(HttpMethodOverrideService::OVERRIDE_HEADER, 'PUT');
$request = $request->withMethod('POST');

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

$next = function(ServerRequestInterface $request) {
$this->assertSame('POST', $request->getAttribute('overrided-method'));
};

call_user_func($this->middleware, $request, $response, $next);
}

public function testNotOverride()
{
$request = new ServerRequest();
Expand Down

0 comments on commit e82bb0b

Please sign in to comment.