fix header handling for requests with a single header

This commit is contained in:
Nick Carneiro
2016-02-12 16:25:15 -08:00
parent 77e61c9109
commit e7fabf0b95
4 changed files with 31 additions and 1 deletions

1
test/curl8.txt Normal file
View File

@@ -0,0 +1 @@
curl http://example.com/ -H 'foo: bar'

18
test/node_output8.js Normal file
View File

@@ -0,0 +1,18 @@
var request = require('request');
var headers = {
'foo': 'bar'
};
var options = {
url: 'http://example.com/',
headers: headers
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);

View File

@@ -162,4 +162,12 @@ test('http post with data - php', function (t) {
var goodPhpCode = fs.readFileSync(__dirname + '/php_output3.php', 'utf-8').trim(); var goodPhpCode = fs.readFileSync(__dirname + '/php_output3.php', 'utf-8').trim();
t.equal(phpCode, goodPhpCode); t.equal(phpCode, goodPhpCode);
t.end(); t.end();
}); });
test('http get with single header - node', function (t) {
var curlHttpPostCommand = fs.readFileSync(__dirname + '/curl8.txt', 'utf-8');
var nodeCode = curlconverter.toNode(curlHttpPostCommand);
var goodNodeCode = fs.readFileSync(__dirname + '/node_output8.js', 'utf-8').trim();
t.equal(nodeCode, goodNodeCode);
t.end();
});

View File

@@ -22,6 +22,9 @@ var parseCurlCommand = function(curlCommand) {
var headers; var headers;
if (parsedArguments.H) { if (parsedArguments.H) {
headers = {}; headers = {};
if (!Array.isArray(parsedArguments.H)) {
parsedArguments.H = [parsedArguments.H];
}
parsedArguments.H.forEach(function (header) { parsedArguments.H.forEach(function (header) {
if (header.indexOf('Cookie') !== -1) { if (header.indexOf('Cookie') !== -1) {
cookieString = header; cookieString = header;