Skip to content

replaced encodeURI with sdk.Url, issue #435 #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
6 changes: 5 additions & 1 deletion codegens/golang/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('./lodash'),
sdk = require('postman-collection'),
sanitize = require('./util').sanitize,
sanitizeMultiline = require('./util').sanitizeMultiline,
sanitizeOptions = require('./util').sanitizeOptions,
@@ -243,7 +244,10 @@ self = module.exports = {
}
codeSnippet += `${indent}"net/http"\n${indent}"io/ioutil"\n)\n\n`;

codeSnippet += `func main() {\n\n${indent}url := "${encodeURI(request.url.toString())}"\n`;
finalUrl = new sdk.Url(request.url.toString());
finalUrl = `${finalUrl.protocol ? `${finalUrl.protocol}://` : ''}${finalUrl.getRemote()}${finalUrl.getPathWithQuery(true)}`;

codeSnippet += `func main() {\n\n${indent}url := "${finalUrl}"\n`;
codeSnippet += `${indent}method := "${request.method}"\n\n`;

if (bodySnippet !== '') {
4 changes: 3 additions & 1 deletion codegens/golang/package.json
Original file line number Diff line number Diff line change
@@ -26,7 +26,9 @@
"author": "Postman Labs <help@getpostman.com>",
"license": "Apache-2.0",
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/golang",
"dependencies": {},
"dependencies": {
"postman-collection": "3.6.8"
},
"devDependencies": {},
"engines": {
"node": ">=8"
4 changes: 2 additions & 2 deletions codegens/golang/test/unit/convert.test.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ describe('Golang convert function', function () {
'method': 'GET',
'header': [],
'url': {
'raw': 'https://google.com',
'raw': 'https://google.com/',
'protocol': 'https',
'host': [
'google',
@@ -27,7 +27,7 @@ describe('Golang convert function', function () {
expect.fail(null, null, error);
}
expect(snippet).to.be.a('string');
expect(snippet).to.include('url := "https://google.com"');
expect(snippet).to.include('url := "https://google.com/"');
expect(snippet).to.include('method := "GET"');
});
});
6 changes: 5 additions & 1 deletion codegens/js-xhr/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('./lodash'),
sdk = require('postman-collection'),
sanitize = require('./util').sanitize,
sanitizeOptions = require('./util').sanitizeOptions,
addFormParam = require('./util').addFormParam,
@@ -271,7 +272,10 @@ function convert (request, options, callback) {
codeSnippet += `${indent.repeat(2)}console.log(this.responseText);\n`;
codeSnippet += `${indent}}\n});\n\n`;

codeSnippet += `xhr.open("${request.method}", "${encodeURI(request.url.toString())}");\n`;
finalUrl = new sdk.Url(request.url.toString());
finalUrl = `${finalUrl.protocol ? `${finalUrl.protocol}://` : ''}${finalUrl.getRemote()}${finalUrl.getPathWithQuery(true)}`;

codeSnippet += `xhr.open("${request.method}", "${finalUrl}");\n`;
if (options.requestTimeout) {
codeSnippet += `xhr.timeout = ${options.requestTimeout};\n`;
codeSnippet += 'xhr.addEventListener("ontimeout", function(e) {\n';
4 changes: 3 additions & 1 deletion codegens/js-xhr/package.json
Original file line number Diff line number Diff line change
@@ -22,7 +22,9 @@
"author": "Postman Labs <help@getpostman.com>",
"license": "Apache-2.0",
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/js-xhr",
"dependencies": {},
"dependencies": {
"postman-collection": "3.6.8"
},
"engines": {
"node": ">=8"
},
7 changes: 3 additions & 4 deletions codegens/nodejs-native/lib/request.js
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ function makeSnippet (request, indentString, options) {
snippet,
optionsArray = [],
postData = '',
url, host, path, query;
url, host, query;

if (options.ES6_enabled) {
snippet = 'const ';
@@ -132,9 +132,8 @@ function makeSnippet (request, indentString, options) {
}


url = sdk.Url.parse(request.url.toString());
url = new sdk.Url(request.url.toString());
host = url.host ? url.host.join('.') : '';
path = url.path ? '/' + url.path.join('/') : '/';
query = url.query ? _.reduce(url.query, (accum, q) => {
accum.push(`${q.key}=${q.value}`);
return accum;
@@ -152,7 +151,7 @@ function makeSnippet (request, indentString, options) {
if (url.port) {
optionsArray.push(`${indentString}'port': ${url.port}`);
}
optionsArray.push(`${indentString}'path': '${sanitize(path)}${sanitize(encodeURI(query))}'`);
optionsArray.push(`${indentString}'path': '${sanitize(url.getPathWithQuery(true))}'`);
optionsArray.push(parseRequest.parseHeader(request, indentString));
if (options.followRedirect) {
optionsArray.push(indentString + '\'maxRedirects\': 20');
2 changes: 1 addition & 1 deletion codegens/nodejs-native/test/unit/snippet.test.js
Original file line number Diff line number Diff line change
@@ -350,7 +350,7 @@ describe('nodejs-native convert function', function () {
}
expect(snippet).to.be.a('string');
// eslint-disable-next-line quotes
expect(snippet).to.include("'path': '/get?query1=b\\'b&query2=c%22c'");
expect(snippet).to.include("'path': '/get?query1=b%27b&query2=c%22c'");
});
});

7 changes: 6 additions & 1 deletion codegens/objective-c/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('./lodash'),
sdk = require('postman-collection'),
sanitizeOptions = require('./util').sanitizeOptions,
sanitize = require('./util').sanitize,
addFormParam = require('./util').addFormParam,
@@ -262,8 +263,12 @@ self = module.exports = {
footerSnippet += '}';
}
codeSnippet += 'dispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\n';

finalUrl = new sdk.Url(request.url.toString());
finalUrl = `${finalUrl.protocol ? `${finalUrl.protocol}://` : ''}${finalUrl.getRemote()}${finalUrl.getPathWithQuery(true)}`;

codeSnippet += 'NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' +
encodeURI(request.url.toString()) + '"]\n';
sanitize(request.url.toString()) + '"]\n';
codeSnippet += `${indent}cachePolicy:NSURLRequestUseProtocolCachePolicy\n`;
codeSnippet += `${indent}timeoutInterval:${requestTimeout}.0];\n`;

4 changes: 3 additions & 1 deletion codegens/objective-c/package.json
Original file line number Diff line number Diff line change
@@ -26,7 +26,9 @@
"author": "Postman Labs <help@getpostman.com>",
"license": "Apache-2.0",
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/objective-c",
"dependencies": {},
"dependencies": {
"postman-collection": "3.6.8"
},
"devDependencies": {},
"engines": {
"node": ">=8"
4 changes: 2 additions & 2 deletions codegens/objective-c/test/unit/fixtures/snippets.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion codegens/ocaml-cohttp/lib/ocaml.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('./lodash'),
sdk = require('postman-collection'),
sanitize = require('./util').sanitize,
sanitizeOptions = require('./util').sanitizeOptions,
addFormParam = require('./util').addFormParam,
@@ -313,7 +314,10 @@ self = module.exports = {
// timeout = options.requestTimeout;
// followRedirect = options.followRedirect;
trim = options.trimRequestBody;
finalUrl = encodeURI(request.url.toString());

finalUrl = new sdk.Url(request.url.toString());
finalUrl = `${finalUrl.protocol ? `${finalUrl.protocol}://` : ''}${finalUrl.getRemote()}${finalUrl.getPathWithQuery(true)}`;

methodArg = getMethodArg(request.method);
if (request.body && !request.headers.has('Content-Type')) {
if (request.body.mode === 'file') {
4 changes: 3 additions & 1 deletion codegens/ocaml-cohttp/package.json
Original file line number Diff line number Diff line change
@@ -25,7 +25,9 @@
"author": "Postman Labs <help@getpostman.com>",
"license": "Apache-2.0",
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/ocaml-cohttp",
"dependencies": {},
"dependencies": {
"postman-collection": "3.6.8"
},
"devDependencies": {},
"engines": {
"node": ">=8"
9 changes: 4 additions & 5 deletions codegens/php-curl/lib/php-curl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('./lodash'),
sdk = require('postman-collection'),
parseBody = require('./util/parseBody'),
sanitize = require('./util/sanitize').sanitize,
sanitizeOptions = require('./util/sanitize').sanitizeOptions,
@@ -105,11 +106,9 @@ self = module.exports = {
identity = options.indentType === 'Tab' ? '\t' : ' ';
indentation = identity.repeat(options.indentCount);
// concatenation and making up the final string
finalUrl = request.url.toString();
if (finalUrl !== encodeURI(finalUrl)) {
// needs to be encoded
finalUrl = encodeURI(finalUrl);
}
finalUrl = new sdk.Url(request.url.toString());
finalUrl = `${finalUrl.protocol ? `${finalUrl.protocol}://` : ''}${finalUrl.getRemote()}${finalUrl.getPathWithQuery(true)}`;

snippet = '<?php\n\n$curl = curl_init();\n\n';
snippet += 'curl_setopt_array($curl, array(\n';
snippet += `${indentation}CURLOPT_URL => '${sanitize(finalUrl, 'url')}',\n`;
4 changes: 3 additions & 1 deletion codegens/php-curl/package.json
Original file line number Diff line number Diff line change
@@ -26,7 +26,9 @@
"author": "Postman Labs <help@getpostman.com>",
"license": "Apache-2.0",
"homepage": "https://github.com/postmanlabs/code-generators/tree/master/codegens/php-curl",
"dependencies": {},
"dependencies": {
"postman-collection": "3.6.8"
},
"devDependencies": {},
"engines": {
"node": ">=8"
7 changes: 3 additions & 4 deletions codegens/python-http.client/lib/python-httpclient.js
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ self = module.exports = {
var snippet = '',
indentation = '',
identity = '',
url, host, path, query;
url, host, query;

if (_.isFunction(options)) {
callback = options;
@@ -113,9 +113,8 @@ self = module.exports = {
identity = options.indentType === 'Tab' ? '\t' : ' ';
indentation = identity.repeat(options.indentCount);

url = sdk.Url.parse(request.url.toString());
url = new sdk.Url(request.url.toString());
host = url.host ? url.host.join('.') : '';
path = url.path ? '/' + url.path.join('/') : '/';
query = url.query ? _.reduce(url.query, (accum, q) => {
accum.push(`${q.key}=${q.value}`);
return accum;
@@ -196,7 +195,7 @@ self = module.exports = {
}
snippet += getheaders(request, indentation);
snippet += `conn.request("${request.method}",` +
` "${sanitize(path)}${sanitize(encodeURI(query))}", payload, headers)\n`;
` "${sanitize(url.getPathWithQuery(true))}", payload, headers)\n`;
snippet += 'res = conn.getresponse()\n';
snippet += 'data = res.read()\n';
snippet += 'print(data.decode("utf-8"))';
4 changes: 2 additions & 2 deletions test/codegen/newman/fixtures/basicCollection.json
Original file line number Diff line number Diff line change
@@ -153,7 +153,7 @@
"method": "GET",
"header": [],
"url": {
"raw": "https://postman-echo.com/get?test=123&anotherone=232",
"raw": "https://postman-echo.com/get?test=123%3A%27&anotherone=232",
"protocol": "https",
"host": [
"postman-echo",
@@ -165,7 +165,7 @@
"query": [
{
"key": "test",
"value": "123"
"value": "123%3A'"
},
{
"key": "anotherone",