Skip to content

Commit

Permalink
feat(cts): add custom request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Mar 30, 2022
1 parent a7ee6a2 commit ffcf747
Show file tree
Hide file tree
Showing 17 changed files with 1,204 additions and 27 deletions.
30 changes: 9 additions & 21 deletions clients/algoliasearch-client-javascript/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,39 @@
"files": [
{
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
"maxSize": "7.05KB"
"maxSize": "7.20KB"
},
{
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
"maxSize": "3.85KB"
"maxSize": "3.95KB"
},
{
"path": "packages/client-analytics/dist/client-analytics.umd.browser.js",
"maxSize": "4.45KB"
"maxSize": "4.50KB"
},
{
"path": "packages/client-insights/dist/client-insights.umd.browser.js",
"maxSize": "3.70KB"
"maxSize": "3.75KB"
},
{
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
"maxSize": "3.85KB"
"maxSize": "3.90KB"
},
{
"path": "packages/client-query-suggestions/dist/client-query-suggestions.umd.browser.js",
"maxSize": "3.90KB"
"maxSize": "4.00KB"
},
{
"path": "packages/client-search/dist/client-search.umd.browser.js",
"maxSize": "5.85KB"
"maxSize": "6.05KB"
},
{
"path": "packages/client-sources/dist/client-sources.umd.browser.js",
"maxSize": "3.70KB"
},
{
"path": "packages/recommend/dist/recommend.umd.browser.js",
"maxSize": "3.80KB"
},
{
"path": "packages/client-common/dist/client-common.esm.node.js",
"maxSize": "5.25KB"
},
{
"path": "packages/requester-browser-xhr/dist/requester-browser-xhr.esm.node.js",
"maxSize": "900B"
},
{
"path": "packages/requester-node-http/dist/requester-node-http.esm.node.js",
"maxSize": "1.15KB"
"path": "packages/recommend/dist/recommend.umd.browser.js",
"maxSize": "3.85KB"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {

import { ApiError, DeserializationError } from './errors';

const CUSTOM_METHOD_NAMES = ['del', 'put', 'post', 'get'];

export function shuffle<TData>(array: TData[]): TData[] {
const shuffledArray = array;

Expand Down Expand Up @@ -115,3 +117,22 @@ export function deserializeFailure(
}
return new ApiError(message, status, stackFrame);
}

export function encodePath(
method: string,
pathWithVariables: string,
args: Record<string, any>
): string {
// We don't encode `path` parameters for the custom methods
const isCustomMethod = CUSTOM_METHOD_NAMES.includes(method);
let path = pathWithVariables;

for (const [key, value] of Object.entries(args)) {
path = pathWithVariables.replace(
key,
isCustomMethod ? value : encodeURIComponent(value)
);
}

return path;
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,16 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
private Map<String, Request[]> loadCTS() throws JsonParseException, JsonMappingException, IOException, CTSException {
TreeMap<String, Request[]> cts = new TreeMap<>();
File dir = new File("tests/CTS/methods/requests/" + client);
File commonTestDir = new File("tests/CTS/methods/requests/common");
if (!dir.exists()) {
throw new CTSException("CTS not found at " + dir.getAbsolutePath(), true);
}
for (File f : dir.listFiles()) {
cts.put(f.getName().replace(".json", ""), Json.mapper().readValue(f, Request[].class));
}
for (File f : commonTestDir.listFiles()) {
cts.put(f.getName().replace(".json", ""), Json.mapper().readValue(f, Request[].class));
}
return cts;
}

Expand Down
17 changes: 11 additions & 6 deletions templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createAuth,
createTransporter,
encodePath,
getUserAgent,
shuffle,
} from '@experimental-api-clients-automation/client-common';
Expand Down Expand Up @@ -202,12 +203,16 @@ export function create{{capitalizedApiName}}Api(options: CreateClientOptions{{#h
{{/allParams.0}}
requestOptions?: RequestOptions
) : Promise<{{{returnType}}}> {
const requestPath = '{{{path}}}'{{#pathParams}}.replace(
{{=<% %>=}}
'{<%baseName%>}',
<%={{ }}=%>
encodeURIComponent(String({{paramName}}))
){{/pathParams}};
{{^pathParams.0}}
const requestPath = '{{{path}}}';
{{/pathParams.0}}
{{#pathParams.0}}
const requestPath = encodePath(
'{{{nickname}}}',
'{{{path}}}',
{ {{#pathParams}} {{=<% %>=}} '{<%baseName%>}': <%={{ }}=%>{{paramName}}, {{/pathParams}} }
);
{{/pathParams.0}}
const headers: Headers = {};
const queryParameters: QueryParameters = {};

Expand Down
35 changes: 35 additions & 0 deletions tests/CTS/methods/requests/common/del.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"method": "del",
"testName": "allow del method for a custom path with minimal parameters",
"parameters": {
"path": "/test/minimal"
},
"request": {
"path": "/1/test/minimal",
"method": "DELETE"
}
},
{
"method": "del",
"testName": "allow del method for a custom path with all parameters",
"parameters": {
"path": "/test/all",
"parameters": "&query=encoded",
"body": {
"body": "parameters"
}
},
"request": {
"path": "/1/test/all",
"method": "DELETE",
"data": {
"body": "parameters"
},
"searchParams": {
"parameters": "",
"query": "encoded"
}
}
}
]
29 changes: 29 additions & 0 deletions tests/CTS/methods/requests/common/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"method": "get",
"testName": "allow get method for a custom path with minimal parameters",
"parameters": {
"path": "/test/minimal"
},
"request": {
"path": "/1/test/minimal",
"method": "GET"
}
},
{
"method": "get",
"testName": "allow get method for a custom path with all parameters",
"parameters": {
"path": "/test/all",
"parameters": "&query=encoded"
},
"request": {
"path": "/1/test/all",
"method": "GET",
"searchParams": {
"parameters": "",
"query": "encoded"
}
}
}
]
35 changes: 35 additions & 0 deletions tests/CTS/methods/requests/common/post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"method": "post",
"testName": "allow post method for a custom path with minimal parameters",
"parameters": {
"path": "/test/minimal"
},
"request": {
"path": "/1/test/minimal",
"method": "POST"
}
},
{
"method": "post",
"testName": "allow post method for a custom path with all parameters",
"parameters": {
"path": "/test/all",
"parameters": "&query=encoded",
"body": {
"body": "parameters"
}
},
"request": {
"path": "/1/test/all",
"method": "POST",
"data": {
"body": "parameters"
},
"searchParams": {
"parameters": "",
"query": "encoded"
}
}
}
]
35 changes: 35 additions & 0 deletions tests/CTS/methods/requests/common/put.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"method": "put",
"testName": "allow put method for a custom path with minimal parameters",
"parameters": {
"path": "/test/minimal"
},
"request": {
"path": "/1/test/minimal",
"method": "PUT"
}
},
{
"method": "put",
"testName": "allow put method for a custom path with all parameters",
"parameters": {
"path": "/test/all",
"parameters": "&query=encoded",
"body": {
"body": "parameters"
}
},
"request": {
"path": "/1/test/all",
"method": "PUT",
"data": {
"body": "parameters"
},
"searchParams": {
"parameters": "",
"query": "encoded"
}
}
}
]
Loading

0 comments on commit ffcf747

Please sign in to comment.