Skip to content
Permalink
Browse files
Merge pull request #455 from DM-Francis/develop
Fix encoding when generating HTTP code snippets
  • Loading branch information
webholik committed Feb 19, 2021
2 parents f458636 + 1173eaf commit 5e6ee152bf4cf10887a58318b091be828d92bcd6
Showing with 28 additions and 3 deletions.
  1. +26 −1 codegens/http/lib/util.js
  2. +2 −2 codegens/http/test/resources/expected-http-messages.json
@@ -117,6 +117,30 @@ function convertPropertyListToString (propertyList, joinUsing, includeDisabled =
}), joinUsing);
}

/**
* Url encodes the members of the property list.
*
* @param {Object} propertyList propertyList
* @param {String} joinUsing specify string that should be used to join the list of properties
* @param {Boolean} includeDisabled indicated whether or not to include disabled properties
* @param {Boolean} trimRequestBody indicates whether or not to trim request body
* @returns {String} Stringified and Url encoded property List
*/
function convertPropListToStringUrlEncoded (propertyList, joinUsing, includeDisabled = false, trimRequestBody = false) {
const properties = getMembersOfPropertyList(propertyList, includeDisabled),
keyvalues = [];

properties.forEach((property) => {
const key = trimRequestBody ? property.key.trim() : property.key,
value = trimRequestBody ? property.value.trim() : property.value,
keyvalue = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;

keyvalues.push(keyvalue);
});

return keyvalues.join(joinUsing);
}


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

@@ -7,7 +7,7 @@
"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",
"POST /post?a=!@$^*()_-`%26&b=,./';[]}{\":/?><|| HTTP/1.1\nHost: postman-echo.com",
"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",
"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=,./';[]}{\":?><|\\\\ ",
"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",
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/json\nContent-Length: 52\n\n{\n \"json\": \"Test-Test!@#$%^&*()+POL:},'';,[;[;:>\"\n}",
"POST /post HTTP/1.1\nHost: postman-echo.com\nContent-Type: application/javascript\nContent-Length: 30\n\nvar val = 6;\nconsole.log(val);",
"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 @@
"trimmedResult": [
"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:},'';,[;[;",
"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",
"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=,./';[]}{\":?><|\\\\"
"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"
]

}

0 comments on commit 5e6ee15

Please sign in to comment.