Skip to content

Commit

Permalink
test(clients): test more region param and timeouts (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Jun 10, 2022
1 parent ed97787 commit ad27abc
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions templates/javascript/browser.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions templates/javascript/node.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions tests/CTS/client/abtesting/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
]
44 changes: 44 additions & 0 deletions tests/CTS/client/analytics/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
22 changes: 21 additions & 1 deletion tests/CTS/client/common/commonApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 44 additions & 0 deletions tests/CTS/client/insights/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
]
26 changes: 26 additions & 0 deletions tests/CTS/client/recommend/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
]
26 changes: 26 additions & 0 deletions tests/CTS/client/search/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
]

0 comments on commit ad27abc

Please sign in to comment.