fix(emulator): sort query keys like the platformUnverified
29dad8b parent: 22edd31 modified
README.md +2 -2 | @@ -392,7 +392,7 @@ PHP's built-in server receives the requests, and forwards the Events to that pro | ||
| 392 | 392 | |
| 393 | 393 | The Event and the context are built the way the platform builds them ([measured](docs/probe-results.md)): |
| 394 | 394 | |
| 395 | -- the `httpMethod`, `queryStringParameters` (the last value of a repeated key winning, `[]` without query string) and other keys of the platform; | |
| 395 | +- the `httpMethod`, `queryStringParameters` (sorted by key, the last value of a repeated key winning, `[]` without query string) and other keys of the platform; | |
| 396 | 396 | - header names in `Title-Case`, repeated headers joined with `, `, no `Host`; |
| 397 | 397 | - `X-Real-Ip`, `X-Envoy-External-Address`, `X-Request-Id` and `X-Forwarded-Proto: https` set whatever the client sends, the client's `X-Forwarded-For` kept in front of its address, its `X-Forwarded-Host` passed through; |
| 398 | 398 | - empty, multipart and non UTF-8 bodies base64-encoded with `isBase64Encoded: true`, text bodies without the flag. |
| @@ -449,7 +449,7 @@ Everything else survives, so your application must be safe for long-running exec | ||
| 449 | 449 | Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation. |
| 450 | 450 | - **Output**: only the returned `Response` reaches the client. |
| 451 | 451 | `echo` outside the Response, `header()` and `http_response_code()` do not: Scaleway does not put stdout in the response body. |
| 452 | -- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only. | |
| 452 | +- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only, and the keys arrive sorted, not in the order the client sent them. | |
| 453 | 453 | Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests. |
| 454 | 454 | - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does. |
| 455 | 455 | Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not. |
| @@ -392,7 +392,7 @@ PHP's built-in server receives the requests, and forwards the Events to that pro | |||
| 392 | 392 | ||
| 393 | The Event and the context are built the way the platform builds them ([measured](docs/probe-results.md)): | 393 | The Event and the context are built the way the platform builds them ([measured](docs/probe-results.md)): |
| 394 | 394 | ||
| 395 | -- the `httpMethod`, `queryStringParameters` (the last value of a repeated key winning, `[]` without query string) and other keys of the platform; | 395 | +- the `httpMethod`, `queryStringParameters` (sorted by key, the last value of a repeated key winning, `[]` without query string) and other keys of the platform; |
| 396 | - header names in `Title-Case`, repeated headers joined with `, `, no `Host`; | 396 | - header names in `Title-Case`, repeated headers joined with `, `, no `Host`; |
| 397 | - `X-Real-Ip`, `X-Envoy-External-Address`, `X-Request-Id` and `X-Forwarded-Proto: https` set whatever the client sends, the client's `X-Forwarded-For` kept in front of its address, its `X-Forwarded-Host` passed through; | 397 | - `X-Real-Ip`, `X-Envoy-External-Address`, `X-Request-Id` and `X-Forwarded-Proto: https` set whatever the client sends, the client's `X-Forwarded-For` kept in front of its address, its `X-Forwarded-Host` passed through; |
| 398 | - empty, multipart and non UTF-8 bodies base64-encoded with `isBase64Encoded: true`, text bodies without the flag. | 398 | - empty, multipart and non UTF-8 bodies base64-encoded with `isBase64Encoded: true`, text bodies without the flag. |
| @@ -449,7 +449,7 @@ Everything else survives, so your application must be safe for long-running exec | |||
| 449 | Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation. | 449 | Read the `Request` object instead; `Request::createFromGlobals()` does not describe the Invocation. |
| 450 | - **Output**: only the returned `Response` reaches the client. | 450 | - **Output**: only the returned `Response` reaches the client. |
| 451 | `echo` outside the Response, `header()` and `http_response_code()` do not: Scaleway does not put stdout in the response body. | 451 | `echo` outside the Response, `header()` and `http_response_code()` do not: Scaleway does not put stdout in the response body. |
| 452 | -- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only. | 452 | +- **Query strings**: the Event carries `queryStringParameters` as a map, so a repeated key such as `a=1&a=2`, or `tags[]=a&tags[]=b`, reaches the Application with its last value only, and the keys arrive sorted, not in the order the client sent them. |
| 453 | Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests. | 453 | Keys are then parsed like PHP does, so `a.b` becomes `a_b` and `a[b]` nests. |
| 454 | - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does. | 454 | - **Form bodies**: `application/x-www-form-urlencoded` bodies are parsed for `POST`, `PUT`, `DELETE`, `PATCH` and `QUERY`, `multipart/form-data` only for `POST`, as PHP does. |
| 455 | Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not. | 455 | Uploaded files are `UploadedFile` objects in test mode: `move()` works, `is_uploaded_file()` does not. |
modified
docs/probe-results.md +1 -1 | @@ -28,7 +28,7 @@ Client addresses are replaced with documentation addresses (`2001:db8::1`, `203. | ||
| 28 | 28 | Keys: `resource`, `path`, `httpMethod`, `headers`, `multiValueHeaders` (null), `queryStringParameters`, `multiValueQueryStringParameters` (null), `pathParameters` (null), `stageVariables`, `requestContext` (with `httpMethod`), `body`, `isBase64Encoded`. |
| 29 | 29 | |
| 30 | 30 | - The method is in `httpMethod`; there is no `method` key. |
| 31 | -- `queryStringParameters` is decoded and keeps the last value of repeated keys: `?repeated=a&repeated=b&list[]=x&list[]=y` gives `{"repeated": "b", "list[]": "y"}`; it is an empty list `[]` when there is no query string. | |
| 31 | +- `queryStringParameters` is decoded, sorted by key, and keeps the last value of repeated keys: `?repeated=a&repeated=b&list[]=x&list[]=y` gives `{"repeated": "b", "list[]": "y"}`; it is an empty list `[]` when there is no query string. | |
| 32 | 32 | - Header names are canonicalised (`X-Custom`, `User-Agent`), single string values. |
| 33 | 33 | - There is no `Host` header. |
| 34 | 34 | - `isBase64Encoded` is `true` with a base64 body for binary and multipart bodies, `true` with an empty body for GET, and absent for text bodies (JSON, plain text). |
| @@ -28,7 +28,7 @@ Client addresses are replaced with documentation addresses (`2001:db8::1`, `203. | |||
| 28 | Keys: `resource`, `path`, `httpMethod`, `headers`, `multiValueHeaders` (null), `queryStringParameters`, `multiValueQueryStringParameters` (null), `pathParameters` (null), `stageVariables`, `requestContext` (with `httpMethod`), `body`, `isBase64Encoded`. | 28 | Keys: `resource`, `path`, `httpMethod`, `headers`, `multiValueHeaders` (null), `queryStringParameters`, `multiValueQueryStringParameters` (null), `pathParameters` (null), `stageVariables`, `requestContext` (with `httpMethod`), `body`, `isBase64Encoded`. |
| 29 | 29 | ||
| 30 | - The method is in `httpMethod`; there is no `method` key. | 30 | - The method is in `httpMethod`; there is no `method` key. |
| 31 | -- `queryStringParameters` is decoded and keeps the last value of repeated keys: `?repeated=a&repeated=b&list[]=x&list[]=y` gives `{"repeated": "b", "list[]": "y"}`; it is an empty list `[]` when there is no query string. | 31 | +- `queryStringParameters` is decoded, sorted by key, and keeps the last value of repeated keys: `?repeated=a&repeated=b&list[]=x&list[]=y` gives `{"repeated": "b", "list[]": "y"}`; it is an empty list `[]` when there is no query string. |
| 32 | - Header names are canonicalised (`X-Custom`, `User-Agent`), single string values. | 32 | - Header names are canonicalised (`X-Custom`, `User-Agent`), single string values. |
| 33 | - There is no `Host` header. | 33 | - There is no `Host` header. |
| 34 | - `isBase64Encoded` is `true` with a base64 body for binary and multipart bodies, `true` with an empty body for GET, and absent for text bodies (JSON, plain text). | 34 | - `isBase64Encoded` is `true` with a base64 body for binary and multipart bodies, `true` with an empty body for GET, and absent for text bodies (JSON, plain text). |
modified
example/e2e/run.sh +1 -1 | @@ -63,7 +63,7 @@ status=$(request query "$base_url/echo?foo=bar&n=42&list%5Bkey%5D=v") | ||
| 63 | 63 | expect "GET /echo status" "$status" 200 |
| 64 | 64 | expect "GET /echo method" "$(field query method)" GET |
| 65 | 65 | expect "GET /echo path" "$(field query path)" /echo |
| 66 | -expect "GET /echo query" "$(field query query)" '{"foo":"bar","n":"42","list":{"key":"v"}}' | |
| 66 | +expect "GET /echo query" "$(field query query)" '{"foo":"bar","list":{"key":"v"},"n":"42"}' | |
| 67 | 67 | |
| 68 | 68 | status=$(request headers -H 'X-Custom: hello' -H 'Accept-Language: fr' --cookie 'flavor=chocolate; size=large' "$base_url/echo") |
| 69 | 69 | expect "custom request header" "$(field headers headers.x-custom)" hello |
| @@ -63,7 +63,7 @@ status=$(request query "$base_url/echo?foo=bar&n=42&list%5Bkey%5D=v") | |||
| 63 | expect "GET /echo status" "$status" 200 | 63 | expect "GET /echo status" "$status" 200 |
| 64 | expect "GET /echo method" "$(field query method)" GET | 64 | expect "GET /echo method" "$(field query method)" GET |
| 65 | expect "GET /echo path" "$(field query path)" /echo | 65 | expect "GET /echo path" "$(field query path)" /echo |
| 66 | -expect "GET /echo query" "$(field query query)" '{"foo":"bar","n":"42","list":{"key":"v"}}' | 66 | +expect "GET /echo query" "$(field query query)" '{"foo":"bar","list":{"key":"v"},"n":"42"}' |
| 67 | 67 | ||
| 68 | status=$(request headers -H 'X-Custom: hello' -H 'Accept-Language: fr' --cookie 'flavor=chocolate; size=large' "$base_url/echo") | 68 | status=$(request headers -H 'X-Custom: hello' -H 'Accept-Language: fr' --cookie 'flavor=chocolate; size=large' "$base_url/echo") |
| 69 | expect "custom request header" "$(field headers headers.x-custom)" hello | 69 | expect "custom request header" "$(field headers headers.x-custom)" hello |
modified
src/Emulator.php +2 -0 | @@ -39,6 +39,8 @@ final class Emulator | ||
| 39 | 39 | $query[urldecode($name)] = urldecode($value); |
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | + // The platform sorts the keys, so the application never sees the client's order. | |
| 43 | + ksort($query); | |
| 42 | 44 | |
| 43 | 45 | $canonical = self::canonicalize($headers); |
| 44 | 46 | $contentType = $canonical['Content-Type'] ?? ''; |
| @@ -39,6 +39,8 @@ final class Emulator | |||
| 39 | $query[urldecode($name)] = urldecode($value); | 39 | $query[urldecode($name)] = urldecode($value); |
| 40 | } | 40 | } |
| 41 | } | 41 | } |
| 42 | + // The platform sorts the keys, so the application never sees the client's order. | ||
| 43 | + ksort($query); | ||
| 42 | 44 | ||
| 43 | $canonical = self::canonicalize($headers); | 45 | $canonical = self::canonicalize($headers); |
| 44 | $contentType = $canonical['Content-Type'] ?? ''; | 46 | $contentType = $canonical['Content-Type'] ?? ''; |
modified
tests/EmulatorTest.php +2 -2 | @@ -36,7 +36,7 @@ final class EmulatorTest extends TestCase | ||
| 36 | 36 | 'X-Real-Ip' => '2001:db8::1', |
| 37 | 37 | ], |
| 38 | 38 | 'multiValueHeaders' => null, |
| 39 | - 'queryStringParameters' => ['sort' => 'name', 'filter[role]' => 'admin user', 'flag' => ''], | |
| 39 | + 'queryStringParameters' => ['filter[role]' => 'admin user', 'flag' => '', 'sort' => 'name'], | |
| 40 | 40 | 'multiValueQueryStringParameters' => null, |
| 41 | 41 | 'pathParameters' => null, |
| 42 | 42 | 'stageVariables' => [], |
| @@ -90,7 +90,7 @@ final class EmulatorTest extends TestCase | ||
| 90 | 90 | $request = HttpConverter::toRequest(Emulator::toEvent('GET', '/search?q=caf%C3%A9&page%5Bsize%5D=10', [], '', '192.0.2.1')); |
| 91 | 91 | |
| 92 | 92 | self::assertSame('/search', $request->getPathInfo()); |
| 93 | - self::assertSame(['q' => 'café', 'page' => ['size' => '10']], $request->query->all()); | |
| 93 | + self::assertSame(['page' => ['size' => '10'], 'q' => 'café'], $request->query->all()); | |
| 94 | 94 | self::assertSame('192.0.2.1', $request->getClientIp()); |
| 95 | 95 | self::assertTrue($request->isSecure()); |
| 96 | 96 | } |
| @@ -36,7 +36,7 @@ final class EmulatorTest extends TestCase | |||
| 36 | 'X-Real-Ip' => '2001:db8::1', | 36 | 'X-Real-Ip' => '2001:db8::1', |
| 37 | ], | 37 | ], |
| 38 | 'multiValueHeaders' => null, | 38 | 'multiValueHeaders' => null, |
| 39 | - 'queryStringParameters' => ['sort' => 'name', 'filter[role]' => 'admin user', 'flag' => ''], | 39 | + 'queryStringParameters' => ['filter[role]' => 'admin user', 'flag' => '', 'sort' => 'name'], |
| 40 | 'multiValueQueryStringParameters' => null, | 40 | 'multiValueQueryStringParameters' => null, |
| 41 | 'pathParameters' => null, | 41 | 'pathParameters' => null, |
| 42 | 'stageVariables' => [], | 42 | 'stageVariables' => [], |
| @@ -90,7 +90,7 @@ final class EmulatorTest extends TestCase | |||
| 90 | $request = HttpConverter::toRequest(Emulator::toEvent('GET', '/search?q=caf%C3%A9&page%5Bsize%5D=10', [], '', '192.0.2.1')); | 90 | $request = HttpConverter::toRequest(Emulator::toEvent('GET', '/search?q=caf%C3%A9&page%5Bsize%5D=10', [], '', '192.0.2.1')); |
| 91 | 91 | ||
| 92 | self::assertSame('/search', $request->getPathInfo()); | 92 | self::assertSame('/search', $request->getPathInfo()); |
| 93 | - self::assertSame(['q' => 'café', 'page' => ['size' => '10']], $request->query->all()); | 93 | + self::assertSame(['page' => ['size' => '10'], 'q' => 'café'], $request->query->all()); |
| 94 | self::assertSame('192.0.2.1', $request->getClientIp()); | 94 | self::assertSame('192.0.2.1', $request->getClientIp()); |
| 95 | self::assertTrue($request->isSecure()); | 95 | self::assertTrue($request->isSecure()); |
| 96 | } | 96 | } |