-
-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathclient.fetch.spec.ts
190 lines (179 loc) · 6.56 KB
/
client.fetch.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import browser from './scripts/browser';
import { cleanup } from './scripts/cleanup';
import { compileWithTypescript } from './scripts/compileWithTypescript';
import { copyAsset } from './scripts/copyAsset';
import { generateClient } from './scripts/generateClient';
import server from './scripts/server';
describe('client.fetch', () => {
beforeAll(async () => {
cleanup('client/fetch');
await generateClient('client/fetch', 'v3', 'fetch', false, false, 'ApiClient');
copyAsset('index.html', 'client/fetch/index.html');
copyAsset('main.ts', 'client/fetch/main.ts');
compileWithTypescript('client/fetch');
await server.start('client/fetch');
await browser.start();
}, 30000);
afterAll(async () => {
await browser.stop();
await server.stop();
});
it('requests token', async () => {
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
const result = await browser.evaluate(async () => {
const { ApiClient } = (window as any).api;
const client = new ApiClient({
TOKEN: (window as any).tokenRequest,
USERNAME: undefined,
PASSWORD: undefined,
});
return await client.simple.getCallWithoutParametersAndResponse();
});
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
});
it('uses credentials', async () => {
const result = await browser.evaluate(async () => {
const { ApiClient } = (window as any).api;
const client = new ApiClient({
TOKEN: undefined,
USERNAME: 'username',
PASSWORD: 'password',
});
return await client.simple.getCallWithoutParametersAndResponse();
});
expect(result.headers.authorization).toBe('Basic dXNlcm5hbWU6cGFzc3dvcmQ=');
});
it('supports complex params', async () => {
const result = await browser.evaluate(async () => {
const { ApiClient } = (window as any).api;
const client = new ApiClient();
return await client.complex.complexTypes({
first: {
second: {
third: 'Hello World!',
},
},
});
});
expect(result).toBeDefined();
});
it('support form data', async () => {
const result = await browser.evaluate(async () => {
const { ApiClient } = (window as any).api;
const client = new ApiClient();
return await client.parameters.callWithParameters(
'valueHeader',
'valueQuery',
'valueForm',
'valueCookie',
'valuePath',
{
prop: 'valueBody',
}
);
});
expect(result).toBeDefined();
});
it('can abort the request', async () => {
let error;
try {
await browser.evaluate(async () => {
const { ApiClient } = (window as any).api;
const client = new ApiClient();
const promise = client.simple.getCallWithoutParametersAndResponse();
setTimeout(() => {
promise.cancel();
}, 10);
await promise;
});
} catch (e) {
error = (e as Error).message;
}
expect(error).toContain('Request aborted');
});
it('can abort the request with custom reason', async () => {
const reason = 'Timeout!';
let error;
try {
await browser.evaluate(async errMsg => {
const { ApiClient } = (window as any).api;
const client = new ApiClient();
const promise = client.simple.getCallWithoutParametersAndResponse();
setTimeout(() => {
promise.cancel(new Error(errMsg));
}, 10);
await promise;
}, reason);
} catch (e) {
error = (e as Error).message;
}
expect(error).toContain(reason);
});
it('should throw known error (500)', async () => {
const error = await browser.evaluate(async () => {
try {
const { ApiClient } = (window as any).api;
const client = new ApiClient();
await client.error.testErrorCode(500);
} catch (e) {
const error = e as any;
return JSON.stringify({
name: error.name,
message: error.message,
url: error.url,
status: error.status,
statusText: error.statusText,
body: error.body,
});
}
return;
});
expect(error).toBe(
JSON.stringify({
name: 'ApiError',
message: 'Custom message: Internal Server Error',
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
status: 500,
statusText: 'Internal Server Error',
body: {
status: 500,
message: 'hello world',
},
})
);
});
it('should throw unknown error (409)', async () => {
const error = await browser.evaluate(async () => {
try {
const { ApiClient } = (window as any).api;
const client = new ApiClient();
await client.error.testErrorCode(409);
} catch (e) {
const error = e as any;
return JSON.stringify({
name: error.name,
message: error.message,
url: error.url,
status: error.status,
statusText: error.statusText,
body: error.body,
});
}
return;
});
expect(error).toBe(
JSON.stringify({
name: 'ApiError',
message:
'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
status: 409,
statusText: 'Conflict',
body: {
status: 409,
message: 'hello world',
},
})
);
});
});