Skip to content

Commit b08b1d9

Browse files
committed
Properly handle requests with a trailing slash in Lumen and add test coverage for all adapters.
Signed-off-by: Jason Lewis <[email protected]>
1 parent cda184a commit b08b1d9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Routing/Adapter/Lumen.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,27 @@ public function dispatch(Request $request, $version)
9090
new $this->dispatcher($routes->getData())
9191
);
9292

93+
$this->normalizeRequestUri($request);
94+
9395
return $this->app->dispatch($request);
9496
}
9597

98+
/**
99+
* Normalize the request URI so that Lumen can properly dispatch it.
100+
*
101+
* @param \Illuminate\Http\Request $request
102+
*
103+
* @return void
104+
*/
105+
protected function normalizeRequestUri(Request $request)
106+
{
107+
$query = $request->server->get('QUERY_STRING');
108+
109+
$uri = '/'.trim(str_replace('?'.$query, '', $request->server->get('REQUEST_URI')), '/').'?'.$query;
110+
111+
$request->server->set('REQUEST_URI', $uri);
112+
}
113+
96114
/**
97115
* Get the URI, methods, and action from the route.
98116
*

tests/Routing/Adapter/BaseAdapterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public function testBasicRouteVersions()
8181
$request = $this->createRequest('/foo', 'GET', ['accept' => 'application/vnd.api.v1+json']);
8282
$this->assertEquals('foo', $this->router->dispatch($request)->getContent());
8383

84+
$request = $this->createRequest('/foo/', 'GET', ['accept' => 'application/vnd.api.v1+json']);
85+
$this->assertEquals('foo', $this->router->dispatch($request)->getContent(), 'Could not dispatch request with trailing slash.');
86+
8487
$request = $this->createRequest('/foo', 'GET', ['accept' => 'application/vnd.api.v2+json']);
8588
$this->assertEquals('bar', $this->router->dispatch($request)->getContent());
8689

0 commit comments

Comments
 (0)