summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-readable.js
blob: c035132eb0deccb615c48ceb3b05056fd4ab254e (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
51
52
53
54
55
56
'use strict';
const common = require('../common');
var assert = require('assert');
var http = require('http');
var util = require('util');

var Duplex = require('stream').Duplex;

function FakeAgent() {
  http.Agent.call(this);
}
util.inherits(FakeAgent, http.Agent);

FakeAgent.prototype.createConnection = function createConnection() {
  var s = new Duplex();
  var once = false;

  s._read = function read() {
    if (once)
      return this.push(null);
    once = true;

    this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
    this.push('b\r\nhello world\r\n');
    this.readable = false;
    this.push('0\r\n\r\n');
  };

  // Blackhole
  s._write = function write(data, enc, cb) {
    cb();
  };

  s.destroy = s.destroySoon = function destroy() {
    this.writable = false;
  };

  return s;
};

var received = '';

var req = http.request({
  agent: new FakeAgent()
}, common.mustCall(function(res) {
  res.on('data', function(chunk) {
    received += chunk;
  });

  res.on('end', common.mustCall(function() {}));
}));
req.end();

process.on('exit', function() {
  assert.equal(received, 'hello world');
});