Skip to content

Commit

Permalink
UnixTime in node.js (Azure#987)
Browse files Browse the repository at this point in the history
* Enhancement to x-ms-parameterized-host extension

* positionInOperation defaults to first

* fixed setting default value for a complex object containing constant properties.

* result of executing gulp regenerate:expected

* Fixed fxcop warnings

* remove null check validation for unixtime as they are treated as long in java

* unixTime in node.js

* modification

* UnixTime tests

* FxCop warnings
  • Loading branch information
amarzavery committed Apr 29, 2016
1 parent 651b102 commit 37a5369
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,29 @@ describe('nodejs', function () {
});
});
});

it('should put and get UnixTime date correctly', function (done) {
var d = new Date('2016-04-13T00:00:00.000Z');
testClient.intModel.putUnixTimeDate(d, function (error, result) {
should.not.exist(error);
testClient.intModel.getUnixTime(function (error, result) {
should.not.exist(error);
assert.deepEqual(result, d);
done();
});
});
});

it('should throw an error for invalid UnixTime date anf get null value for UnixTime', function (done) {
testClient.intModel.getInvalidUnixTime(function (error, result) {
should.exist(error);
testClient.intModel.getNullUnixTime(function (error, result) {
should.not.exist(error);
should.not.exist(result);
done();
});
});
});
});

describe('CompositeBoolInt Client', function () {
Expand Down Expand Up @@ -1891,6 +1914,13 @@ describe('nodejs', function () {
});
});

it('should work when path has a paramaeter in UnixTime format', function (done) {
testClient.paths.unixTimeUrl(new Date('2016-04-13T00:00:00.000Z'), function (error, result) {
should.not.exist(error);
done();
});
});

