Skip to content

Commit 5e6ee15

Browse files
authored
Merge pull request #455 from DM-Francis/develop
Fix encoding when generating HTTP code snippets
2 parents f458636 + 1173eaf commit 5e6ee15

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

codegens/http/lib/util.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ function convertPropertyListToString (propertyList, joinUsing, includeDisabled =
117117
}), joinUsing);
118118
}
119119

120+
/**
121+
* Url encodes the members of the property list.
122+
*
123+
* @param {Object} propertyList propertyList
124+
* @param {String} joinUsing specify string that should be used to join the list of properties
125+
* @param {Boolean} includeDisabled indicated whether or not to include disabled properties
126+
* @param {Boolean} trimRequestBody indicates whether or not to trim request body
127+
* @returns {String} Stringified and Url encoded property List
128+
*/
129+
function convertPropListToStringUrlEncoded (propertyList, joinUsing, includeDisabled = false, trimRequestBody = false) {
130+
const properties = getMembersOfPropertyList(propertyList, includeDisabled),
131+
keyvalues = [];
132+
133+
properties.forEach((property) => {
134+
const key = trimRequestBody ? property.key.trim() : property.key,
135+
value = trimRequestBody ? property.value.trim() : property.value,
136+
keyvalue = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
137+
138+
keyvalues.push(keyvalue);
139+
});
140+
141+
return keyvalues.join(joinUsing);
142+
}
143+
120144

121145
/**
122146
* Returns the request headers as a string
@@ -181,7 +205,8 @@ function getBody (request, trimRequestBody) {
181205
case URL_ENCODED:
182206
/* istanbul ignore else */
183207
if (!_.isEmpty(request.body[request.body.mode])) {
184-
requestBody += convertPropertyListToString(request.body[request.body.mode], '&', false, trimRequestBody);
208+
const propertyList = request.body[request.body.mode];
209+
requestBody += convertPropListToStringUrlEncoded(propertyList, '&', false, trimRequestBody);
185210
}
186211
return trimRequestBody ? requestBody.trim() : requestBody;
187212

codegens/http/test/resources/expected-http-messages.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Length: 586\nContent-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW\n\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"pl\"\n\n'a'\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"qu\"\n\n\"b\"\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"hdjkljh\"\n\nc\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"sa\"\n\nd\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"Special\"\n\n!@#$%&*()^_+=`~ \n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"more\"\n\n,./';[]}{\":?><|\\\\\n----WebKitFormBoundary7MA4YWxkTrZu0gW\n",
88
"POST /post?a=!@$^*()_-`%26&b=,./';[]}{\":/?><|| HTTP/1.1\nHost: postman-echo.com",
99
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/x-www-form-urlencoded\nContent-Length: 284\n\nDuis 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:},'';,[;[;\n\n\n",
10-
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/x-www-form-urlencoded\nContent-Length: 81\n\n1='a'&2=\"b\"&'3'=c&\"4\"=d&Special=!@%23$%%26*()^_=`~ &more=,./';[]}{\":?><|\\\\ ",
10+
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/x-www-form-urlencoded\nContent-Length: 147\n\n1='a'&2=%22b%22&'3'=c&%224%22=d&Special=!%40%23%24%25%26*()%5E_%3D%60~%20%20%20%20&more=%2C.%2F'%3B%5B%5D%7D%7B%22%3A%3F%3E%3C%7C%5C%5C%20%20%20%20",
1111
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/json\nContent-Length: 52\n\n{\n \"json\": \"Test-Test!@#$%^&*()+POL:},'';,[;[;:>\"\n}",
1212
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/javascript\nContent-Length: 30\n\nvar val = 6;\nconsole.log(val);",
1313
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: text/xml\nContent-Length: 48\n\n<xml>\n\tTest Test!@#$%^&*()+POL:},'';,[;[;\n</xml>",
@@ -33,7 +33,7 @@
3333
"trimmedResult": [
3434
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/x-www-form-urlencoded\nContent-Length: 281\n\nDuis 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:},'';,[;[;",
3535
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Length: 581\nContent-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW\n\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"pl\"\n\n'a'\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"qu\"\n\n\"b\"\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"hdjkljh\"\n\nc\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"sa\"\n\nd\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"Special\"\n\n!@#$%&*()^_+=`~\n----WebKitFormBoundary7MA4YWxkTrZu0gW\nContent-Disposition: form-data; name=\"more\"\n\n,./';[]}{\":?><|\\\\\n----WebKitFormBoundary7MA4YWxkTrZu0gW",
36-
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/x-www-form-urlencoded\nContent-Length: 73\n\n1='a'&2=\"b\"&'3'=c&\"4\"=d&Special=!@%23$%%26*()^_=`~&more=,./';[]}{\":?><|\\\\"
36+
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/x-www-form-urlencoded\nContent-Length: 123\n\n1='a'&2=%22b%22&'3'=c&%224%22=d&Special=!%40%23%24%25%26*()%5E_%3D%60~&more=%2C.%2F'%3B%5B%5D%7D%7B%22%3A%3F%3E%3C%7C%5C%5C"
3737
]
3838

3939
}

0 commit comments

Comments
 (0)