Skip to content

Instantly share code, notes, and snippets.

@alexindigo
Created March 19, 2016 21:32
Show Gist options
  • Save alexindigo/fda897bc286443a87d08 to your computer and use it in GitHub Desktop.
Save alexindigo/fda897bc286443a87d08 to your computer and use it in GitHub Desktop.
var common = require('../common');
var assert = common.assert;
var http = require('http');
var FormData = require(common.dir.lib + '/form_data');
var CRLF = '\r\n';
var testHeader = 'X-Test-Fake: 123';
var expectedLength;
// ^----- I assume this part is obvious
// creating new server to receive FormData's (test) submition
var server = http.createServer(function(req, res)
{
// check that content-length header exists
assert.ok( typeof req.headers['content-length'] !== 'undefined' );
// check that it has expected length
assert.equal(req.headers['content-length'], expectedLength);
// return OK code back to FormData to allow it end the test
res.writeHead(200);
res.end('done');
});
// Wait for receiving server to start up (it takes time)
server.listen(common.port, function()
{
// creating FormData instance
var form = new FormData();
// preparing custom options with custom header for test field
var options = {
header:
// this is basically how per-field headers look like in multipart
CRLF + '--' + form.getBoundary() + CRLF +
testHeader +
CRLF + CRLF,
// override content-length,
// much lower than actual buffer size (1000)
knownLength: 1
};
// generating 1000 elements array
// to make a Buffer to use as test fields value
// Note: naive approach
var bufferData = [];
for (var z = 0; z < 1000; z++) {
bufferData.push(1);
}
var buffer = new Buffer(bufferData);
// add test field to the FormData's instance
form.append('my_buffer', buffer, options);
// (available to req handler)
// calculating length of the whole request body,
// including per-field header, boundaries and number of CRLFs
// (I don't remember why it's done this specific way) :)
expectedLength = form._lastBoundary().length + form._overheadLength + options.knownLength;
// do submit dance
// tell FormData, to submit things to the server
common.actions.submit(form, server);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment