summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-set-timeout.js
diff options
context:
space:
mode:
authorLuigi Pinca <luigipinca@gmail.com>2019-01-13 15:30:50 +0100
committerLuigi Pinca <luigipinca@gmail.com>2019-02-12 08:01:26 +0100
commitb361f9577fbd72e518438d3fa0b01f7d34d814a5 (patch)
treec5f2645f0e12fc9894fe80efe9961020d8f0411b /test/parallel/test-http-client-set-timeout.js
parent40a8a7391664e7a5d8a264a1d85d059f9c05063b (diff)
downloadandroid-node-v8-b361f9577fbd72e518438d3fa0b01f7d34d814a5.tar.gz
android-node-v8-b361f9577fbd72e518438d3fa0b01f7d34d814a5.tar.bz2
android-node-v8-b361f9577fbd72e518438d3fa0b01f7d34d814a5.zip
test: refactor two http client timeout tests
Refactor test-http-client-set-timeout and test-http-client-timeout-option-with-agent. PR-URL: https://github.com/nodejs/node/pull/25473 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-http-client-set-timeout.js')
-rw-r--r--test/parallel/test-http-client-set-timeout.js70
1 files changed, 36 insertions, 34 deletions
diff --git a/test/parallel/test-http-client-set-timeout.js b/test/parallel/test-http-client-set-timeout.js
index 51284b4276..15aae7023b 100644
--- a/test/parallel/test-http-client-set-timeout.js
+++ b/test/parallel/test-http-client-set-timeout.js
@@ -1,46 +1,48 @@
'use strict';
-const common = require('../common');
-// Test that `req.setTimeout` will fired exactly once.
+// Test that the `'timeout'` event is emitted exactly once if the `timeout`
+// option and `request.setTimeout()` are used together.
-const assert = require('assert');
-const http = require('http');
+const { expectsError, mustCall } = require('../common');
+const { strictEqual } = require('assert');
+const { createServer, get } = require('http');
-const HTTP_CLIENT_TIMEOUT = 2000;
-
-const options = {
- method: 'GET',
- port: undefined,
- host: '127.0.0.1',
- path: '/',
- timeout: HTTP_CLIENT_TIMEOUT,
-};
-
-const server = http.createServer(() => {
+const server = createServer(() => {
// Never respond.
});
-server.listen(0, options.host, function() {
- doRequest();
-});
-
-function doRequest() {
- options.port = server.address().port;
- const req = http.request(options);
- req.setTimeout(HTTP_CLIENT_TIMEOUT / 2);
- req.on('error', () => {
- // This space is intentionally left blank.
+server.listen(0, mustCall(() => {
+ const req = get({
+ port: server.address().port,
+ timeout: 2000,
});
- req.on('close', common.mustCall(() => server.close()));
- let timeout_events = 0;
- req.on('timeout', common.mustCall(() => {
- timeout_events += 1;
+ req.setTimeout(1000);
+
+ req.on('socket', mustCall((socket) => {
+ strictEqual(socket.timeout, 2000);
+
+ socket.on('connect', mustCall(() => {
+ strictEqual(socket.timeout, 1000);
+
+ // Reschedule the timer to not wait 1 sec and make the test finish faster.
+ socket.setTimeout(10);
+ strictEqual(socket.timeout, 10);
+ }));
+ }));
+
+ req.on('error', expectsError({
+ type: Error,
+ code: 'ECONNRESET',
+ message: 'socket hang up'
}));
- req.end();
- setTimeout(function() {
+ req.on('close', mustCall(() => {
+ server.close();
+ }));
+
+ req.on('timeout', mustCall(() => {
+ strictEqual(req.socket.listenerCount('timeout'), 0);
req.destroy();
- assert.strictEqual(timeout_events, 1);
- }, common.platformTimeout(HTTP_CLIENT_TIMEOUT));
-}
+ }));
+}));