Remove excessive copyright/license boilerplate
[platform/upstream/nodejs.git] / test / parallel / test-http-client-readable.js
1 var common = require('../common');
2 var assert = require('assert');
3 var http = require('http');
4 var util = require('util');
5
6 var Duplex = require('stream').Duplex;
7
8 function FakeAgent() {
9   http.Agent.call(this);
10 }
11 util.inherits(FakeAgent, http.Agent);
12
13 FakeAgent.prototype.createConnection = function createConnection() {
14   var s = new Duplex();
15   var once = false;
16
17   s._read = function read() {
18     if (once)
19       return this.push(null);
20     once = true;
21
22     this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
23     this.push('b\r\nhello world\r\n');
24     this.readable = false;
25     this.push('0\r\n\r\n');
26   };
27
28   // Blackhole
29   s._write = function write(data, enc, cb) {
30     cb();
31   };
32
33   s.destroy = s.destroySoon = function destroy() {
34     this.writable = false;
35   };
36
37   return s;
38 };
39
40 var received = '';
41 var ended = 0;
42
43 var req = http.request({
44   agent: new FakeAgent()
45 }, function(res) {
46   res.on('data', function(chunk) {
47     received += chunk;
48   });
49
50   res.on('end', function() {
51     ended++;
52   });
53 });
54 req.end();
55
56 process.on('exit', function() {
57   assert.equal(received, 'hello world');
58   assert.equal(ended, 1);
59 });