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

Commit

Permalink
Merge a96bd88 into e8c413f
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Apr 4, 2018
2 parents e8c413f + a96bd88 commit de6288a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Middleware/PathMiddlewareDecorator.php
Expand Up @@ -70,7 +70,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// layer.
return $this->middleware->process(
$requestToProcess,
new PathRequestHandlerDecorator($handler, $request)
new PathRequestHandlerDecorator($handler, $this->prefix)
);
}

Expand Down
15 changes: 9 additions & 6 deletions src/Middleware/PathRequestHandlerDecorator.php
Expand Up @@ -33,14 +33,17 @@ class PathRequestHandlerDecorator implements RequestHandlerInterface
private $handler;

/**
* @var ServerRequestInterface
* @var string
*/
private $originalRequest;
private $prefix;

public function __construct(RequestHandlerInterface $handler, ServerRequestInterface $originalRequest)
/**
* @param string $prefix
*/
public function __construct(RequestHandlerInterface $handler, $prefix)
{
$this->handler = $handler;
$this->originalRequest = $originalRequest;
$this->prefix = $prefix;
}

/**
Expand All @@ -49,8 +52,8 @@ public function __construct(RequestHandlerInterface $handler, ServerRequestInter
*/
public function handle(ServerRequestInterface $request)
{
$uri = $request->getUri()
->withPath($this->originalRequest->getUri()->getPath());
$uri = $request->getUri();
$uri = $uri->withPath($this->prefix . $uri->getPath());
return $this->handler->{HANDLER_METHOD}($request->withUri($uri));
}

Expand Down
27 changes: 27 additions & 0 deletions test/Middleware/PathMiddlewareDecoratorTest.php
Expand Up @@ -22,6 +22,8 @@

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

use function Zend\Stratigility\middleware;

class PathMiddlewareDecoratorTest extends TestCase
{
public function setUp()
Expand Down Expand Up @@ -367,4 +369,29 @@ public function testInvocationOfHandlerByDecoratedMiddlewareWillInvokeWithOrigin
$decoratedMiddleware->process($request, $finalHandler->reveal())
);
}

public function testUpdatesInPathInsideNestedMiddlewareAreRespected()
{
$request = new ServerRequest([], [], 'http://local.example.com/foo/bar', 'GET', 'php://memory');
$response = new Response();

$decoratedMiddleware = middleware(function (
ServerRequestInterface $request,
RequestHandlerInterface $handler
) {
return $handler->handle($request->withUri(new Uri('/changed/path')));
});
$middleware = new PathMiddlewareDecorator('/foo', $decoratedMiddleware);

$handler = $this->prophesize(RequestHandlerInterface::class);
$handler
->handle(Argument::that(function (ServerRequestInterface $received) {
Assert::assertEquals('/foo/changed/path', $received->getUri()->getPath());

return $received;
}))
->willReturn($response);

$this->assertSame($response, $middleware->process($request, $handler->reveal()));
}
}
14 changes: 8 additions & 6 deletions test/NextTest.php
Expand Up @@ -25,6 +25,10 @@
use Zend\Stratigility\Next;
use Zend\Stratigility\Route;

use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD;

use function Zend\Stratigility\middleware;

class NextTest extends TestCase
{
protected $errorHandler;
Expand Down Expand Up @@ -89,18 +93,16 @@ public function testMiddlewareCallingNextWithRequestPassesRequestToNextMiddlewar
$cannedRequest = clone $request;
$cannedRequest = $cannedRequest->withMethod('POST');

$route1 = new Route('/foo/bar', $this->decorateCallableMiddleware(
$route1 = new Route('/', $this->decorateCallableMiddleware(
function ($req, $res, $next) use ($cannedRequest) {
return $next($cannedRequest, $res);
},
'/foo/bar'
}
));
$route2 = new Route('/foo/bar/baz', $this->decorateCallableMiddleware(
$route2 = new Route('/', $this->decorateCallableMiddleware(
function ($req, $res, $next) use ($cannedRequest) {
$this->assertEquals($cannedRequest->getMethod(), $req->getMethod());
return $res;
},
'/foo/bar/baz'
}
));

$this->queue->enqueue($route1);
Expand Down

0 comments on commit de6288a

Please sign in to comment.