• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const http = require('http');
5const Countdown = require('../common/countdown');
6
7const test_res_body = 'other stuff!\n';
8const countdown = new Countdown(2, () => server.close());
9
10const server = http.createServer((req, res) => {
11  console.error('Server sending informational message #1...');
12  // These function calls may rewritten as necessary
13  // to call res.writeHead instead
14  res._writeRaw('HTTP/1.1 102 Processing\r\n');
15  res._writeRaw('Foo: Bar\r\n');
16  res._writeRaw('\r\n', common.mustCall());
17  console.error('Server sending full response...');
18  res.writeHead(200, {
19    'Content-Type': 'text/plain',
20    'ABCD': '1'
21  });
22  res.end(test_res_body);
23});
24
25server.listen(0, function() {
26  const req = http.request({
27    port: this.address().port,
28    path: '/world'
29  });
30  req.end();
31  console.error('Client sending request...');
32
33  let body = '';
34
35  req.on('information', function(res) {
36    assert.strictEqual(res.httpVersion, '1.1');
37    assert.strictEqual(res.httpVersionMajor, 1);
38    assert.strictEqual(res.httpVersionMinor, 1);
39    assert.strictEqual(res.statusCode, 102,
40                       `Received ${res.statusCode}, not 102.`);
41    assert.strictEqual(res.statusMessage, 'Processing',
42                       `Received ${res.statusMessage}, not "Processing".`);
43    assert.strictEqual(res.headers.foo, 'Bar');
44    assert.strictEqual(res.rawHeaders[0], 'Foo');
45    assert.strictEqual(res.rawHeaders[1], 'Bar');
46    console.error('Client got 102 Processing...');
47    countdown.dec();
48  });
49
50  req.on('response', function(res) {
51    // Check that all 102 Processing received before full response received.
52    assert.strictEqual(countdown.remaining, 1);
53    assert.strictEqual(res.statusCode, 200,
54                       `Final status code was ${res.statusCode}, not 200.`);
55    res.setEncoding('utf8');
56    res.on('data', function(chunk) { body += chunk; });
57    res.on('end', function() {
58      console.error('Got full response.');
59      assert.strictEqual(body, test_res_body);
60      assert.ok('abcd' in res.headers);
61      countdown.dec();
62    });
63  });
64});
65