Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Event/Http/FpmHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ private function eventToFastCgiRequest(HttpRequestEvent $event): ProvidesRequest
$request->setServerPort($event->getServerPort());
$request->setCustomVar('PATH_INFO', $event->getPath());
$request->setCustomVar('QUERY_STRING', $event->getQueryString());
$request->setCustomVar('LAMBDA_CONTEXT', json_encode($event->getRequestContext()));

$contentType = $event->getContentType();
if ($contentType) {
$request->setContentType($contentType);
Expand Down
5 changes: 5 additions & 0 deletions src/Event/Http/HttpRequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public function getQueryParameters(): array
return $query;
}

public function getRequestContext(): array
{
return $this->event['requestContext'] ?? [];
}

public function getCookies(): array
{
if (! isset($this->headers['cookie'])) {
Expand Down
68 changes: 68 additions & 0 deletions tests/Handler/FpmHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function test simple request()
'QUERY_STRING' => '',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '{"protocol":"HTTP\/1.1"}',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -85,6 +86,7 @@ public function test request with query string()
'QUERY_STRING' => 'foo=bar&bim=baz',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -122,11 +124,59 @@ public function test request with multivalues query string have basic su
'QUERY_STRING' => 'foo=bar',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
}

public function test request with requestContext array support()
{
$event = [
'httpMethod' => 'GET',
'path' => '/hello',
// See https://aws.amazon.com/blogs/compute/support-for-multi-value-parameters-in-amazon-api-gateway/
'multiValueQueryStringParameters' => [
'foo' => ['bar', 'baz'],
],
'queryStringParameters' => [
'foo' => 'baz', // the 2nd value is preserved only by API Gateway
],
'requestContext' => [
'foo' => 'baz',
'baz' => 'far',
'data' => [
'recurse1' => 1,
'recurse2' => 2,
],
],
];
$this->assertGlobalVariables($event, [
'$_GET' => [
// TODO The feature is not implemented yet
'foo' => 'bar',
],
'$_POST' => [],
'$_FILES' => [],
'$_COOKIE' => [],
'$_REQUEST' => [
'foo' => 'bar',
],
'$_SERVER' => [
'REQUEST_URI' => '/hello?foo=bar',
'PHP_SELF' => '/hello',
'PATH_INFO' => '/hello',
'REQUEST_METHOD' => 'GET',
'QUERY_STRING' => 'foo=bar',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '{"foo":"baz","baz":"far","data":{"recurse1":1,"recurse2":2}}',
],
'HTTP_RAW_BODY' => '',

]);
}

public function test request with arrays in query string()
{
$event = [
Expand Down Expand Up @@ -160,6 +210,7 @@ public function test request with arrays in query string()
'QUERY_STRING' => 'vars%5Bval1%5D=foo&vars%5Bval2%5D%5B%5D=bar',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -189,6 +240,7 @@ public function test request with custom header()
'HTTP_X_MY_HEADER' => 'Hello world',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -221,6 +273,7 @@ public function test request with custom multi header()
'HTTP_X_MY_HEADER' => 'Hello world',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -252,6 +305,7 @@ public function test POST request with raw body()
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'application/json',
'HTTP_CONTENT_LENGTH' => '14',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '"Hello world!"',
]);
Expand Down Expand Up @@ -288,6 +342,7 @@ public function test POST request with form data()
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'HTTP_CONTENT_LENGTH' => '15',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => 'foo=bar&bim=baz',
]);
Expand Down Expand Up @@ -339,6 +394,7 @@ public function test request with body and no content length(string $meth
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'application/json',
'HTTP_CONTENT_LENGTH' => '14',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '"Hello world!"',
]);
Expand Down Expand Up @@ -371,6 +427,7 @@ public function test request supports utf8 characters in body()
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'text/plain; charset=UTF-8',
'HTTP_CONTENT_LENGTH' => '10',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => 'Hello 🌍',
]);
Expand Down Expand Up @@ -403,6 +460,7 @@ public function test the content type header is not case sensitive()
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'application/json',
'HTTP_CONTENT_LENGTH' => '2',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '{}',
]);
Expand Down Expand Up @@ -450,6 +508,7 @@ public function test POST request with multipart form data()
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=testBoundary',
'HTTP_CONTENT_LENGTH' => '152',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -505,6 +564,7 @@ public function test POST request with multipart form data containing ar
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=testBoundary',
'HTTP_CONTENT_LENGTH' => '186',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -537,6 +597,7 @@ public function test request with cookies()
'HTTP_COOKIE' => 'tz=Europe%2FParis; four=two; theme=light',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -599,6 +660,7 @@ public function test POST request with multipart file uploads()
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=testBoundary',
'HTTP_CONTENT_LENGTH' => '323',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down Expand Up @@ -635,6 +697,7 @@ public function test POST request with base64 encoded body()
'QUERY_STRING' => '',
'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'HTTP_CONTENT_LENGTH' => '7',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => 'foo=bar',
]);
Expand Down Expand Up @@ -664,6 +727,7 @@ public function test HTTP_HOST header()
'HTTP_HOST' => 'www.example.com',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand All @@ -688,6 +752,7 @@ public function test PUT request()
'QUERY_STRING' => '',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand All @@ -712,6 +777,7 @@ public function test PATCH request()
'QUERY_STRING' => '',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand All @@ -736,6 +802,7 @@ public function test DELETE request()
'QUERY_STRING' => '',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand All @@ -760,6 +827,7 @@ public function test OPTIONS request()
'QUERY_STRING' => '',
'CONTENT_LENGTH' => '0',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'LAMBDA_CONTEXT' => '[]',
],
'HTTP_RAW_BODY' => '',
]);
Expand Down