This repository was archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathAbstractRestClient.test.ts
More file actions
485 lines (383 loc) · 15.5 KB
/
Copy pathAbstractRestClient.test.ts
File metadata and controls
485 lines (383 loc) · 15.5 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import * as https from "https";
import * as http from "http";
import { Session } from "../../src/session/Session";
import { RestClient } from "../../src/client/RestClient";
import { Headers } from "../../src/client/Headers";
import { ProcessUtils } from "../../../utilities";
import { MockHttpRequestResponse } from "./__model__/MockHttpRequestResponse";
import { EventEmitter } from "events";
import { ImperativeError } from "../../../error";
/**
* To test the AbstractRestClient, we use the existing default RestClient which
* extends AbstractRestClient to use as a __model__.
*/
describe("AbstractRestClient tests", () => {
it("should not append any headers to a request by default", () => {
const client = new RestClient(new Session({hostname: "test"}));
expect((client as any).appendHeaders(["Test"])).toMatchSnapshot();
expect((client as any).appendHeaders(undefined)).toMatchSnapshot();
});
it("should give an error when no session is provided", async () => {
let error;
try {
await RestClient.getExpectString(undefined, "/resource");
} catch (thrownError) {
error = thrownError;
}
expect(error.message).toMatchSnapshot();
});
it("should give an error when no resource URI is provided", async () => {
let error;
try {
await RestClient.getExpectString(new Session({hostname: "test"}), " ");
} catch (thrownError) {
error = thrownError;
}
expect(error.message).toMatchSnapshot();
});
it("should not error when chunking data and payload data are present in outgoing request", async () => {
interface IPayload {
data: string;
}
interface IResponseload {
newData: string;
}
const emitter = new MockHttpRequestResponse();
const requestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(async () => {
const newEmit = new MockHttpRequestResponse();
callback(newEmit);
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("{\"newData\":", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("\"response data\"}", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("end");
});
});
return emitter;
});
(https.request as any) = requestFnc;
const payload: IPayload = {
data: "input data",
};
const data = await RestClient.putExpectJSON<IResponseload>(new Session({
hostname: "test",
}), "/resource", [Headers.APPLICATION_JSON], payload);
expect(data).toMatchSnapshot();
});
it("should error with request rejection when status code is not in 200 range", async () => {
interface IResponseload {
newData: string;
}
const emitter = new MockHttpRequestResponse();
const requestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(async () => {
const newEmit = new MockHttpRequestResponse();
newEmit.statusCode = "400";
callback(newEmit);
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("{\"newData\":", "utf8"));
});
// missing closing bracket
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("\"response data\"}", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("end");
});
});
return emitter;
});
(https.request as any) = requestFnc;
const headers: any = [{"My-Header": "value is here"}];
const payload: any = {"my payload object": "hello"};
let error;
try {
await RestClient.putExpectJSON<IResponseload>(new Session({hostname: "test"}), "/resource", headers, payload);
} catch (thrownError) {
error = thrownError;
}
expect(error instanceof ImperativeError).toBe(true);
expect(error.message).toMatchSnapshot();
expect(error.errorCode).toMatchSnapshot();
expect(error.causeErrors).toMatchSnapshot();
for (const header of headers) {
// make sure the error contains the headers that were appended to the request
for (const key of Object.keys(header)) {
expect(error.additionalDetails).toContain(key);
expect(error.additionalDetails).toContain(header[key]);
}
}
expect(error.additionalDetails).toMatchSnapshot();
});
it("should error when chunking JSON data that does not parse", async () => {
interface IResponseload {
newData: string;
}
const emitter = new MockHttpRequestResponse();
const requestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(async () => {
const newEmit = new MockHttpRequestResponse();
callback(newEmit);
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("{\"newData\":", "utf8"));
});
// missing closing bracket
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("\"response data\"", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("end");
});
});
return emitter;
});
(https.request as any) = requestFnc;
let error;
try {
const data = await RestClient.getExpectJSON<IResponseload>(new Session({hostname: "test"}), "/resource");
} catch (thrownError) {
error = thrownError;
}
expect(error instanceof ImperativeError).toBe(true);
expect(error.message).toMatchSnapshot();
});
it("should error when chunking JSON data that does not parse and allow post payload", async () => {
interface IPayload {
data: string;
}
interface IResponseload {
newData: string;
}
const emitter = new MockHttpRequestResponse();
const requestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(async () => {
const newEmit = new MockHttpRequestResponse();
callback(newEmit);
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("{\"newData\":", "utf8"));
});
// missing closing bracket
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("\"response data\"", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("end");
});
});
return emitter;
});
(https.request as any) = requestFnc;
const payload: IPayload = {
data: "input data",
};
let error;
try {
const data = await RestClient.postExpectJSON<IResponseload>(new Session({hostname: "test"}),
"/resource", [Headers.APPLICATION_JSON], payload);
} catch (thrownError) {
error = thrownError;
}
expect(error instanceof ImperativeError).toBe(true);
expect(error.message).toMatchSnapshot();
expect(error.errorCode).toMatchSnapshot();
});
it("should not error when headers and payload data are present in outgoing request", async () => {
interface IPayload {
data: string;
}
interface IResponseload {
newData: string;
}
const emitter = new MockHttpRequestResponse();
const requestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(async () => {
const newEmit = new MockHttpRequestResponse();
callback(newEmit);
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("{\"newData\": \"response data\"}", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("end");
});
});
return emitter;
});
(https.request as any) = requestFnc;
const payload: IPayload = {
data: "input data",
};
const data = await RestClient.putExpectJSON<IResponseload>(new Session({
hostname: "test",
}), "/resource", [Headers.APPLICATION_JSON], payload);
expect(data).toMatchSnapshot();
});
it("should not error when data and end events are sent", async () => {
const emitter = new MockHttpRequestResponse();
const requestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(async () => {
const newEmit = new MockHttpRequestResponse();
callback(newEmit);
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("Sample data", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("end");
});
});
return emitter;
});
(https.request as any) = requestFnc;
const data = await RestClient.getExpectString(new Session({hostname: "test"}), "/resource");
expect(data).toMatchSnapshot();
});
// called IRL when no connectivity
it("should give an error message when error event is called", async () => {
const emitter = new MockHttpRequestResponse();
const requestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(() => {
callback(new EventEmitter());
ProcessUtils.nextTick(() => {
emitter.emit("error", "value");
});
});
return emitter;
});
(https.request as any) = requestFnc;
let error;
try {
await RestClient.getExpectString(new Session({hostname: "test"}), "/resource");
} catch (thrownError) {
error = thrownError;
}
expect(error.message).toMatchSnapshot();
});
it("should call http request for http requests", async () => {
const requestEmitter = new MockHttpRequestResponse();
const httpRequestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(() => {
callback(new EventEmitter());
ProcessUtils.nextTick(() => {
requestEmitter.emit("error", "value");
});
});
return requestEmitter;
});
const emitter = new MockHttpRequestResponse();
const httpsRequestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(() => {
callback(new EventEmitter());
ProcessUtils.nextTick(() => {
emitter.emit("error", "value");
});
});
return emitter;
});
(https.request as any) = httpsRequestFnc;
(http.request as any) = httpRequestFnc;
let error;
try {
await RestClient.getExpectString(new Session({hostname: "test", protocol: "http"}), "/resource");
} catch (thrownError) {
error = thrownError;
}
expect(httpRequestFnc).toBeCalled();
expect(httpsRequestFnc).not.toBeCalled();
});
it("should call https request for https requests", async () => {
const requestEmitter = new MockHttpRequestResponse();
const httpRequestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(() => {
callback(new EventEmitter());
ProcessUtils.nextTick(() => {
requestEmitter.emit("error", "value");
});
});
return requestEmitter;
});
const httpsRequestFnc = jest.fn((options, callback) => {
const emitter = new MockHttpRequestResponse();
ProcessUtils.nextTick(() => {
callback(new EventEmitter());
ProcessUtils.nextTick(() => {
emitter.emit("error", "value");
});
});
return emitter;
});
(https.request as any) = httpsRequestFnc;
(http.request as any) = httpRequestFnc;
let error;
try {
await RestClient.getExpectString(new Session({hostname: "test"}), "/resource");
} catch (thrownError) {
error = thrownError;
}
expect(httpsRequestFnc).toBeCalled();
expect(httpRequestFnc).not.toBeCalled();
});
it("should not error when streaming data", async () => {
interface IPayload {
data: string;
}
const fakeResponseStream: any = {
write: jest.fn(),
on: jest.fn(),
end: jest.fn()
};
const fakeRequestStream: any = {
on: jest.fn((eventName: string, callback: any) => {
// do nothing
}),
};
const emitter = new MockHttpRequestResponse();
const requestFnc = jest.fn((options, callback) => {
ProcessUtils.nextTick(async () => {
const newEmit = new MockHttpRequestResponse();
callback(newEmit);
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("{\"newData\":", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("data", Buffer.from("\"response data\"}", "utf8"));
});
await ProcessUtils.nextTick(() => {
newEmit.emit("end");
});
});
return emitter;
});
(https.request as any) = requestFnc;
await RestClient.putStreamed(new Session({
hostname: "test",
}), "/resource", [Headers.APPLICATION_JSON], fakeResponseStream, fakeRequestStream);
await RestClient.postStreamed(new Session({
hostname: "test",
}), "/resource", [Headers.APPLICATION_JSON], fakeResponseStream, fakeRequestStream);
await RestClient.putStreamedRequestOnly(new Session({
hostname: "test",
}), "/resource", [Headers.APPLICATION_JSON], fakeRequestStream);
await RestClient.postStreamedRequestOnly(new Session({
hostname: "test",
}), "/resource", [Headers.APPLICATION_JSON], fakeRequestStream);
await RestClient.getStreamed(new Session({
hostname: "test",
}), "/resource", [Headers.APPLICATION_JSON], fakeResponseStream);
await RestClient.deleteStreamed(new Session({
hostname: "test",
}), "/resource", [Headers.APPLICATION_JSON], fakeResponseStream);
});
});