Skip to content

Commit e5a84c2

Browse files
committed
Merge branch 'release/1.4.0'
2 parents b9c38f3 + d7a97e9 commit e5a84c2

File tree

6 files changed

+112
-4
lines changed

6 files changed

+112
-4
lines changed

.github/workflows/test.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
Unit-Tests:
7+
runs-on: ubuntu-20.04
8+
steps:
9+
- name: Get Code
10+
uses: actions/checkout@v3
11+
- name: Setup Node JS
12+
uses: actions/setup-node@v1
13+
with:
14+
node-version: '12.x'
15+
- name: Install dependencies
16+
run: npm install
17+
- name: Run tests
18+
run: npm run test

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# cURL to Postman Importer Changelog
22

3+
#### v1.4.0 (March 17, 2023)
4+
* Fixed issue [#7895](https://github.com/postmanlabs/postman-app-support/issues/7895) where cURL with no specific method defined for formdata type of body were not converted correctly.
5+
36
#### v1.3.0 (March 02, 2023)
47
* Fix for [#8087](https://github.com/postmanlabs/postman-app-support/issues/8087) - Add support to convert digest and NTLM auth types
58

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "curl-to-postmanv2",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "Convert a given CURL command to a Postman request",
55
"main": "index.js",
66
"com_postman_plugin": {

src/lib.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ var program,
8282
// checking if the user has mentioned any of these (-d, --data, --data-raw
8383
// --data-binary, --data-ascii) in curl command
8484
else if (curlObj.data.length > 0 || curlObj.dataAscii.length > 0 ||
85-
curlObj.dataUrlencode.length > 0 || curlObj.dataRaw.length > 0 || curlObj.dataBinary) {
85+
curlObj.dataUrlencode.length > 0 || curlObj.dataRaw.length > 0 ||
86+
curlObj.dataBinary || curlObj.form.length > 0) {
8687
return 'POST';
8788
}
8889
// set method to GET if no param is present
@@ -574,7 +575,8 @@ var program,
574575
dataRawString,
575576
dataAsciiString,
576577
dataUrlencode,
577-
formData;
578+
formData,
579+
isMethodGuessed = false;
578580

579581
try {
580582
sanitizedArgs = this.sanitizeArgs(cleanedCurlString);
@@ -601,6 +603,7 @@ var program,
601603
// if method is not given in the curl command
602604
if (!curlObj.request) {
603605
curlObj.request = this.getRequestMethod(curlObj);
606+
isMethodGuessed = true;
604607
}
605608

606609
curlObj.request = this.trimQuotesFromString(curlObj.request);
@@ -685,6 +688,12 @@ var program,
685688
});
686689
request.body.mode = 'formdata';
687690
request.body.formdata = this.parseFormBoundryData(formData, content_type);
691+
692+
/**
693+
* As we are parsing raw args here to detect form-data body, make sure we are also
694+
* defining method if not already defined in cURL
695+
*/
696+
(!_.isEmpty(request.body.formdata) && isMethodGuessed) && (request.method = 'POST');
688697
}
689698

690699
if (request.body.mode === 'formdata') {

test/conversion.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-len */
12
const largeRequest = require('./large-request');
23

34
var Converter = require('../src/lib'),
@@ -991,4 +992,81 @@ describe('Curl converter should', function() {
991992
});
992993
});
993994
});
995+
996+
describe('[Github #8296]: It should correctly generate request for form-data body', function() {
997+
998+
it('containing form-data boundry with correct method', function(done) {
999+
convert({
1000+
type: 'string',
1001+
data: `curl 'https://httpbin.org/anything' \
1002+
-H 'authority: httpbin.org' \
1003+
-H 'accept: application/json, text/plain, */*' \
1004+
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundaryDjpz6jUyMpfzNVCh' \
1005+
--data-raw $'------WebKitFormBoundaryDjpz6jUyMpfzNVCh\r\nContent-Disposition: form-data; name="hello"\r\n\r\nworld\r\n------WebKitFormBoundaryDjpz6jUyMpfzNVCh\r\nContent-Disposition: form-data; name="contact[phone]"\r\n\r\n12345\r\n------WebKitFormBoundaryDjpz6jUyMpfzNVCh\r\nContent-Disposition: form-data; name="data"; filename="wsdl1.wsdl"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundaryDjpz6jUyMpfzNVCh--\r\n' \
1006+
--compressed
1007+
`
1008+
}, function (err, result) {
1009+
expect(result.result).to.equal(true);
1010+
expect(result.output.length).to.equal(1);
1011+
expect(result.output[0].type).to.equal('request');
1012+
expect(result.output[0].data.url).to.equal('https://httpbin.org/anything');
1013+
expect(result.output[0].data.method).to.equal('POST');
1014+
expect(result.output[0].data.body.mode).to.equal('formdata');
1015+
expect(result.output[0].data.body.formdata).to.eql([
1016+
{
1017+
key: 'hello',
1018+
value: 'world',
1019+
type: 'text'
1020+
},
1021+
{
1022+
key: 'contact[phone]',
1023+
value: '12345',
1024+
type: 'text'
1025+
},
1026+
{
1027+
key: 'data',
1028+
value: 'wsdl1.wsdl',
1029+
type: 'file'
1030+
}
1031+
]);
1032+
done();
1033+
});
1034+
});
1035+
1036+
it('containing form params with correct method', function(done) {
1037+
convert({
1038+
type: 'string',
1039+
data: `curl --location "https://httpbin.org/anything" \
1040+
--form "hello=\"world\"" \
1041+
--form "contact[phone]=\"12345\"" \
1042+
--form "data=@\"wsdl1.wsdl\""
1043+
`
1044+
}, function (err, result) {
1045+
expect(result.result).to.equal(true);
1046+
expect(result.output.length).to.equal(1);
1047+
expect(result.output[0].type).to.equal('request');
1048+
expect(result.output[0].data.url).to.equal('https://httpbin.org/anything');
1049+
expect(result.output[0].data.method).to.equal('POST');
1050+
expect(result.output[0].data.body.mode).to.equal('formdata');
1051+
expect(result.output[0].data.body.formdata).to.eql([
1052+
{
1053+
key: 'hello',
1054+
value: 'world',
1055+
type: 'text'
1056+
},
1057+
{
1058+
key: 'contact[phone]',
1059+
value: '12345',
1060+
type: 'text'
1061+
},
1062+
{
1063+
key: 'data',
1064+
value: 'wsdl1.wsdl',
1065+
type: 'file'
1066+
}
1067+
]);
1068+
done();
1069+
});
1070+
});
1071+
});
9941072
});

0 commit comments

Comments
 (0)