From 3ca31cbdfb99eeb6ece9f4387acfe8aca426b047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vannicatte?= Date: Thu, 12 May 2022 15:21:36 +0200 Subject: [PATCH] feat(cts): add assertions for header parameters (#500) --- .../RequestOptions/RequestOptionsFactory.php | 14 +++++++- .../java/com/algolia/codegen/cts/Request.java | 4 +++ scripts/ci/githubActions/setRunVariables.ts | 3 ++ .../libraries/okhttp-gson/ApiClient.mustache | 34 +++++++++++-------- .../methods/requests/search/assignUserId.json | 3 ++ .../requests/search/batchAssignUserIds.json | 3 ++ .../requests/templates/java/requests.mustache | 9 +++++ .../templates/javascript/requests.mustache | 5 +++ .../requests/templates/php/requests.mustache | 16 +++++++++ .../src/test/java/com/algolia/CallEcho.java | 11 ++++++ .../test/java/com/algolia/EchoResponse.java | 1 + 11 files changed, 88 insertions(+), 15 deletions(-) diff --git a/clients/algoliasearch-client-php/lib/RequestOptions/RequestOptionsFactory.php b/clients/algoliasearch-client-php/lib/RequestOptions/RequestOptionsFactory.php index ecf9baebec..55e616e7f2 100644 --- a/clients/algoliasearch-client-php/lib/RequestOptions/RequestOptionsFactory.php +++ b/clients/algoliasearch-client-php/lib/RequestOptions/RequestOptionsFactory.php @@ -64,7 +64,19 @@ private function normalize($options) foreach ($options as $optionName => $value) { if (is_array($value)) { - $normalized[$optionName] = $this->format($value); + if ($optionName == 'headers') { + $headersToLowerCase = []; + + foreach ($value as $key => $v) { + $headersToLowerCase[strtolower($key)] = $v; + } + + $normalized[$optionName] = $this->format( + $headersToLowerCase + ); + } else { + $normalized[$optionName] = $this->format($value); + } } else { $normalized[$optionName] = $value; } diff --git a/generators/src/main/java/com/algolia/codegen/cts/Request.java b/generators/src/main/java/com/algolia/codegen/cts/Request.java index a513749f2c..23d51b947f 100644 --- a/generators/src/main/java/com/algolia/codegen/cts/Request.java +++ b/generators/src/main/java/com/algolia/codegen/cts/Request.java @@ -39,6 +39,9 @@ class RequestProp { @JsonDeserialize(using = RawDeserializer.class) public String queryParameters; + @JsonDeserialize(using = RawDeserializer.class) + public String headers; + @Override public String toString() { StringBuilder sb = new StringBuilder(); @@ -47,6 +50,7 @@ public String toString() { sb.append(" method: ").append(method).append("\n"); sb.append(" body: ").append(body).append("\n"); sb.append(" queryParameters: ").append(queryParameters).append("\n"); + sb.append(" headers: ").append(headers).append("\n"); sb.append("}"); return sb.toString(); } diff --git a/scripts/ci/githubActions/setRunVariables.ts b/scripts/ci/githubActions/setRunVariables.ts index 61714678a1..b44db3a2fd 100644 --- a/scripts/ci/githubActions/setRunVariables.ts +++ b/scripts/ci/githubActions/setRunVariables.ts @@ -63,18 +63,21 @@ export const DEPENDENCIES = { 'generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java', `:!${JS_CLIENT_FOLDER}/.github`, `:!${JS_CLIENT_FOLDER}/README.md`, + 'tests/CTS/methods/requests/templates/javascript', ], JAVA_CLIENT_CHANGED: [ ...CLIENTS_COMMON_FILES, JAVA_CLIENT_FOLDER, 'templates/java', 'generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java', + 'tests/CTS/methods/requests/templates/java', ], PHP_CLIENT_CHANGED: [ ...CLIENTS_COMMON_FILES, PHP_CLIENT_FOLDER, 'templates/php', 'generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java', + 'tests/CTS/methods/requests/templates/php', ], }; diff --git a/templates/java/libraries/okhttp-gson/ApiClient.mustache b/templates/java/libraries/okhttp-gson/ApiClient.mustache index 9accd01b77..c29a94afa0 100644 --- a/templates/java/libraries/okhttp-gson/ApiClient.mustache +++ b/templates/java/libraries/okhttp-gson/ApiClient.mustache @@ -387,22 +387,28 @@ public class ApiClient { * @param reqBuilder Request.Builder */ public void processHeaderParams(Map headerParams, Map extraHeaderParams, Request.Builder reqBuilder) { - for (Entry param : headerParams.entrySet()) { - reqBuilder.header(param.getKey(), parameterToString(param.getValue())); - } - for (Entry header : defaultHeaderMap.entrySet()) { - if (!headerParams.containsKey(header.getKey())) { - reqBuilder.header(header.getKey(), parameterToString(header.getValue())); - } + for (Entry param : headerParams.entrySet()) { + reqBuilder.header( + param.getKey().toLowerCase(), + parameterToString(param.getValue()) + ); + } + for (Entry header : defaultHeaderMap.entrySet()) { + if (!headerParams.containsKey(header.getKey())) { + reqBuilder.header( + header.getKey().toLowerCase(), + parameterToString(header.getValue()) + ); } - if (extraHeaderParams != null) { - for (Entry header : extraHeaderParams.entrySet()) { - reqBuilder.header( - header.getKey(), - parameterToString(header.getValue()) - ); - } + } + if (extraHeaderParams != null) { + for (Entry header : extraHeaderParams.entrySet()) { + reqBuilder.header( + header.getKey().toLowerCase(), + parameterToString(header.getValue()) + ); } + } } /** diff --git a/tests/CTS/methods/requests/search/assignUserId.json b/tests/CTS/methods/requests/search/assignUserId.json index 6c90be0f53..cf02acd2b2 100644 --- a/tests/CTS/methods/requests/search/assignUserId.json +++ b/tests/CTS/methods/requests/search/assignUserId.json @@ -11,6 +11,9 @@ "method": "POST", "body": { "cluster": "theCluster" + }, + "headers": { + "x-algolia-user-id": "userID" } } } diff --git a/tests/CTS/methods/requests/search/batchAssignUserIds.json b/tests/CTS/methods/requests/search/batchAssignUserIds.json index c524f8266f..6450b986f4 100644 --- a/tests/CTS/methods/requests/search/batchAssignUserIds.json +++ b/tests/CTS/methods/requests/search/batchAssignUserIds.json @@ -19,6 +19,9 @@ "user1", "user2" ] + }, + "headers": { + "x-algolia-user-id": "userID" } } } diff --git a/tests/CTS/methods/requests/templates/java/requests.mustache b/tests/CTS/methods/requests/templates/java/requests.mustache index 06b7f1708c..ebc2fa4994 100644 --- a/tests/CTS/methods/requests/templates/java/requests.mustache +++ b/tests/CTS/methods/requests/templates/java/requests.mustache @@ -62,6 +62,15 @@ class {{client}}Tests { assertEquals(expectedQuery.get(p.getKey()), p.getValue()); } {{/request.queryParameters}} + + {{#request.headers}} + Map expectedHeaders = JSON.deserialize("{{#lambda.escapequotes}}{{{request.headers}}}{{/lambda.escapequotes}}", new TypeToken>() {}.getType()); + Map actualHeaders = req.headers; + + for (Map.Entry p : expectedHeaders.entrySet()) { + assertEquals(actualHeaders.get(p.getKey()), p.getValue()); + } + {{/request.headers}} } {{/tests}} {{/blocks}} diff --git a/tests/CTS/methods/requests/templates/javascript/requests.mustache b/tests/CTS/methods/requests/templates/javascript/requests.mustache index b373c32ab2..cf8e549cdd 100644 --- a/tests/CTS/methods/requests/templates/javascript/requests.mustache +++ b/tests/CTS/methods/requests/templates/javascript/requests.mustache @@ -17,6 +17,11 @@ describe('{{operationId}}', () => { expect(req.method).toEqual('{{{request.method}}}'); expect(req.data).toEqual({{#request.body}}{{{.}}}{{/request.body}}{{^request.body}}undefined{{/request.body}}); expect(req.searchParams).toStrictEqual({{#request.queryParameters}}{{{.}}}{{/request.queryParameters}}{{^request.queryParameters}}undefined{{/request.queryParameters}}); + {{#request.headers}} + expect(req.headers).toEqual( + expect.objectContaining({{{request.headers}}}) + ); + {{/request.headers}} }); {{/tests}} diff --git a/tests/CTS/methods/requests/templates/php/requests.mustache b/tests/CTS/methods/requests/templates/php/requests.mustache index dae33e5d14..6c34de8ca0 100644 --- a/tests/CTS/methods/requests/templates/php/requests.mustache +++ b/tests/CTS/methods/requests/templates/php/requests.mustache @@ -50,6 +50,19 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface $recordedRequest->getUri()->getQuery() ); } + + if (isset($request['headers'])) { + foreach ($request['headers'] as $key => $value) { + $this->assertArrayHasKey( + $key, + $recordedRequest->getHeaders() + ); + $this->assertEquals( + $recordedRequest->getHeaderLine($key), + $value + ); + } + } } } @@ -92,6 +105,9 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface {{#request.queryParameters}} "queryParameters" => json_decode("{{#lambda.escapequotes}}{{{request.queryParameters}}}{{/lambda.escapequotes}}", true), {{/request.queryParameters}} + {{#request.headers}} + "headers" => json_decode("{{#lambda.escapequotes}}{{{request.headers}}}{{/lambda.escapequotes}}", true), + {{/request.headers}} ], ]); } diff --git a/tests/output/java/src/test/java/com/algolia/CallEcho.java b/tests/output/java/src/test/java/com/algolia/CallEcho.java index 3612b7e9af..a4bf6cb215 100644 --- a/tests/output/java/src/test/java/com/algolia/CallEcho.java +++ b/tests/output/java/src/test/java/com/algolia/CallEcho.java @@ -6,6 +6,7 @@ import java.util.Map; import okhttp3.Call; import okhttp3.Callback; +import okhttp3.Headers; import okhttp3.HttpUrl; import okhttp3.MediaType; import okhttp3.Protocol; @@ -61,6 +62,14 @@ private Map buildQueryParameters() { return params; } + private Map buildHeaders(Headers headers) { + Map mapHeaders = new HashMap<>(); + for (String headerName : headers.names()) { + mapHeaders.put(headerName, headers.get(headerName)); + } + return mapHeaders; + } + @Override public void enqueue(Callback callback) { Response.Builder builder = new Response.Builder(); @@ -74,6 +83,8 @@ public void enqueue(Callback callback) { body.method = request.method(); body.body = processResponseBody(); body.queryParameters = buildQueryParameters(); + body.headers = buildHeaders(request.headers()); + builder.body( ResponseBody.create( JSON.serialize(body), diff --git a/tests/output/java/src/test/java/com/algolia/EchoResponse.java b/tests/output/java/src/test/java/com/algolia/EchoResponse.java index c49f8f9d4f..6b8f41a83c 100644 --- a/tests/output/java/src/test/java/com/algolia/EchoResponse.java +++ b/tests/output/java/src/test/java/com/algolia/EchoResponse.java @@ -8,4 +8,5 @@ public class EchoResponse { public String method; public String body; public Map queryParameters; + public Map headers; }