Skip to content

Commit

Permalink
Merge pull request #56 from vdechenaux/fix-arrays-in-query-string
Browse files Browse the repository at this point in the history
Fix arrays handling in query string
  • Loading branch information
mnapoli authored Aug 19, 2018
2 parents f85a882 + a8fe1a4 commit d362da1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Bridge/Psr7/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RequestFactory
public static function fromLambdaEvent(array $event) : ServerRequestInterface
{
$method = $event['httpMethod'] ?? 'GET';
$query = $event['queryStringParameters'] ?? [];
$query = [];
$bodyString = $event['body'] ?? '';
$body = self::createBodyStream($bodyString);
$parsedBody = null;
Expand All @@ -30,6 +30,17 @@ public static function fromLambdaEvent(array $event) : ServerRequestInterface
$headers = $event['headers'] ?? [];
$protocolVersion = $event['requestContext']['protocol'] ?? '1.1';

/*
* queryStringParameters does not handle correctly arrays in parameters
* ?array[key]=value gives ['array[key]' => 'value'] while we want ['array' => ['key' = > 'value']]
* We recreate the original query string and we use parse_str which handles correctly arrays
*
* There's still an issue: AWS API Gateway does not support multiple query string parameters with the same name
* So you can't use something like ?array[]=val1&array[]=val2 because only the 'val2' value will survive
*/
$queryString = http_build_query($event['queryStringParameters'] ?? []);
parse_str($queryString, $query);

$cookies = [];
if (isset($headers['Cookie'])) {
$cookieParts = explode('; ', $headers['Cookie']);
Expand All @@ -51,7 +62,7 @@ public static function fromLambdaEvent(array $event) : ServerRequestInterface
'SERVER_PROTOCOL' => $protocolVersion,
'REQUEST_METHOD' => $method,
'REQUEST_TIME' => $event['requestContext']['requestTimeEpoch'] ?? time(),
'QUERY_STRING' => $query ? http_build_query($query) : '',
'QUERY_STRING' => $queryString,
'DOCUMENT_ROOT' => getcwd(),
'REQUEST_URI' => $uri,
];
Expand Down
20 changes: 20 additions & 0 deletions tests/Bridge/Psr7/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,24 @@ public function test cookies are supported()
'theme' => 'light'
], $request->getCookieParams());
}

public function test arrays in query string are supported()
{
$request = RequestFactory::fromLambdaEvent([
'httpMethod' => 'GET',
'queryStringParameters' => [
'vars[val1]' => 'foo',
'vars[val2][]' => 'bar',
],
]);

self::assertEquals([
'vars' => [
'val1' => 'foo',
'val2' => [
'bar',
]
]
], $request->getQueryParams());
}
}

0 comments on commit d362da1

Please sign in to comment.