Skip to content

Commit

Permalink
Adding responseTime. Closes #60.
Browse files Browse the repository at this point in the history
  • Loading branch information
dareid committed Aug 6, 2016
1 parent 68b1386 commit 837e0cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ exports.request = function (method, url, params) {

var deferred = Q.defer();
var reqObj = defaultedRequestObj || request;
var timer = process.hrtime();
reqObj(options, function (error, response, body) {
var elapsedTime = process.hrtime(timer);
var elapsedMilliseconds = (elapsedTime[0] * 1000) + (elapsedTime[1] / 1000000);
/**
Chakram Response Object
@desc Encapsulates the results of a HTTP call into a single object
Expand All @@ -50,13 +53,15 @@ Chakram Response Object
@property {String|Buffer|Object} body - The response body. Typically a JSON object unless the json option has been set to false, in which case will be either a String or Buffer
@property {Object} jar - A {@link https://github.com/goinstant/tough-cookie tough cookie} jar
@property {String} url - The request's original URL
@property {Number} responseTime - The time taken to make the request (including redirects) at millisecond resolution
*/
deferred.resolve({
error : error,
response: response,
body: body,
jar: options.jar,
url: url
url: url,
responseTime: elapsedMilliseconds
});
});
return deferred.promise;
Expand Down Expand Up @@ -165,4 +170,4 @@ Clears any previously set default options.
*/
exports.clearRequestDefaults = function () {
defaultedRequestObj = undefined;
};
};
8 changes: 8 additions & 0 deletions test/core/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ describe("Chakram", function() {
expect(request).not.to.have.status(400);
return chakram.wait().then(assertChakramResponseObject);
});

it("should record response time", function () {
this.timeout(3000);
return chakram.get("http://httpbin.org/delay/2")
.then(function (obj) {
expect(obj.responseTime).to.exist.and.to.be.at.least(2000).and.at.most(3000);
});
});
});

describe("Multiple expects", function () {
Expand Down

0 comments on commit 837e0cc

Please sign in to comment.