-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathtests.ts
More file actions
612 lines (525 loc) · 27.4 KB
/
Copy pathtests.ts
File metadata and controls
612 lines (525 loc) · 27.4 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import assert = require('assert');
import fs = require('fs');
import nock = require('nock');
import os = require('os');
import vsom = require('../../_build/VsoClient');
import WebApi = require('../../_build/WebApi');
import * as rm from 'typed-rest-client/RestClient';
import { ApiResourceLocation, RateLimit } from '../../_build/interfaces/common/VsoBaseInterfaces';
import semver = require('semver');
describe('VSOClient Units', function () {
let rest: rm.RestClient;
let vsoClient: vsom.VsoClient
const baseUrl: string = 'https://dev.azure.com/';
before(() => {
const userAgent: string = "testAgent";
rest = new rm.RestClient(userAgent, null, []);
vsoClient = new vsom.VsoClient(baseUrl, rest);
});
after(() => {
});
afterEach(() => {
nock.cleanAll();
vsoClient = new vsom.VsoClient(baseUrl, rest);
});
it('constructs', () => {
//Arrange
this.timeout(1000);
const userAgent: string = "testAgent";
const rest: rm.RestClient = new rm.RestClient(userAgent, null, []);
//Act
const vso: vsom.VsoClient = new vsom.VsoClient('https://microsoft.com', rest);
//Assert
assert(vso, 'VsoClient should not be null');
});
it('gets versioning data where the max version and released version are identical', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea', 'testLocation', {'testKey': 'testValue'}, null);
//Assert
assert(res.apiVersion === '1');
assert(res.requestUrl === 'https://dev.azure.com/testTemplate');
});
it('gets versioning data where ther requested version is greater than the max version', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea2', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '2', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('3', 'testArea2', 'testLocation', {'testKey': 'testValue'}, null);
//Assert
assert(res.apiVersion === '2-preview.1');
assert(res.requestUrl === 'https://dev.azure.com/testTemplate');
});
it('gets versioning data where ther requested version is less than the max version', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea3', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '2', releasedVersion: '2', routeTemplate: 'testTemplate', area: 'testArea', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea3', 'testLocation', {'testKey': 'testValue'}, null);
//Assert
assert(res.apiVersion === '1');
assert(res.requestUrl === 'https://dev.azure.com/testTemplate');
});
it('gets versioning data with simple query params', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea4', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea4', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const queryParams = {status: 2};
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea4', 'testLocation', {'testKey': 'testValue'}, queryParams);
//Assert
assert(res.apiVersion === '1');
assert(res.requestUrl === 'https://dev.azure.com/testTemplate?status=2');
});
it('gets versioning data with nested query params', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea5', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea5', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const queryParams = {status: {innerstatus: 2}};
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea5', 'testLocation', {'testKey': 'testValue'}, queryParams);
//Assert
assert(res.apiVersion === '1');
assert(res.requestUrl === 'https://dev.azure.com/testTemplate?status.innerstatus=2');
});
it('gets versioning data with complex nested query params', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea5', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea5', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const queryParams = {status: {innerstatus: 2}, version: '1', nestedObject: {nestedField: 'value', innerNestedObject: {key: 'val2'}}};
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea5', 'testLocation', {'testKey': 'testValue'}, queryParams);
//Assert
assert.equal(res.apiVersion, '1');
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate?status.innerstatus=2&version=1&nestedObject.nestedField=value&nestedObject.innerNestedObject.key=val2');
});
it('gets versioning data with array-typed query params', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea5', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea5', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const queryParams = {states: ["active", "inactive"]};
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea5', 'testLocation', {'testKey': 'testValue'}, queryParams);
//Assert
assert.equal(res.apiVersion, '1');
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate?states=active%2Cinactive');
});
it('gets versioning data with mixed query parameter types', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea9', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea9', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const queryParams = {
// Simple parameter
status: 'completed',
// Nested parameter
filter: { type: 'build' },
// Complex nested parameter
options: {
sorting: { field: 'date', order: 'desc' },
pagination: { limit: 50 }
},
// Array parameter
tags: ['important', 'release', 'verified'],
// Another array parameter
assignedTo: ['user1', 'user2']
};
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea9', 'testLocation', {'testKey': 'testValue'}, queryParams);
//Assert
assert.equal(res.apiVersion, '1');
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate?status=completed&filter.type=build&options.sorting.field=date&options.sorting.order=desc&options.pagination.limit=50&tags=important%2Crelease%2Cverified&assignedTo=user1%2Cuser2');
});
it('gets versioning datafor dates', async () => {
//Arrange
nock('https://dev.azure.com/_apis/testArea5', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea5', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const queryParams = {min: new Date(Date.UTC(208, 9, 19))};
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea5', 'testLocation', {'testKey': 'testValue'}, queryParams);
//Assert
assert.equal(res.apiVersion, '1');
//Use different strings for Node 6 and 8, because of a varied response string
var expectedURL = semver.lt(process.versions.node, '8.0.0') ? 'https://dev.azure.com/testTemplate?min=Wed%2C%2019%20Oct%20%20208%2000%3A00%3A00%20GMT' : 'https://dev.azure.com/testTemplate?min=Wed%2C%2019%20Oct%200208%2000%3A00%3A00%20GMT';
assert.equal(res.requestUrl, expectedURL);
});
it('gets versioning data after an initialization promise', async () => {
//Arrange
nock('https://newbase.com/_apis/testArea6', {
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea6', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
vsoClient._setInitializationPromise(new Promise(resolve => {
vsoClient.baseUrl = 'https://newbase.com';
resolve();
}));
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea6', 'testLocation', {'testKey': 'testValue'}, null);
//Assert
assert(res.apiVersion === '1');
assert(res.requestUrl === 'https://newbase.com/testTemplate');
});
it('gets the location', async () => {
nock('https://dev.azure.com/_apis/testArea7', {
//Arrange
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea7', resourceName: 'testName', resourceVersion: '1'}]
});
//Act
const res: ApiResourceLocation = await vsoClient.beginGetLocation('testArea7', 'testLocation');
//Assert
assert(res.id === "testLocation");
});
it('Returns \'undefined\' when the location is not found', async () => {
nock('https://dev.azure.com/_apis/testArea8', {
//Arrange
reqheaders: {
'accept': 'application/json',
'user-agent': 'testAgent'
}})
.options('')
.reply(404, 'Not Found"');
//Act
const res = await vsoClient.beginGetLocation('testArea8', 'testLocation');
//Assert
assert(res === undefined);
})
});
describe('WebApi Units', function () {
const osName: string = os.platform();
const osVersion: string = os.release();
const nodeApiName: string = 'azure-devops-node-api';
const nodeApiVersion: string = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
it('sets the user agent correctly when request settings are specified', async () => {
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://microsoft.com', WebApi.getBasicHandler('user', 'password'),
undefined, {productName: 'name', productVersion: '1.2.3'});
const userAgent: string = `name/1.2.3 (${nodeApiName} ${nodeApiVersion}; ${osName} ${osVersion})`;
assert.equal(userAgent, myWebApi.rest.client.userAgent, 'User agent should be: ' + userAgent);
});
it('AdoRestClient attaches rate limit headers to responses', async () => {
// Arrange
const userAgent: string = `${nodeApiName}/${nodeApiVersion} (${osName} ${osVersion})`;
nock('https://dev.azure.com', {
reqheaders: {
'accept': 'application/json',
'user-agent': userAgent,
},
})
.defaultReplyHeaders({
'x-ratelimit-resource': 'core',
'x-ratelimit-delay': '0.5',
'x-ratelimit-limit': '1000',
'x-ratelimit-remaining': '999',
'x-ratelimit-reset': '1625078400',
'retry-after': '1',
})
.get('/test')
.reply(200, { success: true });
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com', WebApi.getBasicHandler('user', 'password'));
// Act
const response = await myWebApi.rest.get<{ success: boolean, rateLimit?: RateLimit }>('https://dev.azure.com/test');
// Assert
assert(response.result?.success === true, 'Response body should indicate success');
assert(response.result?.rateLimit, 'Response should have rateLimit property');
assert.equal(response.result.rateLimit?.resource, 'core', 'Rate limit resource should be "core"');
assert.equal(response.result.rateLimit?.delay, 0.5, 'Rate limit delay should be 0.5');
assert.equal(response.result.rateLimit?.limit, 1000, 'Rate limit limit should be 1000');
assert.equal(response.result.rateLimit?.remaining, 999, 'Rate limit remaining should be 999');
assert.equal(response.result.rateLimit?.reset, 1625078400, 'Rate limit reset should be 1625078400');
assert.equal(response.result.rateLimit?.retryAfter, 1, 'Rate limit retryAfter should be 1');
});
it('AdoRestClient attaches rate limit headers to error on 429', async () => {
// Arrange
const userAgent: string = `${nodeApiName}/${nodeApiVersion} (${osName} ${osVersion})`;
nock('https://dev.azure.com', {
reqheaders: {
'accept': 'application/json',
'user-agent': userAgent,
},
})
.defaultReplyHeaders({
'x-ratelimit-resource': 'core',
'x-ratelimit-delay': '5.0',
'x-ratelimit-limit': '1000',
'x-ratelimit-remaining': '0',
'x-ratelimit-reset': '1625078400',
'retry-after': '30',
})
.get('/blocked')
.reply(429, { message: 'Too Many Requests' });
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com', WebApi.getBasicHandler('user', 'password'));
// Act & Assert
try {
await myWebApi.rest.get<any>('https://dev.azure.com/blocked');
assert.fail('Should have thrown on 429');
} catch (err: any) {
assert(err.rateLimit, 'Error should have rateLimit property');
assert.equal(err.rateLimit.resource, 'core', 'Rate limit resource should be "core"');
assert.equal(err.rateLimit.remaining, 0, 'Rate limit remaining should be 0');
assert.equal(err.rateLimit.retryAfter, 30, 'Rate limit retryAfter should be 30');
}
});
it('AdoRestClient does not attach rateLimit when no rate limit headers present', async () => {
// Arrange
const userAgent: string = `${nodeApiName}/${nodeApiVersion} (${osName} ${osVersion})`;
nock('https://dev.azure.com', {
reqheaders: {
'accept': 'application/json',
'user-agent': userAgent,
},
})
.get('/no-ratelimit')
.reply(200, { data: 'hello' });
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com', WebApi.getBasicHandler('user', 'password'));
// Act
const response = await myWebApi.rest.get<{ data: string, rateLimit?: RateLimit }>('https://dev.azure.com/no-ratelimit');
// Assert
assert(response.result?.data === 'hello', 'Response body should have data');
assert.equal(response.result?.rateLimit, undefined, 'rateLimit should not be present when no rate limit headers');
});
it('sets the user agent correctly when request settings are not specified', async () => {
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://microsoft.com', WebApi.getBasicHandler('user', 'password'), undefined);
const userAgent: string = `${nodeApiName}/${nodeApiVersion} (${osName} ${osVersion})`;
assert.equal(userAgent, myWebApi.rest.client.userAgent, 'User agent should be: ' + userAgent);
});
it('connects to the server with the correct user agent when request settings are specified', async () => {
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com/', WebApi.getBasicHandler('user', 'password'),
undefined, {productName: 'name', productVersion: '1.2.3'});
const userAgent: string = `name/1.2.3 (${nodeApiName} ${nodeApiVersion}; ${osName} ${osVersion})`;
nock('https://dev.azure.com/_apis/testArea', {
reqheaders: {
'accept': 'application/json',
'user-agent': userAgent
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea', resourceName: 'testName', resourceVersion: '1'}]
});
// Act
const res: vsom.ClientVersioningData = await myWebApi.vsoClient.getVersioningData('1', 'testArea', 'testLocation', {'testKey': 'testValue'}, null);
// Assert
assert.equal(res.apiVersion, '1');
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate');
});
it('connects to the server with the correct user agent when request settings are not specified', async () => {
// Arrange
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com/', WebApi.getBasicHandler('user', 'password'), null);
const userAgent: string = `${nodeApiName}/${nodeApiVersion} (${osName} ${osVersion})`;
nock('https://dev.azure.com/_apis/testArea', {
reqheaders: {
'accept': 'application/json',
'user-agent': userAgent
}})
.options('')
.reply(200, {
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea', resourceName: 'testName', resourceVersion: '1'}]
});
// Act
const res: vsom.ClientVersioningData = await myWebApi.vsoClient.getVersioningData('1', 'testArea', 'testLocation', {'testKey': 'testValue'}, null);
// Assert
assert.equal(res.apiVersion, '1');
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate');
});
it('supports no_proxy environment variable', async() => {
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com/', WebApi.getBasicHandler('user', 'password'), null);
process.env.no_proxy='dev.azure.com,my-tfs-instance.host'
assert.equal(myWebApi.isNoProxyHost('https://dev.azure.com/myproject'), true);
assert.equal(myWebApi.isNoProxyHost('https://my-tfs-instance.host/myproject'), true);
assert.equal(myWebApi.isNoProxyHost('https://my-other-tfs-instance.host/myproject'), false);
});
describe('getServiceEndpointApi', function () {
const baseUrl: string = 'https://dev.azure.com/';
const serviceEndpointResourceAreaId: string = '1814ab31-2f4f-4a9f-8761-f4d77dc5a5d7';
const resourceAreasLocationId: string = 'e81700f7-3be2-46de-8624-2eb35882fcaa';
afterEach(() => {
nock.cleanAll();
});
// Mocks the Location-area OPTIONS discovery call so the resourceAreas GET routes to `_apis/resourceAreas`.
function mockLocationOptions(server: string): void {
nock(server + '_apis/Location')
.options('')
.reply(200, {
value: [{
id: resourceAreasLocationId,
maxVersion: '7.2',
releasedVersion: '7.2',
routeTemplate: '_apis/resourceAreas',
area: 'Location',
resourceName: 'ResourceAreas',
resourceVersion: '1'
}]
});
}
it('returns a ServiceEndpointApi using the resolved resource area URL', async () => {
// Arrange
const resolvedUrl: string = 'https://serviceendpoint.dev.azure.com/';
mockLocationOptions(baseUrl);
nock(baseUrl)
.get('/_apis/resourceAreas')
.reply(200, {
value: [{
id: serviceEndpointResourceAreaId,
name: 'serviceendpoint',
locationUrl: resolvedUrl
}]
});
const myWebApi: WebApi.WebApi = new WebApi.WebApi(baseUrl, WebApi.getBasicHandler('user', 'password'));
// Act
const serviceEndpointApi = await myWebApi.getServiceEndpointApi();
// Assert
assert(serviceEndpointApi, 'ServiceEndpointApi should be created');
assert.equal(serviceEndpointApi.baseUrl, resolvedUrl, 'baseUrl should match the resolved resource area locationUrl');
});
it('falls back to the server URL when resource areas are empty (on-prem)', async () => {
// Arrange
const onPremUrl: string = 'https://my-tfs-instance.host/';
mockLocationOptions(onPremUrl);
nock(onPremUrl)
.get('/_apis/resourceAreas')
.reply(200, { count: 0, value: null });
const myWebApi: WebApi.WebApi = new WebApi.WebApi(onPremUrl, WebApi.getBasicHandler('user', 'password'));
// Act
const serviceEndpointApi = await myWebApi.getServiceEndpointApi();
// Assert
assert(serviceEndpointApi, 'ServiceEndpointApi should be created');
assert.equal(serviceEndpointApi.baseUrl, onPremUrl, 'baseUrl should fall back to the server URL on-prem');
});
it('uses provided handlers instead of the default auth handler', async () => {
// Arrange
const resolvedUrl: string = 'https://serviceendpoint.dev.azure.com/';
mockLocationOptions(baseUrl);
nock(baseUrl)
.get('/_apis/resourceAreas')
.reply(200, {
value: [{
id: serviceEndpointResourceAreaId,
name: 'serviceendpoint',
locationUrl: resolvedUrl
}]
});
const customHandler = WebApi.getBearerHandler('custom-token');
const myWebApi: WebApi.WebApi = new WebApi.WebApi(baseUrl, WebApi.getBasicHandler('user', 'password'));
// Act
const serviceEndpointApi = await myWebApi.getServiceEndpointApi(undefined, [customHandler]);
// Assert
assert(serviceEndpointApi, 'ServiceEndpointApi should be created when custom handlers are provided');
assert.equal(serviceEndpointApi.baseUrl, resolvedUrl, 'baseUrl should still resolve from resource areas');
});
it('uses the provided serverUrl as the fallback baseUrl when resource areas are empty', async () => {
// Arrange
const defaultUrl: string = baseUrl;
const customUrl: string = 'https://custom-tfs.contoso.com/';
// Resource area discovery happens against the WebApi's default serverUrl (via its own LocationsApi),
// but the custom serverUrl is used as the fallback when no resource areas are returned.
mockLocationOptions(defaultUrl);
nock(defaultUrl)
.get('/_apis/resourceAreas')
.reply(200, { count: 0, value: null });
const myWebApi: WebApi.WebApi = new WebApi.WebApi(defaultUrl, WebApi.getBasicHandler('user', 'password'));
// Act
const serviceEndpointApi = await myWebApi.getServiceEndpointApi(customUrl);
// Assert
assert.equal(serviceEndpointApi.baseUrl, customUrl, 'baseUrl should use the custom serverUrl fallback, not the WebApi default');
});
});
});
describe('Auth Handlers Units', function () {
it('cross origin authenthication should be allowed by default in BasicCredentialHandler', () => {
const basicAuthHandler = WebApi.getBasicHandler('user', 'password');
assert(basicAuthHandler['allowCrossOriginAuthentication'] === true);
});
it('cross origin authenthication should be allowed by default in BearerCredentialHandler', () => {
const bearerAuthHandler = WebApi.getBearerHandler('0000000000000000000000000000000000000000');
assert(bearerAuthHandler['allowCrossOriginAuthentication'] === true);
});
it('cross origin authenthication should be allowed by default in PersonalAccessTokenCredentialHandler', () => {
const personalAccessTokenAuthHandler = WebApi.getPersonalAccessTokenHandler('0000000000000000000000000000000000000000');
assert(personalAccessTokenAuthHandler['allowCrossOriginAuthentication'] === true);
});
it('cross origin authenthication could be disabled in BasicCredentialHandler', () => {
const basicAuthHandler = WebApi.getBasicHandler('user', 'password', false);
assert(basicAuthHandler['allowCrossOriginAuthentication'] === false);
});
it('cross origin authenthication could be disabled in BearerCredentialHandler', () => {
const bearerAuthHandler = WebApi.getBearerHandler('0000000000000000000000000000000000000000', false);
assert(bearerAuthHandler['allowCrossOriginAuthentication'] === false);
});
it('cross origin authenthication could be disabled in PersonalAccessTokenCredentialHandler', () => {
const personalAccessTokenAuthHandler = WebApi.getPersonalAccessTokenHandler('0000000000000000000000000000000000000000', false);
assert(personalAccessTokenAuthHandler['allowCrossOriginAuthentication'] === false);
});
});