Skip to content

Commit

Permalink
feat(cts): add assertions for header parameters (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored May 12, 2022
1 parent 7f3d6ee commit 3ca31cb
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions generators/src/main/java/com/algolia/codegen/cts/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down
3 changes: 3 additions & 0 deletions scripts/ci/githubActions/setRunVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
};

Expand Down
34 changes: 20 additions & 14 deletions templates/java/libraries/okhttp-gson/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -387,22 +387,28 @@ public class ApiClient {
* @param reqBuilder Request.Builder
*/
public void processHeaderParams(Map<String, String> headerParams, Map<String, String> extraHeaderParams, Request.Builder reqBuilder) {
for (Entry<String, String> param : headerParams.entrySet()) {
reqBuilder.header(param.getKey(), parameterToString(param.getValue()));
}
for (Entry<String, String> header : defaultHeaderMap.entrySet()) {
if (!headerParams.containsKey(header.getKey())) {
reqBuilder.header(header.getKey(), parameterToString(header.getValue()));
}
for (Entry<String, String> param : headerParams.entrySet()) {
reqBuilder.header(
param.getKey().toLowerCase(),
parameterToString(param.getValue())
);
}
for (Entry<String, String> header : defaultHeaderMap.entrySet()) {
if (!headerParams.containsKey(header.getKey())) {
reqBuilder.header(
header.getKey().toLowerCase(),
parameterToString(header.getValue())
);
}
if (extraHeaderParams != null) {
for (Entry<String, String> header : extraHeaderParams.entrySet()) {
reqBuilder.header(
header.getKey(),
parameterToString(header.getValue())
);
}
}
if (extraHeaderParams != null) {
for (Entry<String, String> header : extraHeaderParams.entrySet()) {
reqBuilder.header(
header.getKey().toLowerCase(),
parameterToString(header.getValue())
);
}
}
}
/**
Expand Down
3 changes: 3 additions & 0 deletions tests/CTS/methods/requests/search/assignUserId.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"method": "POST",
"body": {
"cluster": "theCluster"
},
"headers": {
"x-algolia-user-id": "userID"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/CTS/methods/requests/search/batchAssignUserIds.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"user1",
"user2"
]
},
"headers": {
"x-algolia-user-id": "userID"
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/CTS/methods/requests/templates/java/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class {{client}}Tests {
assertEquals(expectedQuery.get(p.getKey()), p.getValue());
}
{{/request.queryParameters}}

{{#request.headers}}
Map<String, String> expectedHeaders = JSON.deserialize("{{#lambda.escapequotes}}{{{request.headers}}}{{/lambda.escapequotes}}", new TypeToken<HashMap<String, String>>() {}.getType());
Map<String, String> actualHeaders = req.headers;

for (Map.Entry<String, String> p : expectedHeaders.entrySet()) {
assertEquals(actualHeaders.get(p.getKey()), p.getValue());
}
{{/request.headers}}
}
{{/tests}}
{{/blocks}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
16 changes: 16 additions & 0 deletions tests/CTS/methods/requests/templates/php/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
}
}

Expand Down Expand Up @@ -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}}
],
]);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/output/java/src/test/java/com/algolia/CallEcho.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,6 +62,14 @@ private Map<String, String> buildQueryParameters() {
return params;
}

private Map<String, String> buildHeaders(Headers headers) {
Map<String, String> 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();
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public class EchoResponse {
public String method;
public String body;
public Map<String, String> queryParameters;
public Map<String, String> headers;
}

0 comments on commit 3ca31cb

Please sign in to comment.