Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
VShingala committed Mar 17, 2023
2 parents b9c38f3 + d7a97e9 commit e5a84c2
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 4 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Test

on: [push, pull_request]

jobs:
Unit-Tests:
runs-on: ubuntu-20.04
steps:
- name: Get Code
uses: actions/checkout@v3
- name: Setup Node JS
uses: actions/setup-node@v1
with:
node-version: '12.x'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cURL to Postman Importer Changelog

#### v1.4.0 (March 17, 2023)
* 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.

#### v1.3.0 (March 02, 2023)
* Fix for [#8087](https://github.com/postmanlabs/postman-app-support/issues/8087) - Add support to convert digest and NTLM auth types

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "curl-to-postmanv2",
"version": "1.3.0",
"version": "1.4.0",
"description": "Convert a given CURL command to a Postman request",
"main": "index.js",
"com_postman_plugin": {
Expand Down
13 changes: 11 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ var program,
// checking if the user has mentioned any of these (-d, --data, --data-raw
// --data-binary, --data-ascii) in curl command
else if (curlObj.data.length > 0 || curlObj.dataAscii.length > 0 ||
curlObj.dataUrlencode.length > 0 || curlObj.dataRaw.length > 0 || curlObj.dataBinary) {
curlObj.dataUrlencode.length > 0 || curlObj.dataRaw.length > 0 ||
curlObj.dataBinary || curlObj.form.length > 0) {
return 'POST';
}
// set method to GET if no param is present
Expand Down Expand Up @@ -574,7 +575,8 @@ var program,
dataRawString,
dataAsciiString,
dataUrlencode,
formData;
formData,
isMethodGuessed = false;

try {
sanitizedArgs = this.sanitizeArgs(cleanedCurlString);
Expand All @@ -601,6 +603,7 @@ var program,
// if method is not given in the curl command
if (!curlObj.request) {
curlObj.request = this.getRequestMethod(curlObj);
isMethodGuessed = true;
}

curlObj.request = this.trimQuotesFromString(curlObj.request);
Expand Down Expand Up @@ -685,6 +688,12 @@ var program,
});
request.body.mode = 'formdata';
request.body.formdata = this.parseFormBoundryData(formData, content_type);

/**
* As we are parsing raw args here to detect form-data body, make sure we are also
* defining method if not already defined in cURL
*/
(!_.isEmpty(request.body.formdata) && isMethodGuessed) && (request.method = 'POST');
}

if (request.body.mode === 'formdata') {
Expand Down
78 changes: 78 additions & 0 deletions test/conversion.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-len */
const largeRequest = require('./large-request');

var Converter = require('../src/lib'),
Expand Down Expand Up @@ -991,4 +992,81 @@ describe('Curl converter should', function() {
});
});
});

describe('[Github #8296]: It should correctly generate request for form-data body', function() {

it('containing form-data boundry with correct method', function(done) {
convert({
type: 'string',
data: `curl 'https://httpbin.org/anything' \
-H 'authority: httpbin.org' \
-H 'accept: application/json, text/plain, */*' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundaryDjpz6jUyMpfzNVCh' \
--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' \
--compressed
`
}, 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');
expect(result.output[0].data.body.mode).to.equal('formdata');
expect(result.output[0].data.body.formdata).to.eql([
{
key: 'hello',
value: 'world',
type: 'text'
},
{
key: 'contact[phone]',
value: '12345',
type: 'text'
},
{
key: 'data',
value: 'wsdl1.wsdl',
type: 'file'
}
]);
done();
});
});

it('containing form params with correct method', function(done) {
convert({
type: 'string',
data: `curl --location "https://httpbin.org/anything" \
--form "hello=\"world\"" \
--form "contact[phone]=\"12345\"" \
--form "data=@\"wsdl1.wsdl\""
`
}, 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');
expect(result.output[0].data.body.mode).to.equal('formdata');
expect(result.output[0].data.body.formdata).to.eql([
{
key: 'hello',
value: 'world',
type: 'text'
},
{
key: 'contact[phone]',
value: '12345',
type: 'text'
},
{
key: 'data',
value: 'wsdl1.wsdl',
type: 'file'
}
]);
done();
});
});
});
});

0 comments on commit e5a84c2

Please sign in to comment.