it('should work when path has datetime', function (done) {
testClient.paths.dateTimeValid(new Date('2012-01-01T01:01:01Z'), function (error, result) {
should.not.exist(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ export interface IntModel {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
getUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<number>): void;
getUnixTime(callback: ServiceCallback<number>): void;
getUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<Date>): void;
getUnixTime(callback: ServiceCallback<Date>): void;

/**
* Put datetime encoded as Unix time
*
* @param {number} intBody
* @param {date} intBody
*
* @param {object} [options] Optional Parameters.
*
Expand All @@ -195,8 +195,8 @@ export interface IntModel {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
putUnixTimeDate(intBody: number, options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<void>): void;
putUnixTimeDate(intBody: number, callback: ServiceCallback<void>): void;
putUnixTimeDate(intBody: Date, options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<void>): void;
putUnixTimeDate(intBody: Date, callback: ServiceCallback<void>): void;

/**
* Get invalid Unix time value
Expand All @@ -209,8 +209,8 @@ export interface IntModel {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
getInvalidUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<number>): void;
getInvalidUnixTime(callback: ServiceCallback<number>): void;
getInvalidUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<Date>): void;
getInvalidUnixTime(callback: ServiceCallback<Date>): void;

/**
* Get null Unix time value
Expand All @@ -223,6 +223,6 @@ export interface IntModel {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
getNullUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<number>): void;
getNullUnixTime(callback: ServiceCallback<number>): void;
getNullUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<Date>): void;
getNullUnixTime(callback: ServiceCallback<Date>): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ IntModel.prototype.putMin64 = function (intBody, options, callback) {
*
* {Error} err - The Error object if an error occurred, null otherwise.
*
* {number} [result] - The deserialized result object.
* {date} [result] - The deserialized result object.
*
* {object} [request] - The HTTP Request object if an error did not occur.
*
Expand Down Expand Up @@ -1293,7 +1293,7 @@ IntModel.prototype.getUnixTime = function (options, callback) {
required: false,
serializedName: 'parsedResponse',
type: {
name: 'Number'
name: 'UnixTime'
}
};
result = client.deserialize(resultMapper, parsedResponse, 'result');
Expand All @@ -1313,7 +1313,7 @@ IntModel.prototype.getUnixTime = function (options, callback) {
/**
* Put datetime encoded as Unix time
*
* @param {number} intBody
* @param {date} intBody
*
* @param {object} [options] Optional Parameters.
*
Expand Down Expand Up @@ -1343,9 +1343,10 @@ IntModel.prototype.putUnixTimeDate = function (intBody, options, callback) {
}
// Validate
try {
if (intBody === null || intBody === undefined || typeof intBody !== 'number') {
throw new Error('intBody cannot be null or undefined and it must be of type number.');
}
if(!intBody || !(intBody instanceof Date ||
(typeof intBody.valueOf() === 'string' && !isNaN(Date.parse(intBody))))) {
throw new Error('intBody cannot be null or undefined and it must be of type date.');
}
} catch (error) {
return callback(error);
}
Expand Down Expand Up @@ -1380,7 +1381,7 @@ IntModel.prototype.putUnixTimeDate = function (intBody, options, callback) {
required: true,
serializedName: 'intBody',
type: {
name: 'Number'
name: 'UnixTime'
}
};
requestModel = client.serialize(requestModelMapper, intBody, 'intBody');
Expand Down Expand Up @@ -1445,7 +1446,7 @@ IntModel.prototype.putUnixTimeDate = function (intBody, options, callback) {
*
* {Error} err - The Error object if an error occurred, null otherwise.
*
* {number} [result] - The deserialized result object.
* {date} [result] - The deserialized result object.
*
* {object} [request] - The HTTP Request object if an error did not occur.
*
Expand Down Expand Up @@ -1528,7 +1529,7 @@ IntModel.prototype.getInvalidUnixTime = function (options, callback) {
required: false,
serializedName: 'parsedResponse',
type: {
name: 'Number'
name: 'UnixTime'
}
};
result = client.deserialize(resultMapper, parsedResponse, 'result');
Expand Down Expand Up @@ -1559,7 +1560,7 @@ IntModel.prototype.getInvalidUnixTime = function (options, callback) {
*
* {Error} err - The Error object if an error occurred, null otherwise.
*
* {number} [result] - The deserialized result object.
* {date} [result] - The deserialized result object.
*
* {object} [request] - The HTTP Request object if an error did not occur.
*
Expand Down Expand Up @@ -1642,7 +1643,7 @@ IntModel.prototype.getNullUnixTime = function (options, callback) {
required: false,
serializedName: 'parsedResponse',
type: {
name: 'Number'
name: 'UnixTime'
}
};
result = client.deserialize(resultMapper, parsedResponse, 'result');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,13 @@ export interface IntModel {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
getUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<number>): void;
getUnixTime(callback: ServiceCallback<number>): void;
getUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<Date>): void;
getUnixTime(callback: ServiceCallback<Date>): void;

/**
* Put datetime encoded as Unix time
*
* @param {number} intBody
* @param {date} intBody
*
* @param {object} [options] Optional Parameters.
*
Expand All @@ -292,8 +292,8 @@ export interface IntModel {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
putUnixTimeDate(intBody: number, options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<void>): void;
putUnixTimeDate(intBody: number, callback: ServiceCallback<void>): void;
putUnixTimeDate(intBody: Date, options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<void>): void;
putUnixTimeDate(intBody: Date, callback: ServiceCallback<void>): void;

/**
* Get invalid Unix time value
Expand All @@ -306,8 +306,8 @@ export interface IntModel {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
getInvalidUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<number>): void;
getInvalidUnixTime(callback: ServiceCallback<number>): void;
getInvalidUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<Date>): void;
getInvalidUnixTime(callback: ServiceCallback<Date>): void;

/**
* Get null Unix time value
Expand All @@ -320,6 +320,6 @@ export interface IntModel {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
getNullUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<number>): void;
getNullUnixTime(callback: ServiceCallback<number>): void;
getNullUnixTime(options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<Date>): void;
getNullUnixTime(callback: ServiceCallback<Date>): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ IntModel.prototype.putMin64 = function (intBody, options, callback) {
*
* {Error} err - The Error object if an error occurred, null otherwise.
*
* {number} [result] - The deserialized result object.
* {date} [result] - The deserialized result object.
*
* {object} [request] - The HTTP Request object if an error did not occur.
*
Expand Down Expand Up @@ -1293,7 +1293,7 @@ IntModel.prototype.getUnixTime = function (options, callback) {
required: false,
serializedName: 'parsedResponse',
type: {
name: 'Number'
name: 'UnixTime'
}
};
result = client.deserialize(resultMapper, parsedResponse, 'result');
Expand All @@ -1313,7 +1313,7 @@ IntModel.prototype.getUnixTime = function (options, callback) {
/**
* Put datetime encoded as Unix time
*
* @param {number} intBody
* @param {date} intBody
*
* @param {object} [options] Optional Parameters.
*
Expand Down Expand Up @@ -1343,9 +1343,10 @@ IntModel.prototype.putUnixTimeDate = function (intBody, options, callback) {
}
// Validate
try {
if (intBody === null || intBody === undefined || typeof intBody !== 'number') {
throw new Error('intBody cannot be null or undefined and it must be of type number.');
}
if(!intBody || !(intBody instanceof Date ||
(typeof intBody.valueOf() === 'string' && !isNaN(Date.parse(intBody))))) {
throw new Error('intBody cannot be null or undefined and it must be of type date.');
}
} catch (error) {
return callback(error);
}
Expand Down Expand Up @@ -1380,7 +1381,7 @@ IntModel.prototype.putUnixTimeDate = function (intBody, options, callback) {
required: true,
serializedName: 'intBody',
type: {
name: 'Number'
name: 'UnixTime'
}
};
requestModel = client.serialize(requestModelMapper, intBody, 'intBody');
Expand Down Expand Up @@ -1445,7 +1446,7 @@ IntModel.prototype.putUnixTimeDate = function (intBody, options, callback) {
*
* {Error} err - The Error object if an error occurred, null otherwise.
*
* {number} [result] - The deserialized result object.
* {date} [result] - The deserialized result object.
*
* {object} [request] - The HTTP Request object if an error did not occur.
*
Expand Down Expand Up @@ -1528,7 +1529,7 @@ IntModel.prototype.getInvalidUnixTime = function (options, callback) {
required: false,
serializedName: 'parsedResponse',
type: {
name: 'Number'
name: 'UnixTime'
}
};
result = client.deserialize(resultMapper, parsedResponse, 'result');
Expand Down Expand Up @@ -1559,7 +1560,7 @@ IntModel.prototype.getInvalidUnixTime = function (options, callback) {
*
* {Error} err - The Error object if an error occurred, null otherwise.
*
* {number} [result] - The deserialized result object.
* {date} [result] - The deserialized result object.
*
* {object} [request] - The HTTP Request object if an error did not occur.
*
Expand Down Expand Up @@ -1642,7 +1643,7 @@ IntModel.prototype.getNullUnixTime = function (options, callback) {
required: false,
serializedName: 'parsedResponse',
type: {
name: 'Number'
name: 'UnixTime'
}
};
result = client.deserialize(resultMapper, parsedResponse, 'result');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ export interface Paths {
/**
* Get the date 2016-04-13 encoded value as '1460505600' (Unix time)
*
* @param {number} unixTimeUrlPath Unix time encoded value
* @param {date} unixTimeUrlPath Unix time encoded value
*
* @param {object} [options] Optional Parameters.
*
Expand All @@ -408,8 +408,8 @@ export interface Paths {
* @param {ServiceCallback} [callback] callback function; see ServiceCallback
* doc in ms-rest index.d.ts for details
*/
unixTimeUrl(unixTimeUrlPath: number, options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<void>): void;
unixTimeUrl(unixTimeUrlPath: number, callback: ServiceCallback<void>): void;
unixTimeUrl(unixTimeUrlPath: Date, options: { customHeaders? : { [headerName: string]: string; } }, callback: ServiceCallback<void>): void;
unixTimeUrl(unixTimeUrlPath: Date, callback: ServiceCallback<void>): void;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,7 @@ Paths.prototype.arrayCsvInPath = function (arrayPath, options, callback) {
/**
* Get the date 2016-04-13 encoded value as '1460505600' (Unix time)
*
* @param {number} unixTimeUrlPath Unix time encoded value
* @param {date} unixTimeUrlPath Unix time encoded value
*
* @param {object} [options] Optional Parameters.
*
Expand Down Expand Up @@ -2491,17 +2491,18 @@ Paths.prototype.unixTimeUrl = function (unixTimeUrlPath, options, callback) {
}
// Validate
try {
if (unixTimeUrlPath === null || unixTimeUrlPath === undefined || typeof unixTimeUrlPath !== 'number') {
throw new Error('unixTimeUrlPath cannot be null or undefined and it must be of type number.');
}
if(!unixTimeUrlPath || !(unixTimeUrlPath instanceof Date ||
(typeof unixTimeUrlPath.valueOf() === 'string' && !isNaN(Date.parse(unixTimeUrlPath))))) {
throw new Error('unixTimeUrlPath cannot be null or undefined and it must be of type date.');
}
} catch (error) {
return callback(error);
}

// Construct URL
var requestUrl = this.client.baseUri +
'//paths/int/1460505600/{unixTimeUrlPath}';
requestUrl = requestUrl.replace('{unixTimeUrlPath}', encodeURIComponent(unixTimeUrlPath.toString()));
requestUrl = requestUrl.replace('{unixTimeUrlPath}', encodeURIComponent(client.serialize({required: true, serializedName: 'unixTimeUrlPath', type: {name: 'UnixTime'}}, unixTimeUrlPath, 'unixTimeUrlPath')));
// trim all duplicate forward slashes in the url
var regex = /([^:]\/)\/+/gi;
requestUrl = requestUrl.replace(regex, '$1');
Expand Down
Loading

0 comments on commit 37a5369

Please sign in to comment.