1
1
var _ = require ( './lodash' ) ,
2
+ sdk = require ( 'postman-collection' ) ,
2
3
sanitize = require ( './util/sanitize' ) . sanitize ,
3
4
sanitizeOptions = require ( './util/sanitize' ) . sanitizeOptions ,
4
5
addFormParam = require ( './util/sanitize' ) . addFormParam ,
@@ -35,24 +36,6 @@ function getheaders (request, indentation) {
35
36
return 'headers = {}\n' ;
36
37
}
37
38
38
- /**
39
- * Generates URL's path with query string
40
- *
41
- * @param {Object } requestUrl - Postman Sdk Request's Url object
42
- * @returns {String } - Url path with query (no host)
43
- */
44
- function getUrlPathWithQuery ( requestUrl ) {
45
- var path = sanitize ( requestUrl . getPath ( ) ) ,
46
- query = requestUrl . getQueryString ( { ignoreDisabled : true } ) ,
47
- urlPathWithQuery = '' ;
48
-
49
- urlPathWithQuery += ( path === '/' ? '' : path ) ;
50
- if ( query !== '' ) {
51
- urlPathWithQuery += '?' + sanitize ( query ) ;
52
- }
53
- return urlPathWithQuery ;
54
- }
55
-
56
39
self = module . exports = {
57
40
/**
58
41
* Used to return options which are specific to a particular plugin
@@ -115,7 +98,29 @@ self = module.exports = {
115
98
convert : function ( request , options , callback ) {
116
99
var snippet = '' ,
117
100
indentation = '' ,
118
- identity = '' ;
101
+ identity = '' ,
102
+ url , host , path , query ;
103
+
104
+ try {
105
+ url = sdk . Url . parse ( request . url ) ;
106
+ }
107
+ catch ( e ) {
108
+ url = request . url ;
109
+ }
110
+
111
+ host = url . host ? url . host . join ( '.' ) : '' ;
112
+ path = url . path ? '/' + url . path . join ( '/' ) : '/' ;
113
+ query = url . query ? _ . reduce ( url . query , ( accum , q ) => {
114
+ accum . push ( `${ q . key } =${ q . value } ` ) ;
115
+ return accum ;
116
+ } , [ ] ) : [ ] ;
117
+
118
+ if ( query . length > 0 ) {
119
+ query = '?' + query . join ( '&' ) ;
120
+ }
121
+ else {
122
+ query = '' ;
123
+ }
119
124
120
125
if ( _ . isFunction ( options ) ) {
121
126
callback = options ;
@@ -135,8 +140,8 @@ self = module.exports = {
135
140
snippet += 'from codecs import encode\n' ;
136
141
}
137
142
snippet += '\n' ;
138
- snippet += `conn = http.client.HTTPSConnection("${ sanitize ( request . url . host ? request . url . host . join ( '.' ) : '' ) } "` ;
139
- snippet += request . url . port ? `, ${ request . url . port } ` : '' ;
143
+ snippet += `conn = http.client.HTTPSConnection("${ sanitize ( host ) } "` ;
144
+ snippet += url . port ? `, ${ request . url . port } ` : '' ;
140
145
snippet += options . requestTimeout !== 0 ? `, timeout = ${ options . requestTimeout } )\n` : ')\n' ;
141
146
142
147
// The following code handles multiple files in the same formdata param.
@@ -196,7 +201,8 @@ self = module.exports = {
196
201
}
197
202
}
198
203
snippet += getheaders ( request , indentation ) ;
199
- snippet += `conn.request("${ request . method } ", "${ getUrlPathWithQuery ( request . url ) } ", payload, headers)\n` ;
204
+ snippet += `conn.request("${ request . method } ",` +
205
+ ` "${ sanitize ( path ) } ${ sanitize ( encodeURI ( query ) ) } ", payload, headers)\n` ;
200
206
snippet += 'res = conn.getresponse()\n' ;
201
207
snippet += 'data = res.read()\n' ;
202
208
snippet += 'print(data.decode("utf-8"))' ;
0 commit comments