Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created October 18, 2012 11:48
Show Gist options
  • Save isaacs/3911308 to your computer and use it in GitHub Desktop.
Save isaacs/3911308 to your computer and use it in GitHub Desktop.
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