-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclient.test.ts
More file actions
356 lines (295 loc) · 15.3 KB
/
Copy pathclient.test.ts
File metadata and controls
356 lines (295 loc) · 15.3 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import * as url from "url";
import { isRight, right } from "fp-ts/lib/Either";
import nodeFetch from "node-fetch";
import config from "../../config";
import { createClient } from "../../generated/testapi/client";
// @ts-ignore because leaked-handles doesn't ship type defintions
import * as leaked from "leaked-handles";
import { NewModel } from "../../generated/testapi/NewModel";
leaked.set({ debugSockets: true });
const { skipClient } = config;
const { generatedFilesDir, mockPort, isSpecEnabled } = config.specs.testapi;
// if there's no need for this suite in this particular run, just skip it
const describeSuite = skipClient || !isSpecEnabled ? describe.skip : describe;
// given a spied fetch call, check if the request has been made using the provided parameter in querystring
const hasQueryParam = (paramName: string) => ([input]: Parameters<
typeof fetch
>) => {
const inputUrl = typeof input === "string" ? input : input.url;
const parsedUrl = url.parse(inputUrl);
return parsedUrl.query && ~parsedUrl.query.indexOf(paramName);
};
describeSuite("Http client generated from Test API spec", () => {
it("should be a valid module", async () => {
expect(createClient).toBeDefined();
expect(createClient).toEqual(expect.any(Function));
});
it("should make a call", async () => {
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
expect(client.testAuthBearer).toEqual(expect.any(Function));
const result = await client.testAuthBearer({
bearerToken: "acb123",
qr: "acb123"
});
expect(isRight(result)).toBe(true);
});
it("should make a call, with default parameters", async () => {
const client = createClient<"bearerToken">({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: "",
withDefaults: (op: any) => (params: any) =>
op({ ...params, bearerToken: "abc123" })
});
expect(client.testAuthBearer).toEqual(expect.any(Function));
const result = await client.testAuthBearer({
qr: "acb123"
});
expect(isRight(result)).toBe(true);
});
it("should handle parameter with dashes", async () => {
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
expect(client.testParameterWithDash).toEqual(expect.any(Function));
const result = await client.testParameterWithDash({
"foo-bar": "value",
headerInlineParam: "value",
"path-param": "value",
"request-id": "value",
"x-header-param": "value"
});
expect(isRight(result)).toBe(true);
});
it("should handle both parameter with dashes and underscores", async () => {
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
expect(client.testParameterWithDashAnUnderscore).toEqual(
expect.any(Function)
);
const result = await client.testParameterWithDashAnUnderscore({
foo_bar: "value",
headerInlineParam: "value",
"path-param": "value",
"request-id": "value",
"x-header-param": "value"
});
expect(isRight(result)).toBe(true);
});
it("should not edit parameter names", async () => {
// given a spied fetch call, check if the request has been made using the provided header parameter
const hasHeaderParam = (paramName: string) => ([, init]: Parameters<
typeof fetch
>) => {
return init && init.headers && paramName in init.headers;
};
const spiedFetch = jest.fn<
ReturnType<typeof fetch>,
Parameters<typeof fetch>
>((nodeFetch as any) as typeof fetch);
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: spiedFetch,
basePath: ""
});
expect(client.testParameterWithDash).toEqual(expect.any(Function));
const result = await client.testParameterWithDash({
"foo-bar": "value",
headerInlineParam: "value",
"path-param": "value",
"request-id": "value",
"x-header-param": "value"
});
expect(spiedFetch).toHaveBeenCalledTimes(1);
// Because of a bug, we used to edit parameter names into camelCased ones, but they must be uses as they are named in the
// The following ensures we're doing the right thing now
expect(hasQueryParam("foo-bar")(spiedFetch.mock.calls[0])).toBeTruthy();
expect(hasQueryParam("fooBar")(spiedFetch.mock.calls[0])).toBeFalsy();
expect(hasQueryParam("request-id")(spiedFetch.mock.calls[0])).toBeTruthy();
expect(hasQueryParam("requestId")(spiedFetch.mock.calls[0])).toBeFalsy();
expect(
hasHeaderParam("x-header-param")(spiedFetch.mock.calls[0])
).toBeTruthy();
expect(
hasHeaderParam("xHeaderParam")(spiedFetch.mock.calls[0])
).toBeFalsy();
expect(
hasHeaderParam("headerInlineParam")(spiedFetch.mock.calls[0])
).toBeTruthy();
});
it("should strip out undefined query params", async () => {
const spiedFetch = jest.fn<
ReturnType<typeof fetch>,
Parameters<typeof fetch>
>((nodeFetch as any) as typeof fetch);
const client = createClient<"bearerToken">({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: spiedFetch,
basePath: "",
withDefaults: (op: any) => (params: any) =>
op({ ...params, bearerToken: "abc123" })
});
expect(client.testAuthBearer).toEqual(expect.any(Function));
const result = await client.testAuthBearer({
qr: "string",
qo: undefined
});
expect(spiedFetch).toHaveBeenCalledTimes(1);
expect(hasQueryParam("qr")(spiedFetch.mock.calls[0])).toBeTruthy();
expect(hasQueryParam("qo")(spiedFetch.mock.calls[0])).toBeFalsy();
});
it("should handle parameters at path level", async () => {
const spiedFetch = jest.fn<
ReturnType<typeof fetch>,
Parameters<typeof fetch>
>((nodeFetch as any) as typeof fetch);
const client = createClient<"bearerToken">({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: spiedFetch,
basePath: "",
withDefaults: (op: any) => (params: any) =>
op({ ...params, bearerToken: "abc123" })
});
expect(client.testParametersAtPathLevel).toEqual(expect.any(Function));
// It can be called with expected query parameters
const resultWithCursor = await client.testParametersAtPathLevel({
"request-id": "an id",
cursor: "a cursor"
});
// It can be called without optional parameters
const result = await client.testParametersAtPathLevel({
"request-id": "an id"
});
// It cannot be called without optional parameters
// @ts-expect-error
await client.testParametersAtPathLevel({
cursor: "a cursor"
});
});
it("should raise an exception when calling file upload from ode runtime environment", async () => {
const spiedFetch = jest.fn<
ReturnType<typeof fetch>,
Parameters<typeof fetch>
>((nodeFetch as any) as typeof fetch);
const client = createClient<"bearerToken">({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: spiedFetch,
basePath: "",
withDefaults: (op: any) => (params: any) =>
op({ ...params, bearerToken: "abc123" })
});
expect(client.testBinaryFileUpload).toEqual(expect.any(Function));
const base64File =
"iVBORw0KGgoAAAANSUhEUgAAAJQAAAB9CAYAAABEd0qeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMjHxIGmVAAAGaklEQVR4Xu3cP4gdVRzF8cVWrAQFLbQIlnYKtmI6LSIKWoiFhSCmkhQrFlrYaSo1kEptRBSEWFhZCQH/IEjARkFsxdLC7sm97In3nT135s31zYz3N6f4FO+3M3cGfl/YuNl4stvtzI5GDs1ayaFZKzk0ayWHZq3k0KyVHJq1kkOzVnJo1koOzVrJoVkrOTRrJYdmreTQrJUc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aFhY9O2gzuYqlkG470wZUclBdcZBrUcOo5gaFNwOS0UzgM+pcVCdclDLk8NoVEzJXEHhf+ODz/wcB9U5FVPioI5PDqPBQoGDYhzU5699ko1+FmeVHFQQvFgHNR85jAYLxEJri4bWoPgc4Oc4qM45qOXIYTQcFODrvHAO6lDl2QnOxXMwd1Cdc1DLkcMoxkKCWlCXT16cpHxGUgsKIoYlh1E4qOXJYRRTg4LWoPgcwHP4PRxUZxzU8uQwCge1PDmMohYUL5p/RxxBqf+SGzL2HP66g+qMg1qeHEbBQWHB+MwhwbGCAn4uOKjOOKjlyWHvOCT4vwUF/P49k8PeOaj1yGHvWoP67KVL2U/Xns1UNEPKZ5VqQeE9+f17Joe9c1DrkcMoeIHAC0ZI+Fbnb3nt5DAKtbzEQc1HDqPB4vCtjMOpBfXgo0/vURGVykhKtaD8La9TWKCDmp8cRoVwGIcEDz3x8h4OjCEcxiExfs+eyWFUKqZExZQ4qOnkMCoVzRAOagyHouJK+Dp+z57JYVQqmiEqmiEcioop4ev4PXsmh1GpaIaoaKaoBVTGlfB79kwOo1LRDFGRTIFgHFRQKhoFP15QkUzBf2jHX/FwYPyePZPDqFQ8ioNqJ4dRIZRaQKDiKF24+OoedU2CkHiOsPj9IpDDqBCMg5qPHEbHAdUWzy48/Pyec1+n0MbO5feKQA6jc1DzkcPoeLFji8fX+ZxD71NfS/i8COQwOl7soWHwOYfep76W8HkRyGF0WPTYwvm62jnq3lJ5RsLnRCKH0fGCVQQJX1c7R91bKs9I+JxI5DA6tfQhtRDKSBJ1r8LnRCKH0aklD0EwfE4ZU6LuVficSOQwOrVkhYPhczBX9w7hcyKRw+jUkpUypoTPwVzdO4TPiWN38g8PspbBu6NEtgAAAABJRU5ErkJggg==";
var blob = Buffer.from(base64File, "base64");
expect.assertions(2);
try {
await client.testBinaryFileUpload({
logo: {} as File
});
} catch (e) {
expect(e).toEqual(
Error(
"File upload is only support inside a browser runtime envoronment"
)
);
}
});
it("should handle buffer as response", async () => {
const base64File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O5feKQA6jc1DzkcPoeLFji8fX+ZxD71NfS/i8COQwOl7soWHwOYfep76W8HkRyGF0WPTYwvm62jnq3lJ5RsLnRCKH0fGCVQQJX1c7R91bKs9I+JxI5DA6tfQhtRDKSBJ1r8LnRCKH0aklD0EwfE4ZU6LuVficSOQwOrVkhYPhczBX9w7hcyKRw+jUkpUypoTPwVzdO4TPiWN38g8PspbBu6NEtgAAAABJRU5ErkJggg==";
var buffer = Buffer.from(base64File);
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
expect(client.testBinaryFileDownload).toEqual(expect.any(Function));
const result = await client.testBinaryFileDownload({});
expect(result).toMatchObject(
right({
status: 200,
value: buffer
})
);
});
it("should make patch http request", async () => {
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
expect(client.testParameterWithDash).toEqual(expect.any(Function));
const result = await client.testSimplePatch({
"foo-bar": "value",
headerInlineParam: "value",
"path-param": "value",
"request-id": "value",
"x-header-param": "value"
});
expect(isRight(result)).toBe(true);
});
it("should allow any custom header", async () => {
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
expect(client.testCustomTokenHeader).toEqual(expect.any(Function));
const result = await client.testCustomTokenHeader({
customToken: "anystring"
});
expect(isRight(result)).toBe(true);
});
it("should handle model ref model in body", async () => {
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
const aData: NewModel = {
id: "anId",
name: "aName"
};
expect(client.testParameterWithBodyReference).toEqual(expect.any(Function));
client.testParameterWithBodyReference({ body: aData });
// @ts-expect-error
client.testParameterWithBodyReference({ body: "" });
});
it("should handle model ref model in body for put operation", async () => {
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
const aData: NewModel = {
id: "anId",
name: "aName"
};
expect(client.putTestParameterWithBodyReference).toEqual(
expect.any(Function)
);
client.putTestParameterWithBodyReference({ body: aData });
// @ts-expect-error
client.putTestParameterWithBodyReference({ body: "" });
});
it("should make post with response 302 and location header", async () => {
const client = createClient({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: ""
});
expect(client.testRedirectResponseHeader).toEqual(expect.any(Function));
const result = await client.testRedirectResponseHeader({});
expect(isRight(result)).toBe(true);
});
});