From ad27abc14f43977a7e49c078176c1f472829a068 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Fri, 10 Jun 2022 10:53:31 +0200 Subject: [PATCH] test(clients): test more region param and timeouts (#663) --- package.json | 2 +- templates/javascript/browser.mustache | 10 +++-- templates/javascript/node.mustache | 10 +++-- tests/CTS/client/abtesting/parameters.json | 44 ++++++++++++++++++++++ tests/CTS/client/analytics/parameters.json | 44 ++++++++++++++++++++++ tests/CTS/client/common/commonApi.json | 22 ++++++++++- tests/CTS/client/insights/parameters.json | 44 ++++++++++++++++++++++ tests/CTS/client/recommend/api.json | 26 +++++++++++++ tests/CTS/client/search/api.json | 26 +++++++++++++ 9 files changed, 218 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 3d44bd1fd5..79e1173756 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ ], "scripts": { "build:eslint": "yarn workspace eslint-plugin-automation-custom build && yarn install", - "clean": "rm -rf **/dist **/build **/node_modules **/.gradle || true", + "clean": "rm -rf **/dist **/build **/node_modules **/.gradle **/vendor || true", "cli": "yarn workspace scripts ts-node --transpile-only ./index.ts", "docker": "docker exec -it dev yarn cli $*", "docker:build": "./scripts/docker/build.sh", diff --git a/templates/javascript/browser.mustache b/templates/javascript/browser.mustache index 305a2898bb..a7d91e13ba 100644 --- a/templates/javascript/browser.mustache +++ b/templates/javascript/browser.mustache @@ -30,15 +30,17 @@ export function {{apiName}}( throw new Error("`apiKey` is missing."); } - {{#hasRegionalHost}}{{^fallbackToAliasHost}} + {{#hasRegionalHost}} + {{^fallbackToAliasHost}} if (!region) { throw new Error("`region` is missing."); } - - if (typeof region !== 'string' || !REGIONS.includes(region)) { + {{/fallbackToAliasHost}} + + if (region && (typeof region !== 'string' || !REGIONS.includes(region))) { throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`); } - {{/fallbackToAliasHost}}{{/hasRegionalHost}} + {{/hasRegionalHost}} return create{{capitalizedApiName}}({ appId, diff --git a/templates/javascript/node.mustache b/templates/javascript/node.mustache index 6c5a4f1e55..34b628a46f 100644 --- a/templates/javascript/node.mustache +++ b/templates/javascript/node.mustache @@ -30,15 +30,17 @@ export function {{apiName}}( throw new Error("`apiKey` is missing."); } - {{#hasRegionalHost}}{{^fallbackToAliasHost}} + {{#hasRegionalHost}} + {{^fallbackToAliasHost}} if (!region) { throw new Error("`region` is missing."); } - - if (typeof region !== 'string' || !REGIONS.includes(region)) { + {{/fallbackToAliasHost}} + + if (region && (typeof region !== 'string' || !REGIONS.includes(region))) { throw new Error(`\`region\` must be one of the following: ${REGIONS.join(', ')}`); } - {{/fallbackToAliasHost}}{{/hasRegionalHost}} + {{/hasRegionalHost}} return create{{capitalizedApiName}}({ appId, diff --git a/tests/CTS/client/abtesting/parameters.json b/tests/CTS/client/abtesting/parameters.json index 7f353dddd8..75ae3dc272 100644 --- a/tests/CTS/client/abtesting/parameters.json +++ b/tests/CTS/client/abtesting/parameters.json @@ -24,5 +24,49 @@ } } ] + }, + { + "testName": "uses the correct region", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "parameters": { + "appId": "my-app-id", + "apiKey": "my-api-key", + "region": "us" + }, + "expected": {} + }, + { + "type": "method", + "object": "$client", + "path": "getABTest", + "parameters": { + "id": 123 + }, + "expected": { + "type": "host", + "match": "analytics.us.algolia.com" + } + } + ] + }, + { + "testName": "throws when incorrect region is given", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "parameters": { + "appId": "my-app-id", + "apiKey": "my-api-key", + "region": "not_a_region" + }, + "expected": { + "error": "`region` must be one of the following: de, us" + } + } + ] } ] diff --git a/tests/CTS/client/analytics/parameters.json b/tests/CTS/client/analytics/parameters.json index 899644964d..94c5862e5f 100644 --- a/tests/CTS/client/analytics/parameters.json +++ b/tests/CTS/client/analytics/parameters.json @@ -25,6 +25,50 @@ } ] }, + { + "testName": "uses the correct region", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "parameters": { + "appId": "my-app-id", + "apiKey": "my-api-key", + "region": "de" + }, + "expected": {} + }, + { + "type": "method", + "object": "$client", + "path": "post", + "parameters": { + "path": "/test" + }, + "expected": { + "type": "host", + "match": "analytics.de.algolia.com" + } + } + ] + }, + { + "testName": "throws when incorrect region is given", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "parameters": { + "appId": "my-app-id", + "apiKey": "my-api-key", + "region": "not_a_region" + }, + "expected": { + "error": "`region` must be one of the following: de, us" + } + } + ] + }, { "testName": "getAverageClickPosition throws without index", "steps": [ diff --git a/tests/CTS/client/common/commonApi.json b/tests/CTS/client/common/commonApi.json index 7ec4b77f60..2c493e379a 100644 --- a/tests/CTS/client/common/commonApi.json +++ b/tests/CTS/client/common/commonApi.json @@ -17,7 +17,27 @@ ] }, { - "testName": "calls api with correct timeouts", + "testName": "calls api with default read timeouts", + "steps": [ + { + "type": "method", + "object": "$client", + "path": "get", + "parameters": { + "path": "/test" + }, + "expected": { + "type": "timeouts", + "match": { + "connectTimeout": 2000, + "responseTimeout": 5000 + } + } + } + ] + }, + { + "testName": "calls api with default write timeouts", "steps": [ { "type": "method", diff --git a/tests/CTS/client/insights/parameters.json b/tests/CTS/client/insights/parameters.json index 4567ae276a..3551a199ee 100644 --- a/tests/CTS/client/insights/parameters.json +++ b/tests/CTS/client/insights/parameters.json @@ -24,5 +24,49 @@ } } ] + }, + { + "testName": "uses the correct region", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "parameters": { + "appId": "my-app-id", + "apiKey": "my-api-key", + "region": "us" + }, + "expected": {} + }, + { + "type": "method", + "object": "$client", + "path": "del", + "parameters": { + "path": "/test" + }, + "expected": { + "type": "host", + "match": "insights.us.algolia.io" + } + } + ] + }, + { + "testName": "throws when incorrect region is given", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "parameters": { + "appId": "my-app-id", + "apiKey": "my-api-key", + "region": "not_a_region" + }, + "expected": { + "error": "`region` must be one of the following: de, us" + } + } + ] } ] diff --git a/tests/CTS/client/recommend/api.json b/tests/CTS/client/recommend/api.json index 8b6fd7b7dc..c78ba3a1a5 100644 --- a/tests/CTS/client/recommend/api.json +++ b/tests/CTS/client/recommend/api.json @@ -24,5 +24,31 @@ } } ] + }, + { + "testName": "calls api with correct write host", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "parameters": { + "appId": "test-app-id", + "apiKey": "test-api-key" + }, + "expected": {} + }, + { + "type": "method", + "object": "$client", + "path": "post", + "parameters": { + "path": "/test" + }, + "expected": { + "type": "host", + "match": "test-app-id.algolia.net" + } + } + ] } ] diff --git a/tests/CTS/client/search/api.json b/tests/CTS/client/search/api.json index 8b6fd7b7dc..c78ba3a1a5 100644 --- a/tests/CTS/client/search/api.json +++ b/tests/CTS/client/search/api.json @@ -24,5 +24,31 @@ } } ] + }, + { + "testName": "calls api with correct write host", + "autoCreateClient": false, + "steps": [ + { + "type": "createClient", + "parameters": { + "appId": "test-app-id", + "apiKey": "test-api-key" + }, + "expected": {} + }, + { + "type": "method", + "object": "$client", + "path": "post", + "parameters": { + "path": "/test" + }, + "expected": { + "type": "host", + "match": "test-app-id.algolia.net" + } + } + ] } ]