Files
DemoApp/node_modules/gzippo/test/request.js
2013-02-14 08:18:17 -05:00

51 lines
943 B
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
var http = require('http')
;
var port;
// basic request mocking function
module.exports = function (options) {
port = options.port || 32123;
return request;
};
var request = function(path, headers, callback) {
var options = {
host: '127.0.0.1',
port: port,
path: path,
headers: headers || {},
method: 'GET'
};
var req = http.request(options, function(res) {
var buffers = []
, total = 0;
res.on('data', function(buf) {
buffers.push(buf);
total += buf.length;
});
res.on('end', function() {
var data = new Buffer(total)
, offset = 0;
for (var i = 0; i < buffers.length; i++) {
buffers[i].copy(data, offset);
offset += buffers[i].length;
}
callback(null, res, data);
});
res.on('error', function(err) {
callback(err);
});
});
req.on('error', function(err) {
callback(err);
});
req.end();
};