Skip to content

Instantly share code, notes, and snippets.

@ry
Created April 12, 2011 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ry/914705 to your computer and use it in GitHub Desktop.
Save ry/914705 to your computer and use it in GitHub Desktop.
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(''));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment