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

Commit

Permalink
Fixes path truncation in PathMiddlewareDecorator to be case insensitive
Browse files Browse the repository at this point in the history
In #168, we received a report indicating that path matching within
`PathMiddlewareDecorator` fails if the path is the same, but using a
different case.

Interestingly, we _match_ it correctly; the problem is when _preparing
the truncated path_ to pass on to the decorated middleware, as it cannot
identify the matched segment correctly.

This patch modifies the logic in `getTruncatedPath()` to do a
case-insensitive comparison of the segment and the path in the initial
condition.
  • Loading branch information
weierophinney committed Apr 16, 2018
1 parent f946cdc commit 44f8885
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Middleware/PathMiddlewareDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function prepareRequestWithTruncatedPrefix(ServerRequestInterface $reque
*/
private function getTruncatedPath($segment, $path)
{
if ($segment === $path) {
if (strtolower($segment) === strtolower($path)) {
// Decorated path and current path are the same; return empty string
return '';
}
Expand Down
26 changes: 26 additions & 0 deletions test/Middleware/PathMiddlewareDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,30 @@ public function testUpdatesInPathInsideNestedMiddlewareAreRespected()

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

public function testProcessesMatchedPathsWithoutCaseSensitivity()
{
$finalHandler = $this->prophesize(RequestHandlerInterface::class);
$finalHandler->{HANDLER_METHOD}(Argument::any())->willReturn(new Response());

// Note that the path requested is ALL CAPS:
$request = new ServerRequest([], [], 'http://local.example.com/MYADMIN', 'GET', 'php://memory');

$middleware = $this->prophesize(MiddlewareInterface::class);
$middleware
->process(
Argument::that(function (ServerRequestInterface $req) {
Assert::assertSame('', $req->getUri()->getPath());

return true;
}),
Argument::any()
)
->willReturn(new Response())
->shouldBeCalledTimes(1);

// Note that the path to match is lowercase:
$decorator = new PathMiddlewareDecorator('/myadmin', $middleware->reveal());
$decorator->process($request, $finalHandler->reveal());
}
}

0 comments on commit 44f8885

Please sign in to comment.