public
Created

  • Download Gist
test-http-client-pipe.js
JavaScript
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 57 58 59 60 61
var common = require('../common');
var assert = require('assert');
var http = require('http');
var stream = require('stream');
var util = require('util');
 
function SlowStream() {
stream.Stream.call(this);
this.writable = true;
this.output = '';
}
util.inherits(SlowStream, stream.Stream);
 
SlowStream.prototype.write = function(buffer) {
this.output += buffer.toString();
console.log('wrote data.');
var self = this;
setTimeout(function() {
self.emit('drain');
}, 100);
return false;
};
 
SlowStream.prototype.end = function() {
testServer.close();
};
 
var chunks = ['hello ', 'world'];
 
var testServer = new http.Server(function(req, res) {
res.writeHead(200);
res.write(chunks[0]);
process.nextTick(function() {
res.write(chunks[1]);
process.nextTick(function() {
res.end();
});
});
});
 
 
var outputStream = new SlowStream();
 
testServer.listen(common.PORT, function () {
var request = http.get({
host: 'localhost',
port: common.PORT,
path: '/',
}, function(response) {
response.on('data', function() { console.log('got some data'); });
// If response.readable isn't being properly set to false, this will
// cause an error, because the final time .resume() is called on the
// response the underlying socket will already be closed.
response.pipe(outputStream);
});
});
 
 
process.addListener('exit', function() {
assert.equal(outputStream.output, chunks.join(''));
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.