diff --git a/src/Event/Http/FpmHandler.php b/src/Event/Http/FpmHandler.php index 95eb1c024..14ee1d288 100644 --- a/src/Event/Http/FpmHandler.php +++ b/src/Event/Http/FpmHandler.php @@ -229,10 +229,6 @@ private function eventToFastCgiRequest(HttpRequestEvent $event, Context $context $request->setCustomVar('LAMBDA_INVOCATION_CONTEXT', json_encode($context)); $request->setCustomVar('LAMBDA_REQUEST_CONTEXT', json_encode($event->getRequestContext())); - [$user, $password] = $event->getBasicAuthCredentials(); - $request->setCustomVar('PHP_AUTH_USER', $user); - $request->setCustomVar('PHP_AUTH_PW', $password); - $contentType = $event->getContentType(); if ($contentType) { $request->setContentType($contentType); diff --git a/tests/Event/Http/CommonHttpTest.php b/tests/Event/Http/CommonHttpTest.php index efd3c0f67..f89896e37 100644 --- a/tests/Event/Http/CommonHttpTest.php +++ b/tests/Event/Http/CommonHttpTest.php @@ -190,6 +190,20 @@ public function test POST request with form data(int $version) ]); } + /** + * @dataProvider provide API Gateway versions + */ + public function test POST request with form data and content type(int $version) + { + $this->fromFixture(__DIR__ . "/Fixture/ag-v$version-body-form-content-type.json"); + + $this->assertContentType('application/x-www-form-urlencoded;charset=UTF-8'); + $this->assertParsedBody([ + 'foo' => 'bar', + 'bim' => 'baz', + ]); + } + public function provideHttpMethodsWithRequestBodySupport(): array { return [ @@ -438,24 +452,17 @@ public function test path parameters(int $version) ]); } - public function test request with basic auth contains a user and password() + /** + * @dataProvider provide API Gateway versions + */ + public function test request with basic auth(int $version) { - $this->fromFixture(__DIR__ . '/Fixture/ag-v1-header-basic-auth.json'); + $this->fromFixture(__DIR__ . "/Fixture/ag-v$version-header-basic-auth.json"); $this->assertBasicAuthUser('fake'); $this->assertBasicAuthPassword('secret'); } - public function test multipart form content type can have a suffix() - { - $this->fromFixture(__DIR__ . '/Fixture/ag-v1-body-base64-utf8.json'); - - $this->assertContentType('application/x-www-form-urlencoded;charset=UTF-8'); - $this->assertParsedBody([ - 'foo' => 'bar', - ]); - } - abstract protected function fromFixture(string $file): void; abstract protected function assertBody(string $expected): void; diff --git a/tests/Event/Http/Fixture/ag-v1-body-base64-utf8.json b/tests/Event/Http/Fixture/ag-v1-body-form-content-type.json similarity index 96% rename from tests/Event/Http/Fixture/ag-v1-body-base64-utf8.json rename to tests/Event/Http/Fixture/ag-v1-body-form-content-type.json index 35ee7f3e8..2a2454f35 100644 --- a/tests/Event/Http/Fixture/ag-v1-body-base64-utf8.json +++ b/tests/Event/Http/Fixture/ag-v1-body-form-content-type.json @@ -48,6 +48,6 @@ "domainName": "example.org", "apiId": "xxxxxxxxxx" }, - "body": "Zm9vPWJhcg==", - "isBase64Encoded": true + "body": "foo=bar&bim=baz", + "isBase64Encoded": false } diff --git a/tests/Event/Http/Fixture/ag-v1-header-basic-auth.json b/tests/Event/Http/Fixture/ag-v1-header-basic-auth.json index e68d3fb6d..9c2f86c3b 100644 --- a/tests/Event/Http/Fixture/ag-v1-header-basic-auth.json +++ b/tests/Event/Http/Fixture/ag-v1-header-basic-auth.json @@ -13,8 +13,7 @@ "X-Amzn-Trace-Id": "Root=1-ffffffff-ffffffffffffffffffffffff", "X-Forwarded-For": "1.1.1.1", "X-Forwarded-Port": "443", - "X-Forwarded-Proto": "https", - "X-My-Header": "Hello world" + "X-Forwarded-Proto": "https" }, "queryStringParameters": null, "pathParameters": null, diff --git a/tests/Event/Http/Fixture/ag-v2-body-form-content-type.json b/tests/Event/Http/Fixture/ag-v2-body-form-content-type.json new file mode 100644 index 000000000..b1df68246 --- /dev/null +++ b/tests/Event/Http/Fixture/ag-v2-body-form-content-type.json @@ -0,0 +1,42 @@ +{ + "version": "2.0", + "routeKey": "ANY /path", + "rawPath": "/path", + "rawQueryString": "", + "headers": { + "Accept": "*/*", + "Accept-Encoding": "gzip, deflate", + "Cache-Control": "no-cache", + "Content-Length": 15, + "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", + "Host": "example.org", + "User-Agent": "PostmanRuntime/7.20.1", + "X-Amzn-Trace-Id": "Root=1-ffffffff-ffffffffffffffffffffffff", + "X-Forwarded-For": "1.1.1.1", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "queryStringParameters": null, + "stageVariables": null, + "requestContext": { + "accountId": "123400000000", + "apiId": "xxxxxxxxxx", + "domainName": "example.org", + "domainPrefix": "0000000000", + "http": { + "method": "POST", + "path": "/path", + "protocol": "HTTP/1.1", + "sourceIp": "1.1.1.1", + "userAgent": "PostmanRuntime/7.20.1" + }, + "requestId": "JTHoQgr2oAMEPMg=", + "routeId": "47matwk", + "routeKey": "ANY /path", + "stage": "$default", + "time": "24/Nov/2019:18:55:08 +0000", + "timeEpoch": 1574621708700 + }, + "body": "foo=bar&bim=baz", + "isBase64Encoded": false +} diff --git a/tests/Event/Http/Fixture/ag-v2-header-basic-auth.json b/tests/Event/Http/Fixture/ag-v2-header-basic-auth.json new file mode 100644 index 000000000..9be5abc21 --- /dev/null +++ b/tests/Event/Http/Fixture/ag-v2-header-basic-auth.json @@ -0,0 +1,40 @@ +{ + "version": "2.0", + "routeKey": "ANY /path", + "rawPath": "/path", + "rawQueryString": "", + "cookies": [], + "headers": { + "accept": "*/*", + "accept-encoding": "gzip, deflate", + "authorization": "Basic ZmFrZTpzZWNyZXQ=", + "cache-control": "no-cache", + "host": "example.org", + "user-agent": "PostmanRuntime/7.20.1", + "x-amzn-trace-id": "Root=1-ffffffff-ffffffffffffffffffffffff", + "x-forwarded-for": "1.1.1.1", + "x-forwarded-port": "443", + "x-forwarded-proto": "https" + }, + "queryStringParameters": null, + "requestContext": { + "accountId": "123400000000", + "apiId": "xxxxxxxxxx", + "domainName": "example.org", + "domainPrefix": "0000000000", + "http": { + "method": "GET", + "path": "/path", + "protocol": "HTTP/1.1", + "sourceIp": "1.1.1.1", + "userAgent": "PostmanRuntime/7.20.1" + }, + "requestId": "JTHoQgr2oAMEPMg=", + "routeId": "47matwk", + "routeKey": "ANY /path", + "stage": "$default", + "time": "24/Nov/2019:18:55:08 +0000", + "timeEpoch": 1574621708700 + }, + "isBase64Encoded": false +} diff --git a/tests/Handler/FpmHandlerTest.php b/tests/Handler/FpmHandlerTest.php index 3e0f95a3b..f3ec7d00d 100644 --- a/tests/Handler/FpmHandlerTest.php +++ b/tests/Handler/FpmHandlerTest.php @@ -405,6 +405,48 @@ public function test POST request with form data(int $version) ]); } + /** + * @dataProvider provide API Gateway versions + */ + public function test POST request with form data and content type(int $version) + { + $event = [ + 'version' => '1.0', + 'httpMethod' => 'POST', + 'body' => 'foo=bar&bim=baz', + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8', + ], + ]; + $this->assertGlobalVariables($event, [ + '$_GET' => [], + '$_POST' => [ + 'foo' => 'bar', + 'bim' => 'baz', + ], + '$_FILES' => [], + '$_COOKIE' => [], + '$_REQUEST' => [ + 'foo' => 'bar', + 'bim' => 'baz', + ], + '$_SERVER' => [ + 'CONTENT_LENGTH' => '15', + 'CONTENT_TYPE' => 'application/x-www-form-urlencoded;charset=UTF-8', + 'REQUEST_URI' => '/', + 'PHP_SELF' => '/', + 'PATH_INFO' => '/', + 'REQUEST_METHOD' => 'POST', + 'QUERY_STRING' => '', + 'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded;charset=UTF-8', + 'HTTP_CONTENT_LENGTH' => '15', + 'LAMBDA_INVOCATION_CONTEXT' => json_encode($this->fakeContext), + 'LAMBDA_REQUEST_CONTEXT' => '[]', + ], + 'HTTP_RAW_BODY' => 'foo=bar&bim=baz', + ]); + } + public function provideHttpMethodsWithRequestBodySupport(): array { return [ @@ -975,6 +1017,43 @@ public function test OPTIONS request(int $version) ]); } + /** + * @dataProvider provide API Gateway versions + */ + public function test request with basic auth(int $version) + { + $event = [ + 'version' => '1.0', + 'httpMethod' => 'GET', + 'headers' => [ + 'Authorization' => 'Basic ZmFrZTpzZWNyZXQ=', + ], + ]; + $this->assertGlobalVariables($event, [ + '$_GET' => [], + '$_POST' => [], + '$_FILES' => [], + '$_COOKIE' => [], + '$_REQUEST' => [], + '$_SERVER' => [ + 'REQUEST_URI' => '/', + 'PHP_SELF' => '/', + 'PATH_INFO' => '/', + 'REQUEST_METHOD' => 'GET', + 'QUERY_STRING' => '', + 'CONTENT_LENGTH' => '0', + 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', + 'LAMBDA_INVOCATION_CONTEXT' => json_encode($this->fakeContext), + 'LAMBDA_REQUEST_CONTEXT' => '[]', + 'HTTP_AUTHORIZATION' => 'Basic ZmFrZTpzZWNyZXQ=', + // PHP-FPM automatically adds these variables + 'PHP_AUTH_USER' => 'fake', + 'PHP_AUTH_PW' => 'secret', + ], + 'HTTP_RAW_BODY' => '', + ]); + } + /** * @dataProvider provideStatusCodes */ diff --git a/tests/HttpRequestProxyTest.php b/tests/HttpRequestProxyTest.php index 25642606c..4164546ad 100644 --- a/tests/HttpRequestProxyTest.php +++ b/tests/HttpRequestProxyTest.php @@ -25,6 +25,11 @@ public function test POST request with raw body(int $version); public function test POST request with form data(int $version); + /** + * @see https://github.com/brefphp/bref/pull/1325 + */ + public function test POST request with form data and content type(int $version); + /** * @see https://github.com/brefphp/bref/issues/162 */ @@ -51,4 +56,6 @@ public function test PATCH request(int $version); public function test DELETE request(int $version); public function test OPTIONS request(int $version); + + public function test request with basic auth(int $version); }