summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-information-processing.js
blob: b9e5fea339ad0ff871f5d565be69c7d45edfc5f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
require('../common');
const assert = require('assert');
const http = require('http');
const debug = require('util').debuglog('test');

const testResBody = 'other stuff!\n';
const kMessageCount = 2;

const server = http.createServer((req, res) => {
  for (let i = 0; i < kMessageCount; i++) {
    debug(`Server sending informational message #${i}...`);
    res.writeProcessing();
  }
  debug('Server sending full response...');
  res.writeHead(200, {
    'Content-Type': 'text/plain',
    'ABCD': '1'
  });
  res.end(testResBody);
});

server.listen(0, function() {
  const req = http.request({
    port: this.address().port,
    path: '/world'
  });
  req.end();
  debug('Client sending request...');

  let body = '';
  let infoCount = 0;

  req.on('information', () => { infoCount++; });

  req.on('response', function(res) {
    // Check that all 102 Processing received before full response received.
    assert.strictEqual(infoCount, kMessageCount);
    assert.strictEqual(res.statusCode, 200,
                       `Final status code was ${res.statusCode}, not 200.`);
    res.setEncoding('utf8');
    res.on('data', function(chunk) { body += chunk; });
    res.on('end', function() {
      debug('Got full response.');
      assert.strictEqual(body, testResBody);
      assert.ok('abcd' in res.headers);
      server.close();
    });
  });
});