From f8d5cde9752b333553d8d5ca03d977bf3e97e314 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Tue, 6 Sep 2022 23:24:00 +0200 Subject: [PATCH] If "cookie" is already contained in headers, do not yield parsed cookies Fixes issue #8 --- .../aws/lambda/FromApiGateway.class.php | 7 +- .../unittest/InvocationEventsTest.class.php | 103 ++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 src/test/php/com/amazon/aws/lambda/unittest/InvocationEventsTest.class.php diff --git a/src/main/php/com/amazon/aws/lambda/FromApiGateway.class.php b/src/main/php/com/amazon/aws/lambda/FromApiGateway.class.php index 446519e..4e420a9 100755 --- a/src/main/php/com/amazon/aws/lambda/FromApiGateway.class.php +++ b/src/main/php/com/amazon/aws/lambda/FromApiGateway.class.php @@ -8,6 +8,7 @@ * Input from Amazon AWS API Gateway version 2.0 * * @test com.amazon.aws.lambda.unittest.FromApiGatewayTest + * @test com.amazon.aws.lambda.unittest.InvocationEventsTest * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html */ class FromApiGateway implements Input { @@ -64,9 +65,9 @@ public function uri() { public function headers() { yield 'remote-addr' => $this->event['requestContext']['http']['sourceIp'] ?? '127.0.0.1'; yield from $this->event['headers'] ?? []; - if (!empty($this->event['cookies'])) { - yield 'cookie' => implode('; ', $this->event['cookies']); - } + + if (isset($this->event['headers']['cookie']) || empty($this->event['cookies'])) return; + yield 'cookie' => implode('; ', $this->event['cookies']); } /** @return ?io.streams.InputStream */ diff --git a/src/test/php/com/amazon/aws/lambda/unittest/InvocationEventsTest.class.php b/src/test/php/com/amazon/aws/lambda/unittest/InvocationEventsTest.class.php new file mode 100644 index 0000000..fcbc194 --- /dev/null +++ b/src/test/php/com/amazon/aws/lambda/unittest/InvocationEventsTest.class.php @@ -0,0 +1,103 @@ + '2.0', + 'routeKey' => '$default', + 'rawPath' => '/', + 'rawQueryString' => $query, + 'cookies' => $cookies, + 'headers' => $headers, + 'requestContext' => [ + 'accountId' => 'anonymous', + 'apiId' => 'r3vzu5yn3irlwjl4z1ves4w2ne0vkzeg', + 'domainName' => 'r3vzu5yn3irlwjl4z1ves4w2ne0vkzeg.lambda-url.eu-central-1.on.aws', + 'domainPrefix' => 'r3vzu5yn3irlwjl4z1ves4w2ne0vkzeg', + 'http' => [ + 'method' => $method, + 'path' => '/', + 'protocol' => 'HTTP/1.1', + 'sourceIp' => '192.168.178.1', + 'userAgent' => 'XP/Test' + ], + 'requestId' => '93c8108b-7d54-49ff-9b52-23e350b7df63=', + 'routeKey' => '$default', + 'stage' => '$default', + 'time' => '06/Sep/2022:20:45:07 +0000', + 'timeEpoch' => 1662497107430 + ], + 'isBase64Encoded' => $body instanceof Bytes, + 'body' => $body + ]); + } + + /** Returns a new fixture */ + private function fromHttpApiGateway($method= 'GET', $query= '', $headers= [], $body= null) { + + // HTTP API gateway invocations only contain parsed cookies + if (isset($headers['cookie'])) { + $cookies= explode('; ', $headers['cookie']); + unset($headers['cookie']); + } else { + $cookies= []; + } + + return new FromApiGateway([ + 'version' => '2.0', + 'routeKey' => 'ANY /test', + 'rawPath' => '/default/test', + 'rawQueryString' => $query, + 'cookies' => $cookies, + 'headers' => $headers, + 'requestContext' => [ + 'accountId' => '123456789012', + 'apiId' => 'r3pmxmplak', + 'domainName' => 'r3pmxmplak.execute-api.us-east-2.amazonaws.com', + 'domainPrefix' => 'r3pmxmplak', + 'http' => [ + 'method' => $method, + 'path' => '/default/test', + 'protocol' => 'HTTP/1.1', + 'sourceIp' => '192.168.178.1', + 'userAgent' => 'XP/Test' + ], + 'requestId' => 'JKJaXmPLvHcESHA=', + 'routeKey' => 'ANY /test', + 'stage' => 'default', + 'time' => '10/Mar/2020:05:16:23 +0000', + 'timeEpoch' => 1583817383220 + ], + 'isBase64Encoded' => $body instanceof Bytes, + 'body' => $body + ]); + } + + #[Test, Values(['fromLambdaFunctionUrl', 'fromHttpApiGateway'])] + public function one_cookie($source) { + $req= new Request($this->{$source}('GET', '', ['cookie' => 'session=6317aaa1de197746d20fadc1'])); + Assert::equals(['session' => '6317aaa1de197746d20fadc1'], $req->cookies()); + } + + #[Test, Values(['fromLambdaFunctionUrl', 'fromHttpApiGateway'])] + public function two_cookies($source) { + $req= new Request($this->{$source}('GET', '', ['cookie' => 'session=6317aaa1de197746d20fadc1; lang=en'])); + Assert::equals(['session' => '6317aaa1de197746d20fadc1', 'lang' => 'en'], $req->cookies()); + } +} \ No newline at end of file