Skip to content

Commit

Permalink
Added possibility to measure request response time
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Flitsch committed Jun 6, 2019
1 parent 2132cff commit 4e2c9b5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ response of your API.
* `jsonTypes` - Match JSON structure + value types
* `jsonTypesStrict` - Match EXACT JSON structure + value types (extra keys not tested for cause test failures)
* `bodyContains` - Match partial body content (string or regex)
* `responseTime` - Check if request completes within a specified duration (ms)

## Define Custom Expect Handlers

Expand Down
10 changes: 10 additions & 0 deletions src/frisby/expects.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ function incrementAssertionCount() {

const expects = {

responseTime(response, maxResponseTime) {
incrementAssertionCount();

assert.ok(
maxResponseTime >= response.responseTime,
`Request took longer than ${maxResponseTime}ms: (${response.responseTime}ms).`
);
},


status(response, statusCode) {
incrementAssertionCount();

Expand Down
6 changes: 5 additions & 1 deletion src/frisby/response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
"use strict";

class FrisbyResponse {
constructor(fetchResponse) {
Expand All @@ -20,6 +20,10 @@ class FrisbyResponse {
get json() {
return this._json;
}

get responseTime() {
return this._responseTimeMs;
}
}

module.exports = FrisbyResponse;
3 changes: 3 additions & 0 deletions src/frisby/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class FrisbySpec {
this._fetch = Promise.resolve(fetchResponse)
.then(response => {
this._response = new FrisbyResponse(response);
this._response._responseTimeMs = 0;
return response.text()
.then(text => {
let response = this._response;
Expand Down Expand Up @@ -129,9 +130,11 @@ class FrisbySpec {
let fetchParams = this._fetchParams(params);
this._request = new fetch.Request(this._formatUrl(url, options.urlEncode), fetchParams);

let requestStartTime = Date.now();
this._fetch = fetch(this._request, { timeout: this.timeout() }) // 'timeout' is a node-fetch option
.then(response => {
this._response = new FrisbyResponse(response);
this._response._responseTimeMs = Date.now() - requestStartTime;
if (this._setupDefaults.request && this._setupDefaults.request.rawBody) {
return response.arrayBuffer()
.then(buffer => {
Expand Down

0 comments on commit 4e2c9b5

Please sign in to comment.