Created
April 12, 2011 00:53
-
-
Save ry/914705 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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