Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions assets/supportedOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ let supportedOptions = [
description: 'Username and password for server authentication',
format: '[string]',
collectValues: false
},
{
short: '-b',
long: '--cookie',
description: 'Specifies cookies to be used in the format "NAME=VALUE" or a file to read cookies from',
format: '[string]',
collectValues: true
}
];
module.exports = supportedOptions;
12 changes: 10 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
[REQUEST_BODY_LANGUAGE_JAVASCRIPT]: /^(text|application)\/(\S+\+)?javascript/,
[REQUEST_BODY_LANGUAGE_XML]: /^(text|application)\/(\S+\+)?xml/,
[REQUEST_BODY_LANGUAGE_HTML]: /^text\/html/
};
},
ALLOWED_DUPLICATE_HEADERS = ['cookie'];

var program,

Expand Down Expand Up @@ -177,6 +178,13 @@
});
}

// If any cookies are added under -b or --cookie arg, add them as Cookie header
if (curlObj.cookie && Array.isArray(curlObj.cookie)) {
curlObj.cookie.forEach((cookieVal) => {
headerArray.push('Cookie: ' + this.trimQuotesFromString(cookieVal));
});
}

if (headerArray === null || headerArray.length === 0) {
return retVal;
}
Expand Down Expand Up @@ -210,7 +218,7 @@
value = thisHeader.substring(keyIndex + 1, thisHeader.length).trim();
/* eslint-enable */

if (this.headerPairs.hasOwnProperty(key)) {
if (this.headerPairs.hasOwnProperty(key) && !ALLOWED_DUPLICATE_HEADERS.includes(key.toLowerCase())) {
// don't add the same header twice
continue;
}
Expand Down Expand Up @@ -875,7 +883,7 @@
* get data arg value from raw args as formdata boundary separated string
* is not parsed as any of data options by commander
*/
_.forEach(curlObj.rawArgs, (arg, index) => {

Check warning on line 886 in src/lib.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (12.x)

Expected to return a value at the end of arrow function

Check warning on line 886 in src/lib.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (16.x)

Expected to return a value at the end of arrow function

Check warning on line 886 in src/lib.js

View workflow job for this annotation

GitHub Actions / Unit-Tests (18.x)

Expected to return a value at the end of arrow function
if (_.includes(formDataOptions, arg)) {
formData = _.get(curlObj.rawArgs, index + 1);
return false;
Expand Down
80 changes: 80 additions & 0 deletions test/conversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1444,4 +1444,84 @@ describe('Curl converter should', function() {
});
});
});

describe('It should correctly generate request with cookie header', function() {
it('containing -b option', function(done) {
convert({
type: 'string',
data: `curl 'https://httpbin.org/anything' \
-b 'dashboard_beta=yes; postman-beta.track=default' \
-H 'authority: httpbin.org'
--data "{\"context\":{\"client\":{\"hl\":\"en\"}},\"state\":true}"
`
}, function (err, result) {
expect(result.result).to.equal(true);
expect(result.output.length).to.equal(1);
expect(result.output[0].type).to.equal('request');
expect(result.output[0].data.url).to.equal('https://httpbin.org/anything');
expect(result.output[0].data.method).to.equal('POST');

const headerArr = result.output[0].data.header;
expect(headerArr.length).to.equal(2);
expect(headerArr[0].key).to.equal('authority');
expect(headerArr[0].value).to.equal('httpbin.org');
expect(headerArr[1].key).to.equal('Cookie');
expect(headerArr[1].value).to.equal('dashboard_beta=yes; postman-beta.track=default');
done();
});
});

it('containing --cookie option', function(done) {
convert({
type: 'string',
data: `curl 'https://httpbin.org/anything' \
--cookie 'dashboard_beta=yes; postman-beta.track=default' \
-H 'authority: httpbin.org'
--data "{\"context\":{\"client\":{\"hl\":\"en\"}},\"state\":true}"
`
}, function (err, result) {
expect(result.result).to.equal(true);
expect(result.output.length).to.equal(1);
expect(result.output[0].type).to.equal('request');
expect(result.output[0].data.url).to.equal('https://httpbin.org/anything');
expect(result.output[0].data.method).to.equal('POST');

const headerArr = result.output[0].data.header;
expect(headerArr.length).to.equal(2);
expect(headerArr[0].key).to.equal('authority');
expect(headerArr[0].value).to.equal('httpbin.org');
expect(headerArr[1].key).to.equal('Cookie');
expect(headerArr[1].value).to.equal('dashboard_beta=yes; postman-beta.track=default');
done();
});
});

it('containing multiple cookies', function(done) {
convert({
type: 'string',
data: `curl 'https://httpbin.org/anything' \
-b 'dashboard_beta=yes; postman-beta.track=default' \
-b 'name=JohnDoe' \
-H 'authority: httpbin.org' \
--data "{\"context\":{\"client\":{\"hl\":\"en\"}},\"state\":true}"
`
}, function (err, result) {
expect(result.result).to.equal(true);
expect(result.output.length).to.equal(1);
expect(result.output[0].type).to.equal('request');
expect(result.output[0].data.url).to.equal('https://httpbin.org/anything');
expect(result.output[0].data.method).to.equal('POST');

const headerArr = result.output[0].data.header;
expect(headerArr.length).to.equal(3);
expect(headerArr[0].key).to.equal('authority');
expect(headerArr[0].value).to.equal('httpbin.org');
expect(headerArr[1].key).to.equal('Cookie');
expect(headerArr[1].value).to.equal('dashboard_beta=yes; postman-beta.track=default');
expect(headerArr[2].key).to.equal('Cookie');
expect(headerArr[2].value).to.equal('name=JohnDoe');
done();
});
});
});
});