parseRequest.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. var _ = require('../../lodash'), sanitize = require('./util').sanitize, path = require('path');
  3. /**
  4. * parses body of request and returns urlencoded string
  5. *
  6. * @param {Object} requestBody - json object respresenting body of request
  7. * @param {Boolean} trimFields - indicates whether to trim fields of body
  8. * @returns {String} - urlencoded string for request body
  9. */
  10. function parseUrlencode(requestBody, trimFields) {
  11. // reducing array of urlencoded form data to array of strings
  12. return _.reduce(requestBody[requestBody.mode], function (accumalator, data) {
  13. if (!data.disabled) {
  14. accumalator.push(`${sanitize(data.key, trimFields)}=${sanitize(data.value, trimFields)}`.replace(/&/g, '%26'));
  15. }
  16. return accumalator;
  17. }, []).join('&');
  18. }
  19. /**
  20. * parses body of request and creates java okhttp code snippet for adding form data
  21. *
  22. * @param {Object} requestBody - JSON object representing body of request
  23. * @param {String} indentString - string for indentation
  24. * @param {Boolean} trimFields - indicates whether to trim fields of body
  25. * @returns {String} - code snippet of java okhttp for multipart formdata
  26. */
  27. function parseFormData(requestBody, indentString, trimFields) {
  28. return _.reduce(requestBody[requestBody.mode], function (body, data) {
  29. if (data.disabled) {
  30. return body;
  31. }
  32. /* istanbul ignore next */
  33. if (data.type === 'file') {
  34. var pathArray = data.src.split(path.sep), fileName = pathArray[pathArray.length - 1];
  35. body += indentString + '.addFormDataPart' +
  36. `("${sanitize(data.key, trimFields)}","${sanitize(fileName, trimFields)}",\n` +
  37. indentString.repeat(2) + 'RequestBody.create(MediaType.parse("application/octet-stream"),\n' +
  38. indentString.repeat(2) + `new File("${sanitize(data.src)}")))\n`;
  39. }
  40. else {
  41. !data.value && (data.value = '');
  42. body += `${indentString}.addFormDataPart("${sanitize(data.key, trimFields)}",`;
  43. if (data.contentType) {
  44. body += ` null,\n${indentString.repeat(2)} RequestBody.create(MediaType.parse("${data.contentType}"),`;
  45. body += ` "${sanitize(data.value, trimFields)}".getBytes()))\n`;
  46. }
  47. else {
  48. body += `"${sanitize(data.value, trimFields)}")\n`;
  49. }
  50. }
  51. return body;
  52. }, '') + indentString + '.build()';
  53. }
  54. /**
  55. * parses request object and returns java okhttp code snippet for adding request body
  56. *
  57. * @param {Object} requestBody - JSON object representing body of request
  58. * @param {String} indentString - string for indentation
  59. * @param {Boolean} trimFields - indicates whether to trim fields of body
  60. * @returns {String} - code snippet of java okhttp parsed from request object
  61. */
  62. function parseBody(requestBody, indentString, trimFields) {
  63. if (!_.isEmpty(requestBody)) {
  64. switch (requestBody.mode) {
  65. case 'urlencoded':
  66. return 'RequestBody body = RequestBody.create(mediaType, ' +
  67. `"${parseUrlencode(requestBody, trimFields)}");\n`;
  68. case 'raw':
  69. return 'RequestBody body = RequestBody.create(mediaType, ' +
  70. `${JSON.stringify(requestBody[requestBody.mode])});\n`;
  71. case 'graphql':
  72. // eslint-disable-next-line no-case-declarations
  73. let query = requestBody[requestBody.mode].query, graphqlVariables;
  74. try {
  75. graphqlVariables = JSON.parse(requestBody[requestBody.mode].variables);
  76. }
  77. catch (e) {
  78. graphqlVariables = {};
  79. }
  80. return 'RequestBody body = RequestBody.create(mediaType, ' +
  81. `"${sanitize(JSON.stringify({
  82. query: query,
  83. variables: graphqlVariables
  84. }), trimFields)}");\n`;
  85. case 'formdata':
  86. return requestBody.formdata.length ?
  87. 'RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)\n' +
  88. `${parseFormData(requestBody, indentString, trimFields)};\n` :
  89. 'MediaType JSON = MediaType.parse("application/json; charset=utf-8");\n' +
  90. 'RequestBody body = RequestBody.create(JSON, "{}");\n';
  91. /* istanbul ignore next */
  92. case 'file':
  93. // return 'RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)\n' +
  94. // indentString + `.addFormDataPart("file", "${requestBody[requestBody.mode].src}",\n` +
  95. // indentString + 'RequestBody.create(MediaType.parse("application/octet-stream"),\n' +
  96. // indentString + `new File("${requestBody[requestBody.mode].src}"))).build();\n`;
  97. return 'RequestBody body = RequestBody.create(mediaType, "<file contents here>");\n';
  98. default:
  99. return 'RequestBody body = RequestBody.create(mediaType, "");\n';
  100. }
  101. }
  102. return 'RequestBody body = RequestBody.create(mediaType, "");\n';
  103. }
  104. /**
  105. * Parses header in Postman-SDK request and returns code snippet of java okhttp for adding headers
  106. *
  107. * @param {Object} request - Postman SDK request object
  108. * @param {String} indentString - indentation for code snippet
  109. * @returns {String} - code snippet for adding headers in java-okhttp
  110. */
  111. function parseHeader(request, indentString) {
  112. var headerArray = request.toJSON().header, headerSnippet = '';
  113. if (!_.isEmpty(headerArray)) {
  114. headerArray = _.reject(headerArray, 'disabled');
  115. headerSnippet += _.reduce(headerArray, function (accumalator, header) {
  116. accumalator += indentString + `.addHeader("${sanitize(header.key, true)}", ` +
  117. `"${sanitize(header.value)}")\n`;
  118. return accumalator;
  119. }, '');
  120. }
  121. return headerSnippet;
  122. }
  123. /**
  124. * returns content-type of request body if available else returns text/plain as default
  125. *
  126. * @param {Object} request - Postman SDK request object
  127. * @returns {String}- content-type of request body
  128. */
  129. function parseContentType(request) {
  130. if (request.body && request.body.mode === 'graphql') {
  131. return 'application/json';
  132. }
  133. return request.getHeaders({ enabled: true, ignoreCase: true })['content-type'] || 'text/plain';
  134. }
  135. module.exports = {
  136. parseBody: parseBody,
  137. parseHeader: parseHeader,
  138. parseContentType: parseContentType
  139. };