Skip to content

Commit c88d752

Browse files
authored
feat(cts): add tests for host, user agent, and timeouts (#112)
* chore: add tests for host, user agent, and timeouts * chore: rename test * chore: add more tests * fix: add host to EchoResponse * chore: rename tests * chore: fix host test and add region test * Update suite.mustache * chore: update generated tests * update tests for region * fix wrong timeouts * wip * update region tests
1 parent da0612f commit c88d752

File tree

30 files changed

+1272
-26
lines changed

30 files changed

+1272
-26
lines changed

clients/algoliasearch-client-javascript/client-common/src/requester/EchoRequester.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ export class EchoRequester extends Requester {
2929
{ headers, url, connectTimeout, responseTimeout }: EndRequest,
3030
{ data, ...originalRequest }: Request
3131
): Promise<Response> {
32-
const urlSearchParams = new URL(url).searchParams;
32+
const { host, searchParams: urlSearchParams } = new URL(url);
3333
const userAgent = urlSearchParams.get('x-algolia-agent') || undefined;
3434
const originalData =
3535
data && Object.entries(data).length > 0 ? data : undefined;
3636

3737
return Promise.resolve({
3838
content: JSON.stringify({
3939
...originalRequest,
40+
host,
4041
headers,
4142
connectTimeout,
4243
responseTimeout,

clients/algoliasearch-client-javascript/client-common/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type Response = {
4545

4646
export type EchoResponse = Request & {
4747
connectTimeout: number;
48+
host: string;
4849
headers: Record<string, string>;
4950
responseTimeout: number;
5051
searchParams?: Record<string, string>;

tests/CTS/client/abtesting/api.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[
2+
{
3+
"testName": "calls api with correct user agent",
4+
"steps": [
5+
{
6+
"type": "method",
7+
"object": "$client",
8+
"path": "addABTests",
9+
"parameters": [
10+
{
11+
"name": "test",
12+
"variant": [{ "index": "my-test-index", "trafficPercentage": 90 }],
13+
"endAt": "2022-02-01T13:37:01Z"
14+
}
15+
],
16+
"expected": {
17+
"testSubject": "actual.userAgent",
18+
"match": {
19+
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
20+
}
21+
}
22+
}
23+
]
24+
},
25+
{
26+
"testName": "calls api with correct timeouts",
27+
"steps": [
28+
{
29+
"type": "method",
30+
"object": "$client",
31+
"path": "addABTests",
32+
"parameters": [
33+
{
34+
"name": "test",
35+
"variant": [{ "index": "my-test-index", "trafficPercentage": 90 }],
36+
"endAt": "2022-02-01T13:37:01Z"
37+
}
38+
],
39+
"expected": {
40+
"match": {
41+
"objectContaining": {
42+
"connectTimeout": 2,
43+
"responseTimeout": 30
44+
}
45+
}
46+
}
47+
}
48+
]
49+
}
50+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"testName": "fallbacks to the alias when region is not given",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "my-app-id",
10+
"apiKey": "my-api-key",
11+
"region": ""
12+
},
13+
"expected": {
14+
"error": false
15+
}
16+
},
17+
{
18+
"type": "method",
19+
"object": "$client",
20+
"path": "getABTest",
21+
"parameters": [
22+
{
23+
"id": "test"
24+
}
25+
],
26+
"expected": {
27+
"match": {
28+
"objectContaining": {
29+
"host": "analytics.algolia.com"
30+
}
31+
}
32+
}
33+
}
34+
]
35+
}
36+
]

tests/CTS/client/analytics/api.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"testName": "calls api with correct user agent",
4+
"steps": [
5+
{
6+
"type": "method",
7+
"object": "$client",
8+
"path": "getAverageClickPosition",
9+
"parameters": [{ "index": "my-index" }],
10+
"expected": {
11+
"testSubject": "actual.userAgent",
12+
"match": {
13+
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
14+
}
15+
}
16+
}
17+
]
18+
},
19+
{
20+
"testName": "calls api with correct timeouts",
21+
"steps": [
22+
{
23+
"type": "method",
24+
"object": "$client",
25+
"path": "getAverageClickPosition",
26+
"parameters": [{ "index": "my-index" }],
27+
"expected": {
28+
"match": {
29+
"objectContaining": {
30+
"connectTimeout": 2,
31+
"responseTimeout": 5
32+
}
33+
}
34+
}
35+
}
36+
]
37+
}
38+
]

tests/CTS/client/analytics/basic.json renamed to tests/CTS/client/analytics/parameters.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"testName": "does not throw when region is not given",
3+
"testName": "fallbacks to the alias when region is not given",
44
"autoCreateClient": false,
55
"steps": [
66
{
@@ -13,6 +13,19 @@
1313
"expected": {
1414
"error": false
1515
}
16+
},
17+
{
18+
"type": "method",
19+
"object": "$client",
20+
"path": "getAverageClickPosition",
21+
"parameters": [{ "index": "my-index" }],
22+
"expected": {
23+
"match": {
24+
"objectContaining": {
25+
"host": "analytics.algolia.com"
26+
}
27+
}
28+
}
1629
}
1730
]
1831
},

tests/CTS/client/insights/api.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[
2+
{
3+
"testName": "calls api with correct user agent",
4+
"steps": [
5+
{
6+
"type": "method",
7+
"object": "$client",
8+
"path": "pushEvents",
9+
"parameters": [
10+
{
11+
"events": []
12+
}
13+
],
14+
"expected": {
15+
"testSubject": "actual.userAgent",
16+
"match": {
17+
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
18+
}
19+
}
20+
}
21+
]
22+
},
23+
{
24+
"testName": "calls api with correct timeouts",
25+
"steps": [
26+
{
27+
"type": "method",
28+
"object": "$client",
29+
"path": "pushEvents",
30+
"parameters": [
31+
{
32+
"events": []
33+
}
34+
],
35+
"expected": {
36+
"match": {
37+
"objectContaining": {
38+
"connectTimeout": 2,
39+
"responseTimeout": 30
40+
}
41+
}
42+
}
43+
}
44+
]
45+
}
46+
]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"testName": "fallbacks to the alias when region is not given",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "my-app-id",
10+
"apiKey": "my-api-key",
11+
"region": ""
12+
},
13+
"expected": {
14+
"error": false
15+
}
16+
},
17+
{
18+
"type": "method",
19+
"object": "$client",
20+
"path": "pushEvents",
21+
"parameters": [{ "events": [] }],
22+
"expected": {
23+
"match": {
24+
"objectContaining": {
25+
"host": "insights.algolia.io"
26+
}
27+
}
28+
}
29+
}
30+
]
31+
}
32+
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"testName": "calls api with correct user agent",
4+
"steps": [
5+
{
6+
"type": "method",
7+
"object": "$client",
8+
"path": "getPersonalizationStrategy",
9+
"parameters": [],
10+
"expected": {
11+
"testSubject": "actual.userAgent",
12+
"match": {
13+
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
14+
}
15+
}
16+
}
17+
]
18+
},
19+
{
20+
"testName": "calls api with correct timeouts",
21+
"steps": [
22+
{
23+
"type": "method",
24+
"object": "$client",
25+
"path": "getPersonalizationStrategy",
26+
"parameters": [],
27+
"expected": {
28+
"match": {
29+
"objectContaining": {
30+
"connectTimeout": 2,
31+
"responseTimeout": 5
32+
}
33+
}
34+
}
35+
}
36+
]
37+
}
38+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"testName": "throws when region is not given",
4+
"autoCreateClient": false,
5+
"steps": [
6+
{
7+
"type": "createClient",
8+
"parameters": {
9+
"appId": "my-app-id",
10+
"apiKey": "my-api-key",
11+
"region": ""
12+
},
13+
"expected": {
14+
"error": "`region` is missing."
15+
}
16+
}
17+
]
18+
},
19+
{
20+
"testName": "does not throw when region is given",
21+
"autoCreateClient": false,
22+
"steps": [
23+
{
24+
"type": "createClient",
25+
"parameters": {
26+
"appId": "my-app-id",
27+
"apiKey": "my-api-key",
28+
"region": "us"
29+
},
30+
"expected": {
31+
"error": false
32+
}
33+
}
34+
]
35+
}
36+
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"testName": "calls api with correct user agent",
4+
"steps": [
5+
{
6+
"type": "method",
7+
"object": "$client",
8+
"path": "createConfig",
9+
"parameters": [{}],
10+
"expected": {
11+
"testSubject": "actual.userAgent",
12+
"match": {
13+
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
14+
}
15+
}
16+
}
17+
]
18+
},
19+
{
20+
"testName": "calls api with correct timeouts",
21+
"steps": [
22+
{
23+
"type": "method",
24+
"object": "$client",
25+
"path": "createConfig",
26+
"parameters": [{}],
27+
"expected": {
28+
"match": {
29+
"objectContaining": {
30+
"connectTimeout": 2,
31+
"responseTimeout": 30
32+
}
33+
}
34+
}
35+
}
36+
]
37+
}
38+
]

0 commit comments

Comments
 (0)