forked from microsoft/typed-rest-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resttests.ts
356 lines (289 loc) · 14.9 KB
/
resttests.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
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import assert = require('assert');
import * as restm from 'typed-rest-client/RestClient';
import * as util from 'typed-rest-client/Util';
import * as fs from 'fs';
import * as path from 'path';
export interface HttpBinData {
url: string;
data: any;
json: any;
args?: any
}
describe('Rest Tests', function () {
let _rest: restm.RestClient;
let _restBin: restm.RestClient;
let _options: restm.IRequestOptions;
before(() => {
_rest = new restm.RestClient('typed-rest-client-tests');
_restBin = new restm.RestClient('typed-rest-client-tests', 'https://httpbin.org');
_options = {
queryParameters: {
params: {
id: 1,
type: 'compact'
}
}
}
});
after(() => {
});
it('constructs', () => {
this.timeout(1000);
let rest: restm.RestClient = new restm.RestClient('typed-test-client-tests');
assert(rest, 'rest client should not be null');
})
it('gets a resource', async() => {
this.timeout(3000);
let restRes: restm.IRestResponse<HttpBinData> = await _rest.get<HttpBinData>('https://httpbin.org/get');
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/get');
});
it('gets a resource with baseUrl', async() => {
let restRes: restm.IRestResponse<HttpBinData> = await _restBin.get<HttpBinData>('get');
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/get');
});
it('gets a resource passing Query Parameters', async() => {
this.timeout(3000);
const response: restm.IRestResponse<HttpBinData> = await _rest.get<HttpBinData>('https://httpbin.org/get', _options);
assert(response.statusCode == 200, "statusCode should be 200");
assert(response.result.url === 'https://httpbin.org/get?id=1&type=compact');
Object.keys(_options.queryParameters.params).forEach(key => {
const actual = response.result.args[key];
const expected = _options.queryParameters.params[key];
assert(expected == actual);
})
});
it('gets a resource with baseUrl passing Query Parameters', async() => {
const response: restm.IRestResponse<HttpBinData> = await _restBin.get<HttpBinData>('get', _options);
assert(response.statusCode == 200, "statusCode should be 200");
assert(response.result.url === 'https://httpbin.org/get?id=1&type=compact');
Object.keys(_options.queryParameters.params).forEach(key => {
const actual = response.result.args[key];
const expected = _options.queryParameters.params[key];
assert(expected == actual);
})
});
it('creates a resource', async() => {
let res: any = { name: 'foo' };
let restRes: restm.IRestResponse<HttpBinData> = await _rest.create<HttpBinData>('https://httpbin.org/post', res);
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/post');
assert(restRes.result && restRes.result.json.name === 'foo');
});
it('creates a resource with a baseUrl', async() => {
let res: any = { name: 'foo' };
let restRes: restm.IRestResponse<HttpBinData> = await _restBin.create<HttpBinData>('post', res);
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/post');
assert(restRes.result && restRes.result.json.name === 'foo');
});
it('replaces a resource', async() => {
this.timeout(3000);
let res: any = { name: 'foo' };
let restRes: restm.IRestResponse<HttpBinData> = await _rest.replace<HttpBinData>('https://httpbin.org/put', res);
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/put');
assert(restRes.result && restRes.result.json.name === 'foo');
});
it('replaces a resource with a baseUrl', async() => {
let res: any = { name: 'foo' };
let restRes: restm.IRestResponse<HttpBinData> = await _restBin.replace<HttpBinData>('put', res);
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/put');
assert(restRes.result && restRes.result.json.name === 'foo');
});
it('updates a resource', async() => {
let res: any = { name: 'foo' };
let restRes: restm.IRestResponse<HttpBinData> = await _rest.update<HttpBinData>('https://httpbin.org/patch', res);
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/patch');
assert(restRes.result && restRes.result.json.name === 'foo');
});
it('updates a resource with a baseUrl', async() => {
let res: any = { name: 'foo' };
let restRes: restm.IRestResponse<HttpBinData> = await _restBin.update<HttpBinData>('patch', res);
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/patch');
assert(restRes.result && restRes.result.json.name === 'foo');
});
it('deletes a resource', async() => {
let restRes: restm.IRestResponse<HttpBinData> = await _rest.del<HttpBinData>('https://httpbin.org/delete');
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/delete');
});
it('deletes a resource with a baseUrl', async() => {
let restRes: restm.IRestResponse<HttpBinData> = await _restBin.del<HttpBinData>('delete');
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/delete');
});
it('deletes a resource passing Query Parameters', async () => {
this.timeout(3000);
const response: restm.IRestResponse<HttpBinData> = await _rest.del<HttpBinData>('https://httpbin.org/delete', _options);
assert(response.statusCode == 200, "statusCode should be 200");
assert(response.result.url === 'https://httpbin.org/delete?id=1&type=compact');
Object.keys(_options.queryParameters.params).forEach(key => {
const actual = response.result.args[key];
const expected = _options.queryParameters.params[key];
assert(expected == actual);
})
});
it('deletes a resource with baseUrl passing Query Parameters', async () => {
const response: restm.IRestResponse<HttpBinData> = await _restBin.del<HttpBinData>('delete', _options);
assert(response.statusCode == 200, "statusCode should be 200");
assert(response.result.url === 'https://httpbin.org/delete?id=1&type=compact');
Object.keys(_options.queryParameters.params).forEach(key => {
const actual = response.result.args[key];
const expected = _options.queryParameters.params[key];
assert(expected == actual);
})
});
it('does an options request', async() => {
let restRes: restm.IRestResponse<HttpBinData> = await _rest.options<HttpBinData>('https://httpbin.org');
assert(restRes.statusCode == 200, "statusCode should be 200");
});
it('does an options request with baseUrl', async() => {
let restRes: restm.IRestResponse<HttpBinData> = await _restBin.options<HttpBinData>('');
assert(restRes.statusCode == 200, "statusCode should be 200");
});
//----------------------------------------------
// Get Error Cases
//----------------------------------------------
//
// Resource not found (404)
// should return a null resource, 404 status, and should not throw
//
it('gets a non-existant resource (404)', async() => {
this.timeout(3000);
try {
let restRes: restm.IRestResponse<HttpBinData> = await _rest.get<HttpBinData>('https://httpbin.org/status/404');
assert(restRes.statusCode == 404, "statusCode should be 404");
assert(restRes.result === null, "object should be null");
}
catch(err) {
assert(false, "should not throw");
}
});
//
// Unauthorized (401)
// should throw and attach statusCode to the Error object
// err.message is message proerty of resourceful error object or if not supplied, a generic error message
//
it('gets and handles unauthorized (401)', async() => {
try {
let restRes: restm.IRestResponse<HttpBinData> = await _rest.get<HttpBinData>('https://httpbin.org/status/401');
assert(false, "should throw");
}
catch(err) {
assert(err['statusCode'] == 401, "statusCode should be 401");
assert(err.message && err.message.length > 0, "should have error message");
}
});
//
// Internal Server Error
// should throw and attach statusCode to the Error object
// err.message is message proerty of resourceful error object or if not supplied, a generic error message
//
it('gets and handles a server error (500)', async() => {
try {
let restRes: restm.IRestResponse<HttpBinData> = await _rest.get<HttpBinData>('https://httpbin.org/status/500');
assert(false, "should throw");
}
catch(err) {
assert(err['statusCode'] == 500, "statusCode should be 500");
assert(err.message && err.message.length > 0, "should have error message");
}
});
//
// Error with non JSON body
// should return the text in the body if body is not JSON
//
// it('gets and handles an error with non JSON body', async() => {
// this.timeout(5000);
// try {
// let restRes: restm.IRestResponse<any> = await _rest.get('https://httpstat.us/406');
// assert(false, "should throw");
// }
// catch(err) {
// assert(err['statusCode'] == 406, "statusCode should be 406");
// assert(err.message && err.message.length > 0, "should have error message");
// assert.equal(err.message, '"406 Not Acceptable"', "error message should be '406 Not Acceptable'");
// }
// });
//--------------------------------------------------------
// Path in baseUrl tests
//--------------------------------------------------------
it('maintains the path from the base url', async() => {
this.timeout(3000);
// Arrange
let rest = new restm.RestClient('typed-rest-client-tests', 'https://httpbin.org/anything');
// Act
let restRes: restm.IRestResponse<HttpBinData> = await rest.get<HttpBinData>('anythingextra');
// Assert
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/anything/anythingextra');
});
it('maintains the path from the base url with no slashes', async() => {
// Arrange
let rest = new restm.RestClient('typed-rest-client-tests', 'https://httpbin.org/anything');
// Act
let restRes: restm.IRestResponse<HttpBinData> = await rest.get<HttpBinData>('anythingextra');
// Assert
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/anything/anythingextra');
});
it('maintains the path from the base url with double slashes', async() => {
// Arrange
let rest = new restm.RestClient('typed-rest-client-tests', 'https://httpbin.org/anything/');
// Act
let restRes: restm.IRestResponse<HttpBinData> = await rest.get<HttpBinData>('anythingextra');
// Assert
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/anything/anythingextra');
});
it('maintains the path from the base url with multiple parts', async() => {
// Arrange
let rest = new restm.RestClient('typed-rest-client-tests', 'https://httpbin.org/anything/extrapart');
// Act
let restRes: restm.IRestResponse<HttpBinData> = await rest.get<HttpBinData>('anythingextra');
// Assert
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/anything/extrapart/anythingextra');
});
it('maintains the path from the base url where request has multiple parts', async() => {
// Arrange
let rest = new restm.RestClient('typed-rest-client-tests', 'https://httpbin.org/anything');
// Act
let restRes: restm.IRestResponse<HttpBinData> = await rest.get<HttpBinData>('anythingextra/moreparts');
// Assert
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/anything/anythingextra/moreparts');
});
it('maintains the path from the base url where both have multiple parts', async() => {
// Arrange
let rest = new restm.RestClient('typed-rest-client-tests', 'https://httpbin.org/anything/multiple');
// Act
let restRes: restm.IRestResponse<HttpBinData> = await rest.get<HttpBinData>('anythingextra/moreparts');
// Assert
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/anything/multiple/anythingextra/moreparts');
});
it('maintains the path from the base url where request has query parameters', async() => {
// Arrange
this.timeout(3000);
let rest = new restm.RestClient('typed-rest-client-tests', 'https://httpbin.org/anything/multiple');
// Act
let restRes: restm.IRestResponse<HttpBinData> = await rest.get<HttpBinData>('anythingextra/moreparts?foo=bar&baz=top');
// Assert
assert(restRes.statusCode == 200, "statusCode should be 200");
assert(restRes.result && restRes.result.url === 'https://httpbin.org/anything/multiple/anythingextra/moreparts?foo=bar&baz=top');
assert(restRes.result && restRes.result.args.foo === 'bar');
assert(restRes.result && restRes.result.args.baz === 'top');
});
it('preserves trailing slashes in URLs', async () => {
const res = util.getUrl('get/foo/', 'http://httpbin.org/bar');
assert.equal(res, 'http://httpbin.org/bar/get/foo/');
});
});