Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cts): add tests for host, user agent, and timeouts #112

Merged
merged 20 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ export class EchoRequester extends Requester {
{ headers, url, connectTimeout, responseTimeout }: EndRequest,
{ data, ...originalRequest }: Request
): Promise<Response> {
const urlSearchParams = new URL(url).searchParams;
const { host, searchParams: urlSearchParams } = new URL(url);
const userAgent = urlSearchParams.get('x-algolia-agent') || undefined;
const originalData =
data && Object.entries(data).length > 0 ? data : undefined;

return Promise.resolve({
content: JSON.stringify({
...originalRequest,
host,
headers,
connectTimeout,
responseTimeout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type Response = {

export type EchoResponse = Request & {
connectTimeout: number;
host: string;
headers: Record<string, string>;
responseTimeout: number;
searchParams?: Record<string, string>;
Expand Down
50 changes: 50 additions & 0 deletions tests/CTS/client/abtesting/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"testName": "calls api with correct user agent",
"steps": [
{
"type": "method",
"object": "$client",
"path": "addABTests",
"parameters": [
{
"name": "test",
"variant": [{ "index": "my-test-index", "trafficPercentage": 90 }],
"endAt": "2022-02-01T13:37:01Z"
}
],
"expected": {
"testSubject": "actual.userAgent",
"match": {
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
}
}
}
]
},
{
"testName": "calls api with correct timeouts",
"steps": [
{
"type": "method",
"object": "$client",
"path": "addABTests",
"parameters": [
{
"name": "test",
"variant": [{ "index": "my-test-index", "trafficPercentage": 90 }],
"endAt": "2022-02-01T13:37:01Z"
}
],
"expected": {
"match": {
"objectContaining": {
"connectTimeout": 1,
"responseTimeout": 30
}
}
}
}
]
}
]
36 changes: 36 additions & 0 deletions tests/CTS/client/abtesting/parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"testName": "does not throw when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": false
}
}
]
},
{
"testName": "does not throw when region is given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": "us"
},
"expected": {
"error": false
}
}
]
}
]
38 changes: 38 additions & 0 deletions tests/CTS/client/analytics/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"testName": "calls api with correct user agent",
"steps": [
{
"type": "method",
"object": "$client",
"path": "getAverageClickPosition",
"parameters": [{ "index": "my-index" }],
"expected": {
"testSubject": "actual.userAgent",
"match": {
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
}
}
}
]
},
{
"testName": "calls api with correct timeouts",
"steps": [
{
"type": "method",
"object": "$client",
"path": "getAverageClickPosition",
"parameters": [{ "index": "my-index" }],
"expected": {
"match": {
"objectContaining": {
"connectTimeout": 1,
"responseTimeout": 2
}
}
}
}
]
}
]
46 changes: 46 additions & 0 deletions tests/CTS/client/insights/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"testName": "calls api with correct user agent",
"steps": [
{
"type": "method",
"object": "$client",
"path": "pushEvents",
"parameters": [
{
"events": []
}
],
"expected": {
"testSubject": "actual.userAgent",
"match": {
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
}
}
}
]
},
{
"testName": "calls api with correct timeouts",
"steps": [
{
"type": "method",
"object": "$client",
"path": "pushEvents",
"parameters": [
{
"events": []
}
],
"expected": {
"match": {
"objectContaining": {
"connectTimeout": 1,
"responseTimeout": 30
}
}
}
}
]
}
]
38 changes: 38 additions & 0 deletions tests/CTS/client/personalization/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"testName": "calls api with correct user agent",
"steps": [
{
"type": "method",
"object": "$client",
"path": "getPersonalizationStrategy",
"parameters": [],
"expected": {
"testSubject": "actual.userAgent",
"match": {
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
}
}
}
]
},
{
"testName": "calls api with correct timeouts",
"steps": [
{
"type": "method",
"object": "$client",
"path": "getPersonalizationStrategy",
"parameters": [],
"expected": {
"match": {
"objectContaining": {
"connectTimeout": 1,
"responseTimeout": 2
}
}
}
}
]
}
]
36 changes: 36 additions & 0 deletions tests/CTS/client/personalization/parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is missing."
}
}
]
},
{
"testName": "does not throw when region is given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": "us"
},
"expected": {
"error": false
}
}
]
}
]
38 changes: 38 additions & 0 deletions tests/CTS/client/query-suggestions/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"testName": "calls api with correct user agent",
"steps": [
{
"type": "method",
"object": "$client",
"path": "createConfig",
"parameters": [{}],
"expected": {
"testSubject": "actual.userAgent",
"match": {
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
}
}
}
]
},
{
"testName": "calls api with correct timeouts",
"steps": [
{
"type": "method",
"object": "$client",
"path": "createConfig",
"parameters": [{}],
"expected": {
"match": {
"objectContaining": {
"connectTimeout": 1,
"responseTimeout": 30
}
}
}
}
]
}
]
36 changes: 36 additions & 0 deletions tests/CTS/client/query-suggestions/parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"testName": "throws when region is not given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": ""
},
"expected": {
"error": "`region` is missing."
}
}
]
},
{
"testName": "does not throw when region is given",
"autoCreateClient": false,
"steps": [
{
"type": "createClient",
"parameters": {
"appId": "my-app-id",
"apiKey": "my-api-key",
"region": "us"
},
"expected": {
"error": false
}
}
]
}
]
38 changes: 38 additions & 0 deletions tests/CTS/client/recommend/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"testName": "calls api with correct user agent",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not required for this PR) I don't know if it's the same for other clients, but we also send the client name in the UA in JS.

cc @millotp @damcou do you send it in PHP/Java?

We could maybe test Algolia%20for.*clientName or something similar

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me

"steps": [
{
"type": "method",
"object": "$client",
"path": "getRecommendations",
"parameters": [{ "requests": [] }],
"expected": {
"testSubject": "actual.userAgent",
"match": {
"regexp": "/Algolia%20for%20(.+)%20\\(\\d+\\.\\d+\\.\\d+\\)/"
}
}
}
]
},
{
"testName": "calls api with correct timeouts",
"steps": [
{
"type": "method",
"object": "$client",
"path": "getRecommendations",
"parameters": [{ "requests": [] }],
"expected": {
"match": {
"objectContaining": {
"connectTimeout": 1,
"responseTimeout": 30
}
}
}
}
]
}
]
Loading