Skip to content

Instantly share code, notes, and snippets.

@andrewjjenkins
Last active August 29, 2015 14:18
Show Gist options
  • Save andrewjjenkins/427b839de01d620d3c0d to your computer and use it in GitHub Desktop.
Save andrewjjenkins/427b839de01d620d3c0d to your computer and use it in GitHub Desktop.
HTTP client that POSTs arbitrarily large bodies.
var fs = require('fs');
var reqSize = process.env.REQSIZE || ('' + 1024*1024*1024);
reqSize = parseInt(reqSize);
var genericBlob = (new Array(10240 + 1)).join('0123456789');
//Client part.
function runClient() {
var bytesSent = 0;
req = https.request({
// host: 'localhost',
host: '201.0.224.1',
port: 443,
method: 'POST',
path: '/',
rejectUnauthorized: false,
}, function (res) {
var body = '';
res.on('data', function (d) {
body += d;
}).on('end', function () {
console.log('Request got response: ' + body);
});
});
function sendMoreStuffIfNeeded() {
if (bytesSent >= reqSize) {
console.log('Request done, bytesSent: ', bytesSent);
req.end();
return;
}
var didDrain = req.write(genericBlob);
if (didDrain) {
process.nextTick(sendMoreStuffIfNeeded);
} else {
req.once('drain', sendMoreStuffIfNeeded);
}
bytesSent += genericBlob.length;
}
sendMoreStuffIfNeeded();
}
runClient();
//Server part.
/*
var cert = fs.readFileSync('/path/to/cert'),
key = fs.readFileSync('/path/to/key');
var port;
var server = https.createServer({
key: key,
cert: cert,
}, function (req, res) {
var reqSize = 0;
req.on('data', function (d) {
//console.log('Server got a data chunk of %d bytes', d.length);
reqSize += d.length;
});
req.on('end', function () {
var body = 'Request done, ' + reqSize + ' bytes total';
console.log(body);
res.writeHead(
200,
{ 'Content-Type': 'text/plain',
'Content-Length': body.length });
res.end(body);
});
}).listen(0, function () {
port = server.address().port;
console.log('Listening on port %d', port);
runClient();
});
*/
@andrewjjenkins
Copy link
Author

This will send your REQSIZE rounded up to the nearest 10KiB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment