postman-code-generators
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -0,1 +1,7 @@ | ||
v1.1.2 (Dec 15, 2020) | ||
* Fix for - [8736](https://github.com/postmanlabs/postman-app-support/issues/8736) Add content type support for individual form-data fields | ||
* Fix for - [8635](https://github.com/postmanlabs/postman-app-support/issues/8635) Use Json.parse for all json like application types | ||
* Fix for - [9212](https://github.com/postmanlabs/postman-app-support/issues/9212) Add semicolon after header key in curl codegen if the value is empty string. | ||
* Add Newman test for powershell | ||
v1.1.1 (Nov 10, 2020) | ||
@@ -2,0 +8,0 @@ * Change string to enum in cURL quoteType option. |
@@ -72,3 +72,14 @@ var sanitize = require('./util').sanitize, | ||
_.forEach(headersData, (header) => { | ||
snippet += indent + `${form('-H', format)} '${sanitize(header.key, true)}: ${sanitize(header.value)}'`; | ||
if (!header.key) { | ||
return; | ||
} | ||
snippet += indent + `${form('-H', format)} '${sanitize(header.key, true)}`; | ||
// If the header value is an empty string then add a semicolon after key | ||
// otherwise the header would be ignored by curl | ||
if (header.value) { | ||
snippet += `: ${sanitize(header.value)}'`; | ||
} | ||
else { | ||
snippet += ';\''; | ||
} | ||
}); | ||
@@ -154,3 +165,7 @@ } | ||
snippet += indent + `${form('-F', format)}`; | ||
snippet += ` '${sanitize(data.key, trim)}="${sanitize(data.value, trim, true, true)}"'`; | ||
snippet += ` '${sanitize(data.key, trim)}="${sanitize(data.value, trim, true, true)}"`; | ||
if (data.contentType) { | ||
snippet += `;type=${data.contentType}`; | ||
} | ||
snippet += '\''; | ||
} | ||
@@ -157,0 +172,0 @@ } |
@@ -80,3 +80,3 @@ var _ = require('./lodash'), | ||
errFile${index + 1} := writer.CreateFormFile("${sanitize(data.key, trim)}",` + | ||
`filepath.Base("${data.src}"))\n`; | ||
`filepath.Base("${data.src}"))\n`; | ||
bodySnippet += `${indent}_, errFile${index + 1} = io.Copy(part${index + 1}, file)\n`; | ||
@@ -87,2 +87,12 @@ bodySnippet += `${indent}if errFile${index + 1} != nil {` + | ||
} | ||
else if (data.contentType) { | ||
bodySnippet += `\n${indent}mimeHeader${index + 1} := make(map[string][]string)\n`; | ||
bodySnippet += `${indent}mimeHeader${index + 1}["Content-Disposition"] = `; | ||
bodySnippet += `append(mimeHeader${index + 1}["Content-Disposition"], "form-data; `; | ||
bodySnippet += `name=\\"${sanitize(data.key, trim)}\\"")\n`; | ||
bodySnippet += `${indent}mimeHeader${index + 1}["Content-Type"] = append(`; | ||
bodySnippet += `mimeHeader${index + 1}["Content-Type"], "${data.contentType}")\n`; | ||
bodySnippet += `${indent}fieldWriter${index + 1}, _ := writer.CreatePart(mimeHeader${index + 1})\n`; | ||
bodySnippet += `${indent}fieldWriter${index + 1}.Write([]byte("${sanitize(data.value, trim)}"))\n\n`; | ||
} | ||
else { | ||
@@ -89,0 +99,0 @@ bodySnippet += `${indent}_ = writer.WriteField("${sanitize(data.key, trim)}",`; |
@@ -198,2 +198,5 @@ let _ = require('./lodash'), | ||
requestBody += `${(trimRequestBody ? property.key.trim() : property.key)}"\n`; | ||
if (property.contentType) { | ||
requestBody += `Content-Type: ${property.contentType}\n`; | ||
} | ||
requestBody += `\n${(trimRequestBody ? property.value.trim() : property.value)}\n`; | ||
@@ -200,0 +203,0 @@ } |
@@ -281,2 +281,37 @@ let testCollection = require('../resources/test-collection.json'), | ||
it('should add content type if formdata field contains a content-type', function () { | ||
var request = new Request({ | ||
'method': 'POST', | ||
'body': { | ||
'mode': 'formdata', | ||
'formdata': [ | ||
{ | ||
'key': 'json', | ||
'value': '{"hello": "world"}', | ||
'contentType': 'application/json', | ||
'type': 'text' | ||
} | ||
] | ||
}, | ||
'url': { | ||
'raw': 'http://postman-echo.com/post', | ||
'host': [ | ||
'postman-echo', | ||
'com' | ||
], | ||
'path': [ | ||
'post' | ||
] | ||
} | ||
}); | ||
convert(request, {}, function (error, snippet) { | ||
if (error) { | ||
expect.fail(null, null, error); | ||
} | ||
expect(snippet).to.be.a('string'); | ||
expect(snippet).to.contain('Content-Type: application/json'); | ||
}); | ||
}); | ||
it('should not add extra newlines if there is no body or header present', function () { | ||
@@ -283,0 +318,0 @@ var request = new Request({ |
@@ -47,4 +47,10 @@ | ||
!data.value && (data.value = ''); | ||
body += indentString + '.addFormDataPart' + | ||
`("${sanitize(data.key, trimFields)}", "${sanitize(data.value, trimFields)}")\n`; | ||
body += `${indentString}.addFormDataPart("${sanitize(data.key, trimFields)}",`; | ||
if (data.contentType) { | ||
body += ` null,\n${indentString.repeat(2)} RequestBody.create(MediaType.parse("${data.contentType}"),`; | ||
body += ` "${sanitize(data.value, trimFields)}".getBytes()))\n`; | ||
} | ||
else { | ||
body += `"${sanitize(data.value, trimFields)}")\n`; | ||
} | ||
} | ||
@@ -51,0 +57,0 @@ |
@@ -65,3 +65,7 @@ var _ = require('./lodash'), | ||
body += indentString + `.field("${sanitize(data.key, trimField)}", ` + | ||
`"${sanitize(data.value, trimField)}")\n`; | ||
`"${sanitize(data.value, trimField)}"`; | ||
if (data.contentType) { | ||
body += `, "${sanitize(data.contentType, trimField)}"`; | ||
} | ||
body += ')\n'; | ||
} | ||
@@ -105,3 +109,3 @@ return body; | ||
if (!formDataContent.includes('.field("file", new File')) { | ||
formDataContent = indentString + '.multiPartContent()' + formDataContent; | ||
formDataContent = indentString + '.multiPartContent()\n' + formDataContent; | ||
} | ||
@@ -108,0 +112,0 @@ return formDataContent; |
@@ -68,3 +68,6 @@ var _ = require('./lodash'), | ||
var bodySnippet = 'var raw = '; | ||
if (contentType === 'application/json') { | ||
// Match any application type whose underlying structure is json | ||
// For example application/vnd.api+json | ||
// All of them have +json as suffix | ||
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { | ||
try { | ||
@@ -71,0 +74,0 @@ let jsonBody = JSON.parse(body); |
@@ -23,3 +23,6 @@ var _ = require('../lodash'), | ||
if (!_.isEmpty(request.body[request.body.mode])) { | ||
if (contentType === 'application/json') { | ||
// Match any application type whose underlying structure is json | ||
// For example application/vnd.api+json | ||
// All of them have +json as suffix | ||
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { | ||
// eslint-disable-next-line max-depth | ||
@@ -26,0 +29,0 @@ try { |
@@ -33,3 +33,6 @@ var _ = require('./lodash'), | ||
var bodySnippet = 'var data = '; | ||
if (contentType === 'application/json') { | ||
// Match any application type whose underlying structure is json | ||
// For example application/vnd.api+json | ||
// All of them have +json as suffix | ||
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { | ||
try { | ||
@@ -36,0 +39,0 @@ let jsonBody = JSON.parse(body); |
@@ -174,2 +174,5 @@ var sanitize = require('./util').sanitize, | ||
snippet += indentString + `curl_mime_name(part, "${sanitize(data.key, trim)}");\n`; | ||
if (data.contentType) { | ||
snippet += indentString + `curl_mime_type(part, "${sanitize(data.contentType, trim)}");\n`; | ||
} | ||
snippet += indentString + | ||
@@ -176,0 +179,0 @@ `curl_mime_data(part, "${sanitize(data.value, trim)}", CURL_ZERO_TERMINATED);\n`; |
@@ -61,3 +61,7 @@ var _ = require('./lodash'), | ||
else { | ||
bodySnippet += `data.append('${sanitize(data.key, trim)}', '${sanitize(data.value, trim)}');\n`; | ||
bodySnippet += `data.append('${sanitize(data.key, trim)}', '${sanitize(data.value, trim)}'`; | ||
if (data.contentType) { | ||
bodySnippet += `, {contentType: '${sanitize(data.contentType, trim)}'}`; | ||
} | ||
bodySnippet += ');\n'; | ||
} | ||
@@ -80,3 +84,6 @@ } | ||
bodySnippet = varDeclare + ' data = '; | ||
if (contentType === 'application/json') { | ||
// Match any application type whose underlying structure is json | ||
// For example application/vnd.api+json | ||
// All of them have +json as suffix | ||
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { | ||
try { | ||
@@ -83,0 +90,0 @@ let jsonBody = JSON.parse(body); |
@@ -53,2 +53,36 @@ var expect = require('chai').expect, | ||
it('should use JSON.parse if the content-type is application/vnd.api+json', function () { | ||
request = new sdk.Request({ | ||
'method': 'POST', | ||
'header': [ | ||
{ | ||
'key': 'Content-Type', | ||
'value': 'application/vnd.api+json' | ||
} | ||
], | ||
'body': { | ||
'mode': 'raw', | ||
'raw': '{"data": {"hello": "world"} }' | ||
}, | ||
'url': { | ||
'raw': 'https://postman-echo.com/get', | ||
'protocol': 'https', | ||
'host': [ | ||
'postman-echo', | ||
'com' | ||
], | ||
'path': [ | ||
'get' | ||
] | ||
} | ||
}); | ||
convert(request, {}, function (error, snippet) { | ||
if (error) { | ||
expect.fail(null, null, error); | ||
} | ||
expect(snippet).to.be.a('string'); | ||
expect(snippet).to.contain('var data = JSON.stringify({"data":{"hello":"world"}});'); | ||
}); | ||
}); | ||
it('should return snippet with maxRedirects property set to ' + | ||
@@ -129,2 +163,37 @@ '0 for no follow redirect', function () { | ||
it('should add content type if formdata field contains a content-type', function () { | ||
request = new sdk.Request({ | ||
'method': 'POST', | ||
'body': { | ||
'mode': 'formdata', | ||
'formdata': [ | ||
{ | ||
'key': 'json', | ||
'value': '{"hello": "world"}', | ||
'contentType': 'application/json', | ||
'type': 'text' | ||
} | ||
] | ||
}, | ||
'url': { | ||
'raw': 'http://postman-echo.com/post', | ||
'host': [ | ||
'postman-echo', | ||
'com' | ||
], | ||
'path': [ | ||
'post' | ||
] | ||
} | ||
}); | ||
convert(request, {}, function (error, snippet) { | ||
if (error) { | ||
expect.fail(null, null, error); | ||
} | ||
expect(snippet).to.be.a('string'); | ||
expect(snippet).to.contain('data.append(\'json\', \'{"hello": "world"}\', {contentType: \'application/json\'});'); // eslint-disable-line max-len | ||
}); | ||
}); | ||
it('should return snippet with proper semicolon placed where required', function () { | ||
@@ -131,0 +200,0 @@ // testing for the below snippet |
@@ -60,3 +60,8 @@ const _ = require('./lodash'), | ||
const value = dataArrayElement.value.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); | ||
accumalator.push(`name=\\"${key}\\"\\r\\n\\r\\n${value}\\r\\n`); | ||
let field = `name=\\"${key}\\"\\r\\n`; | ||
if (dataArrayElement.contentType) { | ||
field += `Content-Type: ${dataArrayElement.contentType}\\r\\n`; | ||
} | ||
field += `\\r\\n${value}\\r\\n`; | ||
accumalator.push(field); | ||
} | ||
@@ -84,3 +89,6 @@ } | ||
case 'raw': | ||
if (contentType === 'application/json') { | ||
// Match any application type whose underlying structure is json | ||
// For example application/vnd.api+json | ||
// All of them have +json as suffix | ||
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { | ||
try { | ||
@@ -87,0 +95,0 @@ let jsonBody = JSON.parse(requestbody[requestbody.mode]); |
@@ -99,3 +99,6 @@ var _ = require('./lodash'), | ||
case 'raw': | ||
if (contentType === 'application/json') { | ||
// Match any application type whose underlying structure is json | ||
// For example application/vnd.api+json | ||
// All of them have +json as suffix | ||
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { | ||
try { | ||
@@ -102,0 +105,0 @@ let jsonBody = JSON.parse(requestbody[requestbody.mode]); |
@@ -63,3 +63,6 @@ var _ = require('./lodash'), | ||
case 'raw': | ||
if (contentType === 'application/json') { | ||
// Match any application type whose underlying structure is json | ||
// For example application/vnd.api+json | ||
// All of them have +json as suffix | ||
if (contentType && (contentType === 'application/json' || contentType.match(/\+json$/))) { | ||
try { | ||
@@ -66,0 +69,0 @@ let jsonBody = JSON.parse(requestbody[requestbody.mode]); |
@@ -86,3 +86,5 @@ var _ = require('./lodash'), | ||
const value = sanitize(data.value, 'formdata-value', trim); | ||
accumalator.push(`${indent}[| ("name", "${key}"); ("value", "${value}") |]`); | ||
accumalator.push(`${indent}[| ("name", "${key}"); ("value", "${value}")` + | ||
(data.contentType ? `; ("contentType", "${data.contentType}")` : '') + | ||
' |]'); | ||
} | ||
@@ -104,3 +106,9 @@ } | ||
bodySnippet += `${indent.repeat(2)}let (_, paramValue) = parameters.(x).(1) in\n`; | ||
bodySnippet += `${indent.repeat(2)}postData := !postData ^ accum ^ "\\r\\n\\r\\n" ^ paramValue ^ "\\r\\n";\n`; | ||
bodySnippet += `${indent.repeat(2)}postData := if Array.length parameters.(x) == 3 then (\n`; | ||
bodySnippet += `${indent.repeat(3)}let (_, contentType) = parameters.(x).(2) in\n`; | ||
bodySnippet += `${indent.repeat(3)}!postData ^ accum ^ "\\r\\n" ^ "Content-Type: " ^ contentType ^`; | ||
bodySnippet += ' "\\r\\n\\r\\n" ^ paramValue ^ "\\r\\n"\n'; | ||
bodySnippet += `${indent.repeat(2)}) else (\n`; | ||
bodySnippet += `${indent.repeat(3)}!postData ^ accum ^ "\\r\\n\\r\\n" ^ paramValue ^ "\\r\\n"\n`; | ||
bodySnippet += `${indent.repeat(2)});\n`; | ||
bodySnippet += `${indent})\n`; | ||
@@ -107,0 +115,0 @@ bodySnippet += `${indent}else if paramType = "fileName" then (\n`; |
@@ -23,3 +23,3 @@ var _ = require('./lodash'), | ||
function parseURLEncodedBody (body) { | ||
var bodySnippet = '$body = "', | ||
var bodySnippet = '', | ||
urlencodedArray = []; | ||
@@ -31,3 +31,5 @@ _.forEach(body, function (data) { | ||
}); | ||
bodySnippet += urlencodedArray.join('&') + '"\n'; | ||
if (urlencodedArray.length > 0) { | ||
bodySnippet = '$body = "' + urlencodedArray.join('&') + '"\n'; | ||
} | ||
return bodySnippet; | ||
@@ -44,3 +46,3 @@ } | ||
if (_.isEmpty(body)) { | ||
return '$body = $null\n'; | ||
return ''; | ||
} | ||
@@ -67,4 +69,6 @@ | ||
`$stringHeader.Name = "${sanitize(data.key, trim)}"\n` + | ||
`$StringContent = [System.Net.Http.StringContent]::new("${sanitize(data.value, trim)}")\n` + | ||
'$StringContent.Headers.ContentDisposition = $stringHeader\n' + | ||
`$stringContent = [System.Net.Http.StringContent]::new("${sanitize(data.value, trim)}")\n` + | ||
'$stringContent.Headers.ContentDisposition = $stringHeader\n' + | ||
(data.contentType ? '$contentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::new("' + | ||
data.contentType + '")\n$stringContent.Headers.ContentType = $contentType\n' : '') + | ||
'$multipartContent.Add($stringContent)\n\n'; | ||
@@ -293,8 +297,11 @@ } | ||
codeSnippet += `$response = Invoke-RestMethod '${request.url.toString().replace(/'/g, '\'\'')}' -Method '` + | ||
`${request.method}' -Headers $headers -Body $body`; | ||
`${request.method}' -Headers $headers`; | ||
} | ||
else { | ||
codeSnippet += `$response = Invoke-RestMethod '${request.url.toString()}' -CustomMethod ` + | ||
`'${request.method}' -Headers $headers -Body $body`; | ||
`'${request.method}' -Headers $headers`; | ||
} | ||
if (bodySnippet !== '') { | ||
codeSnippet += ' -Body $body'; | ||
} | ||
if (options.requestTimeout > 0) { | ||
@@ -301,0 +308,0 @@ // Powershell rest method accepts timeout in seconds |
@@ -14,3 +14,8 @@ /* eslint-disable no-useless-escape */ | ||
} | ||
inputString = inputString.replace(/\\/g, '\`\\').replace(/\"/g, '\`\"').replace(/\n/g, '\`n'); | ||
inputString = inputString | ||
.replace(/`/g, '``') | ||
.replace(/\$/g, '`$') | ||
.replace(/\\/g, '\`\\') | ||
.replace(/\"/g, '\`\"') | ||
.replace(/\n/g, '\`n'); | ||
return trim ? inputString.trim() : inputString; | ||
@@ -17,0 +22,0 @@ } |
@@ -10,3 +10,4 @@ #!/usr/bin/env node | ||
require('./test-lint'), | ||
require('./test-unit') | ||
require('./test-unit'), | ||
require('./test-newman') | ||
], function (code) { | ||
@@ -13,0 +14,0 @@ // eslint-disable-next-line max-len |
@@ -68,3 +68,4 @@ var _ = require('../lodash'), | ||
requestBody += `dataList.append(encode('Content-Disposition: form-data; name=${sanitize(data.key, 'form-data', true)};'))\n\n`; // eslint-disable-line max-len | ||
requestBody += 'dataList.append(encode(\'Content-Type: {}\'.format(\'multipart/form-data\')))\n'; | ||
requestBody += 'dataList.append(encode(\'Content-Type: {}\'.format(\'' + | ||
(data.contentType ? data.contentType : 'text/plain') + '\')))\n'; | ||
requestBody += 'dataList.append(encode(\'\'))\n\n'; | ||
@@ -71,0 +72,0 @@ requestBody += `dataList.append(encode("${sanitize(data.value, 'form-data', true)}"))\n`; |
@@ -102,3 +102,7 @@ var _ = require('./lodash'), | ||
parameter += `${indent.repeat(2)}"value": "${sanitize(data.value, mode, trim)}",\n`; | ||
parameter += `${indent.repeat(2)}"type": "text"\n${indent}]`; | ||
parameter += `${indent.repeat(2)}"type": "text"`; | ||
if (data.contentType) { | ||
parameter += `,\n${indent.repeat(2)}"contentType": "${sanitize(data.contentType, mode, trim)}"`; | ||
} | ||
parameter += `\n${indent}]`; | ||
} | ||
@@ -118,2 +122,5 @@ parameters.push(parameter); | ||
bodySnippet += `${indent.repeat(2)}body += "Content-Disposition:form-data; name=\\"\\(paramName)\\"\"\n`; | ||
bodySnippet += `${indent.repeat(2)}if param["contentType"] != nil {\n`; | ||
bodySnippet += `${indent.repeat(3)}body += "\\r\\nContent-Type: \\(param["contentType"] as! String)"\n`; | ||
bodySnippet += `${indent.repeat(2)}}\n`; | ||
bodySnippet += `${indent.repeat(2)}let paramType = param["type"] as! String\n`; | ||
@@ -120,0 +127,0 @@ bodySnippet += `${indent.repeat(2)}if paramType == "text" {\n`; |
{ | ||
"name": "postman-code-generators", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Generates code snippets for a postman collection", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
2411838
409
36440