Skip to content

Commit

Permalink
Add support for custom timeout setting (#176)
Browse files Browse the repository at this point in the history
* Add support for custom timeout setting
* Document new option parameter
  • Loading branch information
holm authored and mheap committed Jan 11, 2018
1 parent 177eb92 commit 1274b1c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ Options are:
log: function() {level, args...}
info: function() {args...},
warn: function() {args...}
}
},
// Set a custom timeout for requests to Nexmo in milliseconds. Defaults to the standard for Node http requests, which is 120,000 ms.
timeout: integer
}
```

Expand Down
7 changes: 6 additions & 1 deletion src/HttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HttpClient {
Accept: "application/json"
};
this.logger = options.logger;
this.timeout = options.timeout;

if (options.userAgent) {
this.headers["User-Agent"] = options.userAgent;
Expand Down Expand Up @@ -42,6 +43,10 @@ class HttpClient {
headers: Object.assign({}, this.headers)
};

if (this.timeout !== undefined) {
options.timeout = this.timeout;
}

// Allow existing headers to be overridden
// Allow new headers to be added
if (endpoint.headers) {
Expand All @@ -56,7 +61,7 @@ class HttpClient {
if (options.port === 443) {
request = this.https.request(options);
} else {
request = http.request(options);
request = this.http.request(options);
}

request.end(endpoint.body);
Expand Down
33 changes: 33 additions & 0 deletions test/HttpClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,39 @@ describe("HttpClient Object", function() {
);
});

it("should be possible to set the timeout", function() {
var mock = sinon.mock(fakeHttp);
mock
.expects("request")
.once()
.withArgs({
headers: defaultHeaders,
host: "api.nexmo.com",
method: "POST",
path: "/api",
port: 443,
timeout: 5000
})
.returns(fakeRequest);

var client = new HttpClient({
https: fakeHttp,
logger: logger,
timeout: 5000
});

client.request(
{
host: "api.nexmo.com",
path: "/api"
},
"POST",
{
some: "data"
}
);
});

it("should not override the method when method and callback are undefined", function() {
var mock = sinon.mock(fakeHttp);
mock
Expand Down

0 comments on commit 1274b1c

Please sign in to comment.