diff --git a/src/HttpMethodOverrideListener.php b/src/HttpMethodOverrideListener.php index df6ab1c..8e0887d 100644 --- a/src/HttpMethodOverrideListener.php +++ b/src/HttpMethodOverrideListener.php @@ -48,6 +48,7 @@ public function override(MvcEvent $event) if ($overridedMethod !== $method) { $request->setMethod($overridedMethod); + $event->setParam('overrided-method', $method); } } } diff --git a/src/HttpMethodOverrideMiddleware.php b/src/HttpMethodOverrideMiddleware.php index ee8b651..3c02fd8 100644 --- a/src/HttpMethodOverrideMiddleware.php +++ b/src/HttpMethodOverrideMiddleware.php @@ -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); } diff --git a/test/HttpMethodOverrideListenerTest.php b/test/HttpMethodOverrideListenerTest.php index a2ba235..13008e8 100644 --- a/test/HttpMethodOverrideListenerTest.php +++ b/test/HttpMethodOverrideListenerTest.php @@ -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(); diff --git a/test/HttpMethodOverrideMiddlewareTest.php b/test/HttpMethodOverrideMiddlewareTest.php index 5452f54..13842a7 100644 --- a/test/HttpMethodOverrideMiddlewareTest.php +++ b/test/HttpMethodOverrideMiddlewareTest.php @@ -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();