Skip to content

Commit

Permalink
Merge pull request #799 from brefphp/remove-prefix-url
Browse files Browse the repository at this point in the history
Re-remove the API Gateway stage prefix from URLs
  • Loading branch information
mnapoli authored Nov 23, 2020
2 parents fdab7bd + f327120 commit 69f9a8a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
17 changes: 14 additions & 3 deletions src/Event/Http/HttpRequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,21 @@ public function getPath(): string
}

/**
* $event['requestContext']['path'] contains the real URL, including the stage prefix.
* $event['path'] contains the URL without the stage prefix.
* $event['path'] contains the URL always without the stage prefix.
* $event['requestContext']['path'] contains the URL always with the stage prefix.
* None of the represents the real URL because:
* - the native API Gateway URL has the stage (`/dev`)
* - with a custom domain, the URL doesn't have the stage (`/`)
* - with CloudFront in front of AG, the URL doesn't have the stage (`/`)
* Because it's hard to detect whether CloudFront is used, we will go with the "non-prefixed" URL ($event['path'])
* as it's the one most likely used in production (because in production we use custom domains).
* Since Bref now recommends HTTP APIs (that don't have a stage prefix), this problem will not be common anyway.
* Full history:
* - https://github.com/brefphp/bref/issues/67
* - https://github.com/brefphp/bref/issues/309
* - https://github.com/brefphp/bref/pull/794
*/
return $this->event['requestContext']['path'] ?? '/';
return $this->event['path'] ?? '/';
}

public function getUri(): string
Expand Down
6 changes: 3 additions & 3 deletions tests/Event/Http/CommonHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public function test simple request(int $version)
$this->assertHasMultiHeader(false);
}

public function test v1 stage prefix is included in the URL()
public function test v1 stage prefix is not included in the URL()
{
$this->fromFixture(__DIR__ . '/Fixture/ag-v1-stage-prefix.json');

$this->assertPath('/dev/path');
$this->assertUri('/dev/path');
$this->assertPath('/path');
$this->assertUri('/path');
}

/**
Expand Down
16 changes: 4 additions & 12 deletions tests/Handler/FpmHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function test simple request(int $version)
'httpMethod' => 'GET',
'path' => '/hello',
'requestContext' => [
'path' => '/hello',
'protocol' => 'HTTP/1.1',
],
];
Expand All @@ -69,7 +68,7 @@ public function test simple request(int $version)
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_INVOCATION_CONTEXT' => json_encode($this->fakeContext),
'LAMBDA_REQUEST_CONTEXT' => '{"path":"\/hello","protocol":"HTTP\/1.1"}',
'LAMBDA_REQUEST_CONTEXT' => '{"protocol":"HTTP\/1.1"}',
],
'HTTP_RAW_BODY' => '',
]);
Expand All @@ -84,9 +83,6 @@ public function test request with query string(int $version)
'version' => '1.0',
'httpMethod' => 'GET',
'path' => '/hello',
'requestContext' => [
'path' => '/hello',
],
'queryStringParameters' => [
'foo' => 'bar',
'bim' => 'baz',
Expand All @@ -113,7 +109,7 @@ public function test request with query string(int $version)
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_INVOCATION_CONTEXT' => json_encode($this->fakeContext),
'LAMBDA_REQUEST_CONTEXT' => '{"path":"\/hello"}',
'LAMBDA_REQUEST_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand All @@ -128,9 +124,6 @@ public function test request with multivalues query string have basic su
'version' => '1.0',
'httpMethod' => 'GET',
'path' => '/hello',
'requestContext' => [
'path' => '/hello',
],
// See https://aws.amazon.com/blogs/compute/support-for-multi-value-parameters-in-amazon-api-gateway/
'multiValueQueryStringParameters' => [
'foo[]' => ['bar', 'baz'],
Expand Down Expand Up @@ -174,7 +167,7 @@ public function test request with multivalues query string have basic su
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_INVOCATION_CONTEXT' => json_encode($this->fakeContext),
'LAMBDA_REQUEST_CONTEXT' => '{"path":"\/hello"}',
'LAMBDA_REQUEST_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand All @@ -187,7 +180,6 @@ public function test request with requestContext array support()
'httpMethod' => 'GET',
'path' => '/hello',
'requestContext' => [
'path' => '/hello',
'foo' => 'baz',
'baz' => 'far',
'data' => [
Expand All @@ -211,7 +203,7 @@ public function test request with requestContext array support()
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_INVOCATION_CONTEXT' => json_encode($this->fakeContext),
'LAMBDA_REQUEST_CONTEXT' => '{"path":"\/hello","foo":"baz","baz":"far","data":{"recurse1":1,"recurse2":2}}',
'LAMBDA_REQUEST_CONTEXT' => '{"foo":"baz","baz":"far","data":{"recurse1":1,"recurse2":2}}',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down

0 comments on commit 69f9a8a

Please sign in to comment.