Created
October 18, 2012 11:48
-
-
Save isaacs/3911308 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 http = require('http'); | |
var assert = require('assert'); | |
// poo face, apple, many tigers | |
var STR = "π©ο£Ώπ π π π π π π π "; | |
var BUF = new Buffer(STR, 'utf8'); | |
var server = http.createServer(function(req, res) { | |
req.setEncoding('utf8'); | |
var str = ''; | |
req.on('data', function(chunk) { | |
assert.ok(typeof chunk === 'string'); | |
console.error('SERVER got chunk %j', chunk); | |
str += chunk; | |
}); | |
req.on('end', function() { | |
console.error('str = %j', str); | |
// now send the response 1 byte at a time. | |
var b = new Buffer(str, 'utf8'); | |
var i = 0; | |
setTimeout(function r() { | |
res.write(b.slice(i, i + 1)); | |
i++; | |
if (i < b.length) | |
setTimeout(r, 1); | |
else | |
res.end(); | |
}, 1); | |
}); | |
}); | |
server.listen(1337); | |
var req = http.request({ | |
method: 'POST', | |
path: '/', | |
host: 'localhost', | |
port: 1337 | |
}); | |
var i = 0; | |
setTimeout(function r() { | |
req.write(BUF.slice(i, i + 1)); | |
i++; | |
if (i < BUF.length) | |
setTimeout(r, 1); | |
else | |
req.end(); | |
}, 1); | |
req.on('response', function(res) { | |
res.setEncoding('utf8'); | |
var str = ''; | |
res.on('data', function(chunk) { | |
assert.ok(typeof chunk === 'string'); | |
console.error('CLIENT got chunk %j', chunk); | |
str += chunk; | |
}); | |
res.on('end', function() { | |
console.error('response str = %j', str); | |
assert.equal(str, STR); | |
assert.equal(new Buffer(str, 'utf8').toString('hex'), BUF.toString('hex')); | |
console.log('works'); | |
server.close(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment