Skip to content

Commit d99bc59

Browse files
MLoughrydamccorm
authored andcommitted
Use toUTCString() on Date objects. (#251)
Fixes #250
1 parent 5e3fa36 commit d99bc59

2 files changed

Lines changed: 37 additions & 12 deletions

File tree

api/VsoClient.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class VsoClient {
8080
if (regExExecArray[3]) {
8181
// requesting preview
8282
isPreview = true;
83-
if (regExExecArray[5]) {
83+
if (regExExecArray[5]) {
8484
// we have a resource version
8585
resourceVersion = +regExExecArray[5];
8686
}
@@ -119,7 +119,7 @@ export class VsoClient {
119119

120120
return this.beginGetLocation(area, locationId)
121121
.then((location: ifm.ApiResourceLocation): ClientVersioningData => {
122-
if (!location) {
122+
if (!location) {
123123
throw new Error("Failed to find api location for area: " + area + " id: " + locationId);
124124
}
125125

@@ -132,7 +132,7 @@ export class VsoClient {
132132
};
133133
});
134134
}
135-
135+
136136
/**
137137
* Sets a promise that is waited on before any requests are issued. Can be used to asynchronously
138138
* set the request url and auth token manager.
@@ -142,10 +142,10 @@ export class VsoClient {
142142
this._initializationPromise = promise;
143143
}
144144
}
145-
145+
146146
/**
147147
* Gets information about an API resource location (route template, supported versions, etc.)
148-
*
148+
*
149149
* @param area resource area name
150150
* @param locationId Guid of the location to get
151151
*/
@@ -162,7 +162,7 @@ export class VsoClient {
162162
if (!areaLocationsPromise) {
163163
let requestUrl = this.resolveUrl(VsoClient.APIS_RELATIVE_PATH + "/" + area);
164164
areaLocationsPromise = this.restClient.options<any>(requestUrl)
165-
.then((res:restm.IRestResponse<any>) => {
165+
.then((res:restm.IRestResponse<any>) => {
166166
let locationsLookup: VssApiResourceLocationLookup = {};
167167
let resourceLocations: ifm.ApiResourceLocation[] = res.result.value;
168168
let i;
@@ -201,8 +201,12 @@ export class VsoClient {
201201
}
202202

203203
if(queryString === '' && prefix.length > 0){
204+
// Date.prototype.toString() returns a string that is not valid for the REST API.
205+
// Need to specially call `toUTCString()` instead for such cases
206+
const queryValue = typeof queryParams === 'object' && 'toUTCString' in queryParams ? (queryParams as Date).toUTCString() : queryParams.toString();
207+
204208
// Will always need to chop period off of end of prefix
205-
queryString = prefix.slice(0,-1) + '=' + encodeURIComponent(queryParams.toString()) + '&';
209+
queryString = prefix.slice(0,-1) + '=' + encodeURIComponent(queryValue) + '&';
206210
}
207211
return queryString;
208212
}

test/units/tests.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('VSOClient Units', function () {
1111
let rest: rm.RestClient;
1212
let vsoClient: vsom.VsoClient
1313
const baseUrl: string = 'https://dev.azure.com/';
14-
14+
1515
before(() => {
1616
const userAgent: string = "testAgent";
1717
rest = new rm.RestClient(userAgent, null, []);
@@ -162,6 +162,27 @@ describe('VSOClient Units', function () {
162162
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate?status.innerstatus=2&version=1&nestedObject.nestedField=value&nestedObject.innerNestedObject.key=val2');
163163
});
164164

165+
it('gets versioning datafor dates', async () => {
166+
//Arrange
167+
nock('https://dev.azure.com/_apis/testArea5', {
168+
reqheaders: {
169+
'accept': 'application/json',
170+
'user-agent': 'testAgent'
171+
}})
172+
.options('')
173+
.reply(200, {
174+
value: [{id: 'testLocation', maxVersion: '1', releasedVersion: '1', routeTemplate: 'testTemplate', area: 'testArea5', resourceName: 'testName', resourceVersion: '1'}]
175+
});
176+
177+
//Act
178+
const queryParams = {min: new Date(Date.UTC(208, 9, 19))};
179+
const res: vsom.ClientVersioningData = await vsoClient.getVersioningData('1', 'testArea5', 'testLocation', {'testKey': 'testValue'}, queryParams);
180+
181+
//Assert
182+
assert.equal(res.apiVersion, '1');
183+
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate?min=Wed%2C%2019%20Oct%200208%2000%3A00%3A00%20GMT');
184+
});
185+
165186
it('gets versioning data after an initialization promise', async () => {
166187
//Arrange
167188
nock('https://newbase.com/_apis/testArea6', {
@@ -207,7 +228,7 @@ describe('VSOClient Units', function () {
207228
});
208229

209230
describe('WebApi Units', function () {
210-
const osName: string = os.platform();
231+
const osName: string = os.platform();
211232
const osVersion: string = os.release();
212233
const nodeApiName: string = 'azure-devops-node-api';
213234
const nodeApiVersion: string = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
@@ -245,7 +266,7 @@ describe('WebApi Units', function () {
245266
// Assert
246267
assert.equal(res.apiVersion, '1');
247268
assert.equal(res.requestUrl, 'https://dev.azure.com/testTemplate');
248-
269+
249270
});
250271

251272
it('connects to the server with the correct user agent when request settings are not specified', async () => {
@@ -273,8 +294,8 @@ describe('WebApi Units', function () {
273294
it('supports no_proxy environment variable', async() => {
274295
const myWebApi: WebApi.WebApi = new WebApi.WebApi('https://dev.azure.com/', WebApi.getBasicHandler('user', 'password'), null);
275296
process.env.no_proxy='dev.azure.com,my-tfs-instance.host'
276-
assert.equal(myWebApi.isNoProxyHost('https://dev.azure.com/myproject'), true);
277-
assert.equal(myWebApi.isNoProxyHost('https://my-tfs-instance.host/myproject'), true);
297+
assert.equal(myWebApi.isNoProxyHost('https://dev.azure.com/myproject'), true);
298+
assert.equal(myWebApi.isNoProxyHost('https://my-tfs-instance.host/myproject'), true);
278299
assert.equal(myWebApi.isNoProxyHost('https://my-other-tfs-instance.host/myproject'), false);
279300
});
280301
});

0 commit comments

Comments
 (0)