mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-22 08:56:42 -06:00
Updated PathVersionMiddleware so that it is only applied to rest routes
This commit is contained in:
parent
869865f22a
commit
de9d9d8667
@ -10,6 +10,7 @@ services:
|
|||||||
- "8000:80"
|
- "8000:80"
|
||||||
volumes:
|
volumes:
|
||||||
- ./:/home/shlink/www
|
- ./:/home/shlink/www
|
||||||
|
- ./docs:/home/shlink/www/public/docs
|
||||||
- ./data/infra/vhost.conf:/etc/nginx/conf.d/shlink-vhost.conf
|
- ./data/infra/vhost.conf:/etc/nginx/conf.d/shlink-vhost.conf
|
||||||
links:
|
links:
|
||||||
- shlink_php
|
- shlink_php
|
||||||
|
@ -5,6 +5,7 @@ return [
|
|||||||
|
|
||||||
'middleware_pipeline' => [
|
'middleware_pipeline' => [
|
||||||
'pre-routing' => [
|
'pre-routing' => [
|
||||||
|
'path' => '/rest',
|
||||||
'middleware' => [
|
'middleware' => [
|
||||||
Middleware\PathVersionMiddleware::class,
|
Middleware\PathVersionMiddleware::class,
|
||||||
],
|
],
|
||||||
|
@ -37,19 +37,13 @@ class PathVersionMiddleware implements MiddlewareInterface
|
|||||||
$uri = $request->getUri();
|
$uri = $request->getUri();
|
||||||
$path = $uri->getPath();
|
$path = $uri->getPath();
|
||||||
|
|
||||||
// Exclude non-rest route
|
|
||||||
if (strpos($path, '/rest') !== 0) {
|
|
||||||
return $out($request, $response);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the path does not begin with the version number, prepend v1 by default for retrocompatibility purposes
|
// If the path does not begin with the version number, prepend v1 by default for retrocompatibility purposes
|
||||||
if (strpos($path, '/rest/v') !== 0) {
|
if (strpos($path, '/v') !== 0) {
|
||||||
$parts = explode('/', $path);
|
$parts = explode('/', $path);
|
||||||
// Remove the first empty part and the "/rest" prefix
|
// Remove the first empty part and the
|
||||||
array_shift($parts);
|
array_shift($parts);
|
||||||
array_shift($parts);
|
// Prepend the version prefix
|
||||||
// Prepend the prefix with version
|
array_unshift($parts, '/v1');
|
||||||
array_unshift($parts, '/rest/v1');
|
|
||||||
|
|
||||||
$request = $request->withUri($uri->withPath(implode('/', $parts)));
|
$request = $request->withUri($uri->withPath(implode('/', $parts)));
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,7 @@ class PathVersionMiddlewareTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function whenVersionIsProvidedRequestRemainsUnchanged()
|
public function whenVersionIsProvidedRequestRemainsUnchanged()
|
||||||
{
|
{
|
||||||
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo'));
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo'));
|
||||||
$test = $this;
|
$test = $this;
|
||||||
$this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) {
|
$this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) {
|
||||||
$test->assertSame($request, $req);
|
$test->assertSame($request, $req);
|
||||||
@ -37,23 +37,11 @@ class PathVersionMiddlewareTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function versionOneIsPrependedWhenNoVersionIsDefined()
|
public function versionOneIsPrependedWhenNoVersionIsDefined()
|
||||||
{
|
{
|
||||||
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz'));
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz'));
|
||||||
$test = $this;
|
$test = $this;
|
||||||
$this->middleware->__invoke($request, new Response(), function (Request $req) use ($request, $test) {
|
$this->middleware->__invoke($request, new Response(), function (Request $req) use ($request, $test) {
|
||||||
$test->assertNotSame($request, $req);
|
$test->assertNotSame($request, $req);
|
||||||
$this->assertEquals('/rest/v1/bar/baz', $req->getUri()->getPath());
|
$this->assertEquals('/v1/bar/baz', $req->getUri()->getPath());
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
*/
|
|
||||||
public function nonRestPathsAreNotProcessed()
|
|
||||||
{
|
|
||||||
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/non-rest'));
|
|
||||||
$test = $this;
|
|
||||||
$this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) {
|
|
||||||
$test->assertSame($request, $req);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user