• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const http = require('http');
6const helloWorld = 'Hello World!';
7const helloAgainLater = 'Hello again later!';
8
9let next = null;
10
11const server = http.createServer((req, res) => {
12  res.writeHead(200, {
13    'Content-Length': `${(helloWorld.length + helloAgainLater.length)}`
14  });
15
16  // We need to make sure the data is flushed
17  // before writing again
18  next = () => {
19    res.end(helloAgainLater);
20    next = () => { };
21  };
22
23  res.write(helloWorld);
24}).listen(0, function() {
25  const opts = {
26    hostname: 'localhost',
27    port: server.address().port,
28    path: '/'
29  };
30
31  const expectedData = [helloWorld, helloAgainLater];
32  const expectedRead = [helloWorld, null, helloAgainLater, null, null];
33
34  const req = http.request(opts, (res) => {
35    res.on('error', common.mustNotCall());
36
37    res.on('readable', common.mustCall(() => {
38      let data;
39
40      do {
41        data = res.read();
42        assert.strictEqual(data, expectedRead.shift());
43        next();
44      } while (data !== null);
45    }, 3));
46
47    res.setEncoding('utf8');
48    res.on('data', common.mustCall((data) => {
49      assert.strictEqual(data, expectedData.shift());
50    }, 2));
51
52    res.on('end', common.mustCall(() => {
53      server.close();
54    }));
55  });
56
57  req.end();
58});
59