diff --git a/codegens/golang/lib/index.js b/codegens/golang/lib/index.js index f2bca42da..8534fd89e 100644 --- a/codegens/golang/lib/index.js +++ b/codegens/golang/lib/index.js @@ -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 !== '') { diff --git a/codegens/golang/package.json b/codegens/golang/package.json index 8679be718..febf05364 100644 --- a/codegens/golang/package.json +++ b/codegens/golang/package.json @@ -26,7 +26,9 @@ "author": "Postman Labs ", "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" diff --git a/codegens/golang/test/unit/convert.test.js b/codegens/golang/test/unit/convert.test.js index 4859c85e6..c59a2b075 100644 --- a/codegens/golang/test/unit/convert.test.js +++ b/codegens/golang/test/unit/convert.test.js @@ -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"'); }); }); diff --git a/codegens/js-xhr/lib/index.js b/codegens/js-xhr/lib/index.js index 6249a5b66..4850eb90a 100644 --- a/codegens/js-xhr/lib/index.js +++ b/codegens/js-xhr/lib/index.js @@ -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'; diff --git a/codegens/js-xhr/package.json b/codegens/js-xhr/package.json index 0a30f64bd..a43f93da8 100644 --- a/codegens/js-xhr/package.json +++ b/codegens/js-xhr/package.json @@ -22,7 +22,9 @@ "author": "Postman Labs ", "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" }, diff --git a/codegens/nodejs-native/lib/request.js b/codegens/nodejs-native/lib/request.js index a281983ad..0b07557f0 100644 --- a/codegens/nodejs-native/lib/request.js +++ b/codegens/nodejs-native/lib/request.js @@ -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'); diff --git a/codegens/nodejs-native/test/unit/snippet.test.js b/codegens/nodejs-native/test/unit/snippet.test.js index 117b9523b..eb440ca6b 100644 --- a/codegens/nodejs-native/test/unit/snippet.test.js +++ b/codegens/nodejs-native/test/unit/snippet.test.js @@ -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'"); }); }); diff --git a/codegens/objective-c/lib/index.js b/codegens/objective-c/lib/index.js index 70978e5bf..2c3030af7 100644 --- a/codegens/objective-c/lib/index.js +++ b/codegens/objective-c/lib/index.js @@ -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`; diff --git a/codegens/objective-c/package.json b/codegens/objective-c/package.json index 7339bb5ba..5e3f0581e 100644 --- a/codegens/objective-c/package.json +++ b/codegens/objective-c/package.json @@ -26,7 +26,9 @@ "author": "Postman Labs ", "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" diff --git a/codegens/objective-c/test/unit/fixtures/snippets.json b/codegens/objective-c/test/unit/fixtures/snippets.json index f97ac73fa..edd515205 100644 --- a/codegens/objective-c/test/unit/fixtures/snippets.json +++ b/codegens/objective-c/test/unit/fixtures/snippets.json @@ -4,12 +4,12 @@ "Request Headers with disabled headers": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/headers\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSDictionary *headers = @{\n @\"my-sample-header\": @\"Lorem ipsum dolor sit amet\",\n @\"not-disabled-header\": @\"ENABLED\"\n};\n\n[request setAllHTTPHeaderFields:headers];\n\n[request setHTTPMethod:@\"GET\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "GET Request with disabled query": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/get?test=123&anotherone=232\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\n\n[request setHTTPMethod:@\"GET\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "POST form data with special characters": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSArray *parameters = @[\n @{ @\"name\": @\"pl\", @\"value\": @\"'a'\" }, \n @{ @\"name\": @\"qu\", @\"value\": @\"\\\"b\\\"\" }, \n @{ @\"name\": @\"hdjkljh\", @\"value\": @\"c\" }, \n @{ @\"name\": @\"sa\", @\"value\": @\"d\" }, \n @{ @\"name\": @\"Special\", @\"value\": @\"!@#$%&*()^_+=`~\" }, \n @{ @\"name\": @\"more\", @\"value\": @\",./';[]}{\\\":?><|\\\\\\\\\" } \n];\n\nNSString *boundary = @\"----WebKitFormBoundary7MA4YWxkTrZu0gW\";\nNSError *error;\nNSMutableString *body = [NSMutableString string];\n\nfor (NSDictionary *param in parameters) {\n [body appendFormat:@\"--%@\\r\\n\", boundary];\n [body appendFormat:@\"Content-Disposition:form-data; name=\\\"%@\\\"\\r\\n\\r\\n\", param[@\"name\"]];\n [body appendFormat:@\"%@\", param[@\"value\"]];\n}\n[body appendFormat:@\"\\r\\n--%@--\\r\\n\", boundary];\nNSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", - "Resolve URL (Quotes + Special Characters) Copy": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post?a=!@$%5E*()_-%60%2526&b=,./';%5B%5D%7D%7B%22:/?%3E%3C%7C%7C\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSData *postData = [[NSData alloc] initWithData:[@\"\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", + "Resolve URL (Quotes + Special Characters) Copy": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post?a=!@$^*()_-`%26&b=,./%27;[]}{%22:/?%3E%3C||\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSData *postData = [[NSData alloc] initWithData:[@\"\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "POST Raw Text": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSDictionary *headers = @{\n @\"Content-Type\": @\"text/plain\"\n};\n\n[request setAllHTTPHeaderFields:headers];\nNSData *postData = [[NSData alloc] initWithData:[@\"Duis posuere augue vel cursus pharetra. In luctus a ex nec pretium. Praesent neque quam, tincidunt nec leo eget, rutrum vehicula magna.\\nMaecenas consequat elementum elit, id semper sem tristique et. Integer pulvinar enim quis consectetur interdum volutpat.!@#$%^&*()+POL:},'';,[;[;\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "POST urlencoded data": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSDictionary *headers = @{\n @\"Content-Type\": @\"application/x-www-form-urlencoded\"\n};\n\n[request setAllHTTPHeaderFields:headers];\nNSMutableData *postData = [[NSMutableData alloc] initWithData:[@\"1='a'\" dataUsingEncoding:NSUTF8StringEncoding]];\n[postData appendData:[@\"&2=\\\"b\\\"\" dataUsingEncoding:NSUTF8StringEncoding]];\n[postData appendData:[@\"&'3'=c\" dataUsingEncoding:NSUTF8StringEncoding]];\n[postData appendData:[@\"&\\\"4\\\"=d\" dataUsingEncoding:NSUTF8StringEncoding]];\n[postData appendData:[@\"&Special=!@#$%&*()^_=`~ \" dataUsingEncoding:NSUTF8StringEncoding]];\n[postData appendData:[@\"&more=,./';[]}{\\\":?><|\\\\\\\\ \" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "POST json with raw": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSDictionary *headers = @{\n @\"Content-Type\": @\"application/json\"\n};\n\n[request setAllHTTPHeaderFields:headers];\nNSData *postData = [[NSData alloc] initWithData:[@\"{\\n \\\"json\\\": \\\"Test-Test!@#$%^&*()+POL:},'';,[;[;:>\\\"\\n}\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "POST javascript with raw": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSDictionary *headers = @{\n @\"Content-Type\": @\"application/javascript\"\n};\n\n[request setAllHTTPHeaderFields:headers];\nNSData *postData = [[NSData alloc] initWithData:[@\"var val = 6;\\nconsole.log(val);\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", - "Resolve URL": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post?a=''&b=%22%22\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSData *postData = [[NSData alloc] initWithData:[@\"\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", + "Resolve URL": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/post?a=%27%27&b=%22%22\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSData *postData = [[NSData alloc] initWithData:[@\"\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"POST\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "PUT Request": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/put\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSDictionary *headers = @{\n @\"Content-Type\": @\"text/plain\"\n};\n\n[request setAllHTTPHeaderFields:headers];\nNSData *postData = [[NSData alloc] initWithData:[@\"Etiam mi lacus, cursus vitae felis et, blandit pellentesque neque. Vestibulum eget nisi a tortor commodo dignissim.\\nQuisque ipsum ligula, faucibus a felis a, commodo elementum nisl. Mauris vulputate sapien et tincidunt viverra. Donec vitae velit nec metus.\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"PUT\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "PATCH Request": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/patch\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSDictionary *headers = @{\n @\"Content-Type\": @\"text/plain\"\n};\n\n[request setAllHTTPHeaderFields:headers];\nNSData *postData = [[NSData alloc] initWithData:[@\"Curabitur auctor, elit nec pulvinar porttitor, ex augue condimentum enim, eget suscipit urna felis quis neque.\\nSuspendisse sit amet luctus massa, nec venenatis mi. Suspendisse tincidunt massa at nibh efficitur fringilla. Nam quis congue mi. Etiam volutpat.\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"PATCH\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", "DELETE Request": "#import \n\ndispatch_semaphore_t sema = dispatch_semaphore_create(0);\n\nNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"https://postman-echo.com/delete\"]\n cachePolicy:NSURLRequestUseProtocolCachePolicy\n timeoutInterval:10.0];\nNSDictionary *headers = @{\n @\"Content-Type\": @\"text/plain\"\n};\n\n[request setAllHTTPHeaderFields:headers];\nNSData *postData = [[NSData alloc] initWithData:[@\"Donec fermentum, nisi sed cursus eleifend, nulla tortor ultricies tellus, ut vehicula orci arcu ut velit. In volutpat egestas dapibus.\\nMorbi condimentum vestibulum sapien. Etiam dignissim diam quis eros lobortis gravida vel lobortis est. Etiam gravida sed.\" dataUsingEncoding:NSUTF8StringEncoding]];\n[request setHTTPBody:postData];\n\n[request setHTTPMethod:@\"DELETE\"];\n\nNSURLSession *session = [NSURLSession sharedSession];\nNSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request\ncompletionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {\n if (error) {\n NSLog(@\"%@\", error);\n dispatch_semaphore_signal(sema);\n } else {\n NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;\n NSError *parseError = nil;\n NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];\n NSLog(@\"%@\",responseDictionary);\n dispatch_semaphore_signal(sema);\n }\n}];\n[dataTask resume];\ndispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);", diff --git a/codegens/ocaml-cohttp/lib/ocaml.js b/codegens/ocaml-cohttp/lib/ocaml.js index 3eca2a4ef..02c7a6f28 100644 --- a/codegens/ocaml-cohttp/lib/ocaml.js +++ b/codegens/ocaml-cohttp/lib/ocaml.js @@ -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') { diff --git a/codegens/ocaml-cohttp/package.json b/codegens/ocaml-cohttp/package.json index 54e641f59..5c0faa40d 100644 --- a/codegens/ocaml-cohttp/package.json +++ b/codegens/ocaml-cohttp/package.json @@ -25,7 +25,9 @@ "author": "Postman Labs ", "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" diff --git a/codegens/php-curl/lib/php-curl.js b/codegens/php-curl/lib/php-curl.js index c6a24339a..1ee3b0d80 100644 --- a/codegens/php-curl/lib/php-curl.js +++ b/codegens/php-curl/lib/php-curl.js @@ -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 = ' '${sanitize(finalUrl, 'url')}',\n`; diff --git a/codegens/php-curl/package.json b/codegens/php-curl/package.json index ff765d1f7..4a2927e70 100644 --- a/codegens/php-curl/package.json +++ b/codegens/php-curl/package.json @@ -26,7 +26,9 @@ "author": "Postman Labs ", "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" diff --git a/codegens/python-http.client/lib/python-httpclient.js b/codegens/python-http.client/lib/python-httpclient.js index 4c3530fcc..ec9a6eaba 100644 --- a/codegens/python-http.client/lib/python-httpclient.js +++ b/codegens/python-http.client/lib/python-httpclient.js @@ -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"))'; diff --git a/test/codegen/newman/fixtures/basicCollection.json b/test/codegen/newman/fixtures/basicCollection.json index 6af37012c..d07de8bfc 100644 --- a/test/codegen/newman/fixtures/basicCollection.json +++ b/test/codegen/newman/fixtures/basicCollection.json @@ -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",