• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const { createServer } = require('http');
6const { connect } = require('net');
7
8// This test validates that the server returns 408
9// after server.requestTimeout if the client
10// pauses before start sending the body.
11
12let sendDelayedRequestBody;
13const server = createServer(common.mustCall((req, res) => {
14  let body = '';
15  req.setEncoding('utf-8');
16
17  req.on('data', (chunk) => {
18    body += chunk;
19  });
20
21  req.on('end', () => {
22    res.writeHead(200, { 'Content-Type': 'text/plain' });
23    res.write(body);
24    res.end();
25  });
26
27  assert.strictEqual(typeof sendDelayedRequestBody, 'function');
28  sendDelayedRequestBody();
29}));
30
31// 0 seconds is the default
32assert.strictEqual(server.requestTimeout, 0);
33const requestTimeout = common.platformTimeout(1000);
34server.requestTimeout = requestTimeout;
35assert.strictEqual(server.requestTimeout, requestTimeout);
36
37server.listen(0, common.mustCall(() => {
38  const client = connect(server.address().port);
39  let response = '';
40
41  client.on('data', common.mustCall((chunk) => {
42    response += chunk.toString('utf-8');
43  }));
44
45  client.resume();
46  client.write('POST / HTTP/1.1\r\n');
47  client.write('Content-Length: 20\r\n');
48  client.write('Connection: close\r\n');
49  client.write('\r\n');
50
51  sendDelayedRequestBody = common.mustCall(() => {
52    setTimeout(() => {
53      client.write('12345678901234567890\r\n\r\n');
54    }, common.platformTimeout(2000)).unref();
55  });
56
57  const errOrEnd = common.mustCall(function(err) {
58    console.log(err);
59    assert.strictEqual(
60      response,
61      'HTTP/1.1 408 Request Timeout\r\nConnection: close\r\n\r\n'
62    );
63    server.close();
64  });
65
66  client.on('end', errOrEnd);
67  client.on('error', errOrEnd);
68}));
69