From dda866a741f8f41d25a61e69b7ba638e66d89561 Mon Sep 17 00:00:00 2001 From: Boris Verkhovskiy Date: Thu, 21 Apr 2022 15:19:36 -0700 Subject: [PATCH] axios.post .put and .patch expect data as the second argument (#393) * axios.post .put .patch take data as the 2nd arg * rename formData to form * add warning about data not being sent * sort imports properly * passing an object as data sends it as json, not urlencoded --- src/generators/javascript/axios.ts | 395 ++++++++++++------ .../node-axios/delete_content_type.js | 15 +- .../node-axios/get_url_starting_with_http.js | 2 +- .../node-axios/get_with_all_numerical_data.js | 17 +- test/fixtures/node-axios/get_with_data2.js | 22 +- test/fixtures/node-axios/get_with_form.js | 28 +- .../node-axios/j_data_priority_than_f.js | 12 +- test/fixtures/node-axios/j_patch_array.js | 15 +- test/fixtures/node-axios/j_patch_file_only.js | 16 +- .../node-axios/j_patch_file_with_data.js | 20 +- .../node-axios/j_post_data_wo_verb.js | 12 +- test/fixtures/node-axios/j_post_form_f.js | 18 +- .../node-axios/multiline_post_with_data.js | 7 +- test/fixtures/node-axios/multipart_post.js | 22 +- .../node-axios/multipart_with_headers.js | 24 +- test/fixtures/node-axios/multiple_d_post.js | 15 +- test/fixtures/node-axios/patch.js | 29 +- .../post_basic_auth_url_encoded_data.js | 20 +- test/fixtures/node-axios/post_binary_file.js | 17 +- .../post_data_binary_with_equals.js | 67 +-- test/fixtures/node-axios/post_empty.js | 12 +- ..._escaped_double_quotes_in_single_quotes.js | 15 +- ..._escaped_single_quotes_in_double_quotes.js | 15 +- test/fixtures/node-axios/post_form.js | 18 +- test/fixtures/node-axios/post_image.js | 16 +- test/fixtures/node-axios/post_json.js | 21 +- .../node-axios/post_json_multiple_headers.js | 21 +- test/fixtures/node-axios/post_number.js | 15 +- .../node-axios/post_quotes_inside_data.js | 12 +- .../post_same_field_multiple_times.js | 15 +- .../node-axios/post_with_browser_headers.js | 28 +- .../node-axios/post_with_colon_in_header.js | 14 +- .../node-axios/post_with_data-ascii.js | 31 +- .../node-axios/post_with_data_binary.js | 15 +- .../fixtures/node-axios/post_with_data_raw.js | 15 +- .../post_with_data_with_percent_sign.js | 15 +- ...with_double_quotes_inside_single_quotes.js | 15 +- .../post_with_escaped_double_quotes.js | 15 +- .../node-axios/post_with_extra_whitespace.js | 30 +- .../node-axios/post_with_quotes_anywhere.js | 29 +- ...with_single_quotes_inside_double_quotes.js | 15 +- .../node-axios/post_with_urlencoded_data.js | 31 +- .../post_with_urlencoded_data_and_headers.js | 29 +- test/fixtures/node-axios/post_xpost.js | 15 +- .../node-axios/put_basic_auth_json_data.js | 23 +- test/fixtures/node-axios/put_with_T_option.js | 19 +- test/fixtures/node-axios/put_with_file.js | 15 +- test/fixtures/node-axios/put_xput.js | 19 +- .../node-axios/strange_http_method.js | 4 +- 49 files changed, 786 insertions(+), 524 deletions(-) diff --git a/src/generators/javascript/axios.ts b/src/generators/javascript/axios.ts index 5fc89f2..6ca8b69 100644 --- a/src/generators/javascript/axios.ts +++ b/src/generators/javascript/axios.ts @@ -42,25 +42,34 @@ const repr = (value: string | object, indentLevel?: number): string => { }; // TODO: @ -const getDataString = (request: Request): string | null => { +const _getDataString = (request: Request): [string | null, string | null] => { if (!request.data) { - return null; + return [null, null]; } + const originalStringRepr = repr(request.data); + const contentType = util.getContentType(request); + // can have things like ; charset=utf-8 which we want to preserve + const exactContentType = util.getHeader(request, "content-type"); if (contentType === "application/json") { - const originalStringRepr = repr(request.data); - const parsed = JSON.parse(request.data); - const backToString = JSON.stringify(parsed); - const jsonAsJavaScriptString = repr(parsed, 1); - - let result = ""; - if (request.data !== backToString) { - result += " // data: " + originalStringRepr + ",\n"; + // Only arrays and {} can be passed to axios to be encoded as JSON + // TODO: check this in other generators + if (typeof parsed !== "object" || parsed === null) { + return [originalStringRepr, null]; } - result += " data: JSON.stringify(" + jsonAsJavaScriptString + "),\n"; - return result; + const roundtrips = JSON.stringify(parsed) === request.data; + const jsonAsJavaScript = repr(parsed, 1); + if ( + roundtrips && + exactContentType === "application/json" && + util.getHeader(request, "accept") === "application/json, text/plain, */*" + ) { + util.deleteHeader(request, "content-type"); + util.deleteHeader(request, "accept"); + } + return [jsonAsJavaScript, roundtrips ? null : originalStringRepr]; } if (contentType === "application/x-www-form-urlencoded") { const query = util.parseQueryString(request.data); @@ -69,10 +78,165 @@ const getDataString = (request: Request): string | null => { queryDict && Object.values(queryDict).every((v) => typeof v === "string") ) { - return " data: " + repr(queryDict, 1) + ",\n"; + // Technically axios sends + // application/x-www-form-urlencoded;charset=utf-8 + if (exactContentType === "application/x-www-form-urlencoded") { + util.deleteHeader(request, "content-type"); + } + // TODO: check roundtrip, add a comment + return ["new URLSearchParams(" + repr(queryDict, 1) + ")", null]; + } else { + return [originalStringRepr, null]; } } - return null; + return [null, null]; +}; +const getDataString = (request: Request): [string | null, string | null] => { + if (!request.data) { + return [null, null]; + } + + let dataString = null; + let commentedOutDataString = null; + try { + [dataString, commentedOutDataString] = _getDataString(request); + } catch {} + if (!dataString) { + dataString = repr(request.data); + } + return [dataString, commentedOutDataString]; +}; + +const buildConfigObject = ( + request: Request, + method: string, + methods: string[], + dataMethods: string[], + hasSearchParams: boolean, + warnings: Warnings +): string => { + let code = "{\n"; + + if (!methods.includes(method)) { + // Axios uppercases methods + code += " method: " + repr(method) + ",\n"; + } + if (hasSearchParams) { + // code += " params,\n"; + code += " params: params,\n"; + } else if (request.queryDict) { + code += " params: " + repr(request.queryDict, 1) + ",\n"; + } + + const [dataString, commentedOutDataString] = getDataString(request); // can delete headers + + if ((request.headers && request.headers.length) || request.multipartUploads) { + code += " headers: {\n"; + if (request.multipartUploads) { + code += " ...form.getHeaders(),\n"; + } + for (const [key, value] of request.headers || []) { + code += " " + repr(key) + ": " + repr(value || "") + ",\n"; + } + if (code.endsWith(",\n")) { + code = code.slice(0, -2); + code += "\n"; + } + code += " },\n"; + } + + if (request.auth) { + const [username, password] = request.auth; + code += " auth: {\n"; + code += " username: " + repr(username); + if (password) { + code += ",\n"; + code += " password: " + repr(password) + "\n"; + } else { + code += "\n"; + } + code += " },\n"; + } + + if (!dataMethods.includes(method)) { + if (request.data) { + if (commentedOutDataString) { + code += " // data: " + commentedOutDataString + ",\n"; + } + code += " data: " + dataString + ",\n"; + } else if (request.multipartUploads) { + code += " data: form,\n"; + } + // the only other http method that sends data + if (method !== "options") { + warnings.push([ + "bad-method", + "axios doesn't send data: with " + method + " requests", + ]); + } + } + + if (request.timeout) { + const timeout = parseFloat(request.timeout); + if (!isNaN(timeout) && timeout > 0) { + code += " timeout: " + timeout * 1000 + ",\n"; + } + } + + if (request.proxy === "") { + // TODO: this probably won't be set if it's empty + // TODO: could have --socks5 proxy + code += " proxy: false,\n"; + } else if (request.proxy) { + // TODO: do this parsing in utils.ts + const proxy = request.proxy.includes("://") + ? request.proxy + : "http://" + request.proxy; + let [protocol, host] = proxy.split(/:\/\/(.*)/s, 2); + protocol = + protocol.toLowerCase() === "socks" ? "socks4" : protocol.toLowerCase(); + host = host ? host : ""; + + let port = "1080"; + const proxyPart = host.match(/:([0-9]+$)/); + if (proxyPart) { + host = host.slice(0, proxyPart.index); + port = proxyPart[1]; + } + const portInt = parseInt(port); + + code += " proxy: {\n"; + code += " protocol: " + repr(protocol) + ",\n"; + code += " host: " + repr(host) + ",\n"; + if (!isNaN(portInt)) { + code += " port: " + port + ",\n"; + } else { + code += " port: " + repr(port) + ",\n"; + } + if (request.proxyAuth) { + const [proxyUser, proxyPassword] = request.proxyAuth.split(/:(.*)/s, 2); + code += " auth: {\n"; + code += " user: " + repr(proxyUser); + if (proxyPassword !== undefined) { + code += ",\n"; + code += " password: " + repr(proxyPassword) + "\n"; + } else { + code += "\n"; + } + code += " },\n"; + } + if (code.endsWith(",\n")) { + code = code.slice(0, -2); + code += "\n"; + } + code += " },\n"; + } + + if (code.endsWith(",\n")) { + code = code.slice(0, -2); + } + code += "\n}"; + return code; }; export const _toNodeAxios = ( @@ -86,24 +250,6 @@ export const _toNodeAxios = ( let code = ""; - const needsConfig = - request.query || - request.queryDict || - request.headers || - request.auth || - request.data || - request.multipartUploads || - request.timeout || - request.proxy; - - // TODO: need to add http:// to URL? - const method = request.method.toLowerCase(); - if (method === "get" && !needsConfig) { - // TODO: is this actually helpful? - code = "const response = await axios(" + repr(request.url) + ");\n"; - return [importCode + "\n" + code, warnings]; - } - const hasSearchParams = request.query && (!request.queryDict || @@ -119,15 +265,15 @@ export const _toNodeAxios = ( } if (request.multipartUploads) { - imports.add(["form-data", "FormData"]); - code += "const formData = new FormData();\n"; + imports.add(["FormData", "form-data"]); + code += "const form = new FormData();\n"; for (const { name, filename, content, contentFile, } of request.multipartUploads) { - code += "formData.append(" + repr(name) + ", "; + code += "form.append(" + repr(name) + ", "; if (contentFile === "-") { code += "fs.readFileSync(0).toString()"; imports.add(["fs", "fs"]); @@ -145,125 +291,102 @@ export const _toNodeAxios = ( code += "\n"; } + const method = request.method.toLowerCase(); const methods = ["get", "delete", "head", "options", "post", "put", "patch"]; - const fn = methods.includes(method) ? method : "request"; - code += "const response = await axios." + fn + "("; + code += "const response = await axios"; + if (methods.includes(method)) { + code += "." + method; + } + code += "("; - code += repr( - request.queryDict || hasSearchParams ? request.urlWithoutQuery : request.url + const url = + request.queryDict || hasSearchParams + ? request.urlWithoutQuery + : request.url; + + // axios only supports posting data with these HTTP methods + // You can also post data with OPTIONS, but that has to go in the config object + const dataMethods = ["post", "put", "patch"]; + let needsConfig = !!( + request.query || + request.queryDict || + request.headers || + request.auth || + request.multipartUploads || + (request.data && !dataMethods.includes(method)) || + request.timeout || + request.proxy ); + const needsData = + dataMethods.includes(method) && + (request.data || request.multipartUploads || needsConfig); - if (fn === "request" || needsConfig) { - code += ", {\n"; - if (fn === "request") { - // Axios probably uppercases methods - code += " method: " + repr(request.method.toLowerCase()) + ",\n"; - } - if (hasSearchParams) { - // code += " params,\n"; - code += " params: params,\n"; - } else if (request.queryDict) { - code += " params: " + repr(request.queryDict, 1) + ",\n"; - } - - if (request.headers) { - code += - " headers: " + repr(Object.fromEntries(request.headers), 1) + ",\n"; - } - - if (request.auth) { - const [username, password] = request.auth; - code += " auth: {\n"; - code += " username: " + repr(username); - if (password) { - code += ",\n"; - code += " password: " + repr(password) + "\n"; - } else { - code += "\n"; - } - code += " },\n"; - } - + let dataString, commentedOutDataString; + if (needsData) { + code += "\n"; + code += " " + repr(url) + ",\n"; if (request.data) { try { - const dataString = getDataString(request); - if (dataString) { - code += dataString; - } else { - code += " data: " + repr(request.data) + ",\n"; + [dataString, commentedOutDataString] = getDataString(request); + if (!dataString) { + dataString = repr(request.data); } } catch { - code += " data: " + repr(request.data) + ",\n"; + dataString = repr(request.data); } + if (commentedOutDataString) { + code += " // " + commentedOutDataString + ",\n"; + } + code += " " + dataString; } else if (request.multipartUploads) { - code += " data: formData,\n"; + code += " form"; + } else if (needsConfig) { + // TODO: this works but maybe undefined would be more correct? + code += " ''"; } + } else { + code += repr(url); + } - if (request.timeout) { - const timeout = parseFloat(request.timeout); - if (!isNaN(timeout) && timeout > 0) { - code += " timeout: " + timeout * 1000 + ",\n"; + // getDataString() can delete a header, so we can end up with an empty config + needsConfig = !!( + request.query || + request.queryDict || + (request.headers && request.headers.length) || + request.auth || + request.multipartUploads || + (request.data && !dataMethods.includes(method)) || + request.timeout || + request.proxy + ); + + if (needsConfig) { + const config = buildConfigObject( + request, + method, + methods, + dataMethods, + !!hasSearchParams, + warnings + ); + if (needsData) { + code += ",\n"; + for (const line of config.split("\n")) { + code += " " + line + "\n"; } + } else { + code += ", "; + code += config; } - - if (request.proxy === "") { - // TODO: this probably won't be set if it's empty - // TODO: could have --socks5 proxy - code += " proxy: false,\n"; - } else if (request.proxy) { - // TODO: do this parsing in utils.ts - const proxy = request.proxy.includes("://") - ? request.proxy - : "http://" + request.proxy; - let [protocol, host] = proxy.split(/:\/\/(.*)/s, 2); - protocol = - protocol.toLowerCase() === "socks" ? "socks4" : protocol.toLowerCase(); - host = host ? host : ""; - - let port = "1080"; - const proxyPart = host.match(/:([0-9]+$)/); - if (proxyPart) { - host = host.slice(0, proxyPart.index); - port = proxyPart[1]; - } - const portInt = parseInt(port); - - code += " proxy: {\n"; - code += " protocol: " + repr(protocol) + ",\n"; - code += " host: " + repr(host) + ",\n"; - if (!isNaN(portInt)) { - code += " port: " + port + ",\n"; - } else { - code += " port: " + repr(port) + ",\n"; - } - if (request.proxyAuth) { - const [proxyUser, proxyPassword] = request.proxyAuth.split(/:(.*)/s, 2); - code += " auth: {\n"; - code += " user: " + repr(proxyUser); - if (proxyPassword !== undefined) { - code += ",\n"; - code += " password: " + repr(proxyPassword) + "\n"; - } else { - code += "\n"; - } - code += " },\n"; - } - if (code.endsWith(",\n")) { - code = code.slice(0, -2); - code += "\n"; - } - code += " },\n"; - } - - if (code.endsWith(",\n")) { - code = code.slice(0, -2); - } - code += "\n}"; + } else if (needsData) { + code += "\n"; } code += ");\n"; - for (const [imp, varName] of Array.from(imports).sort()) { + const bySecondElem = (a: [string, string], b: [string, string]): number => + a[1].localeCompare(b[1]); + for (const [varName, imp] of Array.from(imports).sort(bySecondElem)) { importCode += "const " + varName + " = require(" + repr(imp) + ");\n"; } diff --git a/test/fixtures/node-axios/delete_content_type.js b/test/fixtures/node-axios/delete_content_type.js index 1953b03..f7fd2e5 100644 --- a/test/fixtures/node-axios/delete_content_type.js +++ b/test/fixtures/node-axios/delete_content_type.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('localhost:28139', { - headers: { - 'Content-Type': 'application/json' - }, - data: JSON.stringify({}) -}); +const response = await axios.post( + 'localhost:28139', + {}, + { + headers: { + 'Content-Type': 'application/json' + } + } +); diff --git a/test/fixtures/node-axios/get_url_starting_with_http.js b/test/fixtures/node-axios/get_url_starting_with_http.js index 5a0c9fb..2dcd64d 100644 --- a/test/fixtures/node-axios/get_url_starting_with_http.js +++ b/test/fixtures/node-axios/get_url_starting_with_http.js @@ -1,3 +1,3 @@ const axios = require('axios'); -const response = await axios('httpbin.org/test'); +const response = await axios.get('httpbin.org/test'); diff --git a/test/fixtures/node-axios/get_with_all_numerical_data.js b/test/fixtures/node-axios/get_with_all_numerical_data.js index 64e7de6..e2d95ee 100644 --- a/test/fixtures/node-axios/get_with_all_numerical_data.js +++ b/test/fixtures/node-axios/get_with_all_numerical_data.js @@ -1,9 +1,12 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/CurlToNode', { - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/json' - }, - data: JSON.stringify(18233982904) -}); +const response = await axios.post( + 'http://localhost:28139/CurlToNode', + '18233982904', + { + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + } +); diff --git a/test/fixtures/node-axios/get_with_data2.js b/test/fixtures/node-axios/get_with_data2.js index f92e34b..5fc6670 100644 --- a/test/fixtures/node-axios/get_with_data2.js +++ b/test/fixtures/node-axios/get_with_data2.js @@ -1,12 +1,16 @@ const axios = require('axios'); -const response = await axios.put('http://localhost:28139/v2/alerts_policy_channels.json', { - params: { - 'policy_id': 'policy_id', - 'channel_ids': 'channel_id' - }, - headers: { - 'X-Api-Key': '{admin_api_key}', - 'Content-Type': 'application/json' +const response = await axios.put( + 'http://localhost:28139/v2/alerts_policy_channels.json', + '', + { + params: { + 'policy_id': 'policy_id', + 'channel_ids': 'channel_id' + }, + headers: { + 'X-Api-Key': '{admin_api_key}', + 'Content-Type': 'application/json' + } } -}); +); diff --git a/test/fixtures/node-axios/get_with_form.js b/test/fixtures/node-axios/get_with_form.js index c34e250..5468b9d 100644 --- a/test/fixtures/node-axios/get_with_form.js +++ b/test/fixtures/node-axios/get_with_form.js @@ -1,15 +1,21 @@ const axios = require('axios'); const FormData = require('form-data'); -const formData = new FormData(); -formData.append('from', 'test@tester.com'); -formData.append('to', 'devs@tester.net'); -formData.append('subject', 'Hello'); -formData.append('text', 'Testing the converter!'); +const form = new FormData(); +form.append('from', 'test@tester.com'); +form.append('to', 'devs@tester.net'); +form.append('subject', 'Hello'); +form.append('text', 'Testing the converter!'); -const response = await axios.post('http://localhost:28139/v3', { - auth: { - username: 'test' - }, - data: formData -}); +const response = await axios.post( + 'http://localhost:28139/v3', + form, + { + headers: { + ...form.getHeaders() + }, + auth: { + username: 'test' + } + } +); diff --git a/test/fixtures/node-axios/j_data_priority_than_f.js b/test/fixtures/node-axios/j_data_priority_than_f.js index 66e718c..6e82465 100644 --- a/test/fixtures/node-axios/j_data_priority_than_f.js +++ b/test/fixtures/node-axios/j_data_priority_than_f.js @@ -1,12 +1,10 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/post', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: { +const response = await axios.post( + 'http://localhost:28139/post', + new URLSearchParams({ 'data1': 'data1', 'data2': 'data2', 'data3': 'data3' - } -}); + }) +); diff --git a/test/fixtures/node-axios/j_patch_array.js b/test/fixtures/node-axios/j_patch_array.js index f9ce85b..aa81065 100644 --- a/test/fixtures/node-axios/j_patch_array.js +++ b/test/fixtures/node-axios/j_patch_array.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.patch('http://localhost:28139/patch', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'item[]=1&item[]=2&item[]=3' -}); +const response = await axios.patch( + 'http://localhost:28139/patch', + 'item[]=1&item[]=2&item[]=3', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/j_patch_file_only.js b/test/fixtures/node-axios/j_patch_file_only.js index 5647cf2..d1ff86b 100644 --- a/test/fixtures/node-axios/j_patch_file_only.js +++ b/test/fixtures/node-axios/j_patch_file_only.js @@ -2,9 +2,15 @@ const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs'); -const formData = new FormData(); -formData.append('file1', fs.readFileSync('./fixtures/curl_commands/delete.sh'), './fixtures/curl_commands/delete.sh'); +const form = new FormData(); +form.append('file1', fs.readFileSync('./fixtures/curl_commands/delete.sh'), './fixtures/curl_commands/delete.sh'); -const response = await axios.patch('http://localhost:28139/patch', { - data: formData -}); +const response = await axios.patch( + 'http://localhost:28139/patch', + form, + { + headers: { + ...form.getHeaders() + } + } +); diff --git a/test/fixtures/node-axios/j_patch_file_with_data.js b/test/fixtures/node-axios/j_patch_file_with_data.js index 373a744..f36ce23 100644 --- a/test/fixtures/node-axios/j_patch_file_with_data.js +++ b/test/fixtures/node-axios/j_patch_file_with_data.js @@ -2,11 +2,17 @@ const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs'); -const formData = new FormData(); -formData.append('file1', fs.readFileSync('./fixtures/curl_commands/delete.sh'), './fixtures/curl_commands/delete.sh'); -formData.append('form1', 'form+data+1'); -formData.append('form2', 'form_data_2'); +const form = new FormData(); +form.append('file1', fs.readFileSync('./fixtures/curl_commands/delete.sh'), './fixtures/curl_commands/delete.sh'); +form.append('form1', 'form+data+1'); +form.append('form2', 'form_data_2'); -const response = await axios.patch('http://localhost:28139/patch', { - data: formData -}); +const response = await axios.patch( + 'http://localhost:28139/patch', + form, + { + headers: { + ...form.getHeaders() + } + } +); diff --git a/test/fixtures/node-axios/j_post_data_wo_verb.js b/test/fixtures/node-axios/j_post_data_wo_verb.js index 66e718c..6e82465 100644 --- a/test/fixtures/node-axios/j_post_data_wo_verb.js +++ b/test/fixtures/node-axios/j_post_data_wo_verb.js @@ -1,12 +1,10 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/post', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: { +const response = await axios.post( + 'http://localhost:28139/post', + new URLSearchParams({ 'data1': 'data1', 'data2': 'data2', 'data3': 'data3' - } -}); + }) +); diff --git a/test/fixtures/node-axios/j_post_form_f.js b/test/fixtures/node-axios/j_post_form_f.js index f8695fb..9417d36 100644 --- a/test/fixtures/node-axios/j_post_form_f.js +++ b/test/fixtures/node-axios/j_post_form_f.js @@ -1,10 +1,16 @@ const axios = require('axios'); const FormData = require('form-data'); -const formData = new FormData(); -formData.append('d1', 'data1'); -formData.append('d2', 'data'); +const form = new FormData(); +form.append('d1', 'data1'); +form.append('d2', 'data'); -const response = await axios.post('http://localhost:28139/post', { - data: formData -}); +const response = await axios.post( + 'http://localhost:28139/post', + form, + { + headers: { + ...form.getHeaders() + } + } +); diff --git a/test/fixtures/node-axios/multiline_post_with_data.js b/test/fixtures/node-axios/multiline_post_with_data.js index 69dc515..6dd87d4 100644 --- a/test/fixtures/node-axios/multiline_post_with_data.js +++ b/test/fixtures/node-axios/multiline_post_with_data.js @@ -2,11 +2,10 @@ const axios = require('axios'); const response = await axios.get('http://localhost:28139/echo/html/', { headers: { - 'Origin': 'http://fiddle.jshell.net', - 'Content-Type': 'application/x-www-form-urlencoded' + 'Origin': 'http://fiddle.jshell.net' }, - data: { + data: new URLSearchParams({ 'msg1': 'value1', 'msg2': 'value2' - } + }) }); diff --git a/test/fixtures/node-axios/multipart_post.js b/test/fixtures/node-axios/multipart_post.js index cd9120a..24da910 100644 --- a/test/fixtures/node-axios/multipart_post.js +++ b/test/fixtures/node-axios/multipart_post.js @@ -2,13 +2,17 @@ const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs'); -const formData = new FormData(); -formData.append('attributes', '{"name":"tigers.jpeg", "parent":{"id":"11446498"}}'); -formData.append('file', fs.readFileSync('myfile.jpg'), 'myfile.jpg'); +const form = new FormData(); +form.append('attributes', '{"name":"tigers.jpeg", "parent":{"id":"11446498"}}'); +form.append('file', fs.readFileSync('myfile.jpg'), 'myfile.jpg'); -const response = await axios.post('https://localhost:28139/api/2.0/files/content', { - headers: { - 'Authorization': 'Bearer ACCESS_TOKEN' - }, - data: formData -}); +const response = await axios.post( + 'https://localhost:28139/api/2.0/files/content', + form, + { + headers: { + ...form.getHeaders(), + 'Authorization': 'Bearer ACCESS_TOKEN' + } + } +); diff --git a/test/fixtures/node-axios/multipart_with_headers.js b/test/fixtures/node-axios/multipart_with_headers.js index 10a3279..2d99ffa 100644 --- a/test/fixtures/node-axios/multipart_with_headers.js +++ b/test/fixtures/node-axios/multipart_with_headers.js @@ -2,14 +2,18 @@ const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs'); -const formData = new FormData(); -formData.append('attributes', '{"name":"tigers.jpeg", "parent":{"id":"11446498"}}'); -formData.append('file', fs.readFileSync('myfile.jpg'), 'myfile.jpg'); +const form = new FormData(); +form.append('attributes', '{"name":"tigers.jpeg", "parent":{"id":"11446498"}}'); +form.append('file', fs.readFileSync('myfile.jpg'), 'myfile.jpg'); -const response = await axios.post('https://localhost:28139/api/2.0/files/content', { - headers: { - 'Authorization': 'Bearer ACCESS_TOKEN', - 'X-Nice': 'Header' - }, - data: formData -}); +const response = await axios.post( + 'https://localhost:28139/api/2.0/files/content', + form, + { + headers: { + ...form.getHeaders(), + 'Authorization': 'Bearer ACCESS_TOKEN', + 'X-Nice': 'Header' + } + } +); diff --git a/test/fixtures/node-axios/multiple_d_post.js b/test/fixtures/node-axios/multiple_d_post.js index 121ac39..c462590 100644 --- a/test/fixtures/node-axios/multiple_d_post.js +++ b/test/fixtures/node-axios/multiple_d_post.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('https://localhost:28139/webservices/rest.php', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'version=1.2&auth_user=fdgxf&auth_pwd=oxfdscds&json_data={ "operation": "core/get", "class": "Software", "key": "key" }' -}); +const response = await axios.post( + 'https://localhost:28139/webservices/rest.php', + 'version=1.2&auth_user=fdgxf&auth_pwd=oxfdscds&json_data={ "operation": "core/get", "class": "Software", "key": "key" }', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/patch.js b/test/fixtures/node-axios/patch.js index ea43936..3cce9f3 100644 --- a/test/fixtures/node-axios/patch.js +++ b/test/fixtures/node-axios/patch.js @@ -1,16 +1,9 @@ const axios = require('axios'); -const response = await axios.patch('http://localhost:28139/go/api/agents/adb9540a-b954-4571-9d9b-2f330739d4da', { - headers: { - 'Accept': 'application/vnd.go.cd.v4+json', - 'Content-Type': 'application/json' - }, - auth: { - username: 'username', - password: 'password' - }, - // data: '{\n "hostname": "agent02.example.com",\n "agent_config_state": "Enabled",\n "resources": ["Java","Linux"],\n "environments": ["Dev"]\n }', - data: JSON.stringify({ +const response = await axios.patch( + 'http://localhost:28139/go/api/agents/adb9540a-b954-4571-9d9b-2f330739d4da', + // '{\n "hostname": "agent02.example.com",\n "agent_config_state": "Enabled",\n "resources": ["Java","Linux"],\n "environments": ["Dev"]\n }', + { 'hostname': 'agent02.example.com', 'agent_config_state': 'Enabled', 'resources': [ @@ -20,5 +13,15 @@ const response = await axios.patch('http://localhost:28139/go/api/agents/adb9540 'environments': [ 'Dev' ] - }) -}); + }, + { + headers: { + 'Accept': 'application/vnd.go.cd.v4+json', + 'Content-Type': 'application/json' + }, + auth: { + username: 'username', + password: 'password' + } + } +); diff --git a/test/fixtures/node-axios/post_basic_auth_url_encoded_data.js b/test/fixtures/node-axios/post_basic_auth_url_encoded_data.js index 4d56fc7..4820ede 100644 --- a/test/fixtures/node-axios/post_basic_auth_url_encoded_data.js +++ b/test/fixtures/node-axios/post_basic_auth_url_encoded_data.js @@ -1,14 +1,14 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/api/oauth/token/', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - auth: { - username: 'foo', - password: 'bar' - }, - data: { +const response = await axios.post( + 'http://localhost:28139/api/oauth/token/', + new URLSearchParams({ 'grant_type': 'client_credentials' + }), + { + auth: { + username: 'foo', + password: 'bar' + } } -}); +); diff --git a/test/fixtures/node-axios/post_binary_file.js b/test/fixtures/node-axios/post_binary_file.js index b2bef63..8ca47fa 100644 --- a/test/fixtures/node-axios/post_binary_file.js +++ b/test/fixtures/node-axios/post_binary_file.js @@ -1,9 +1,12 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/american-art/query', { - headers: { - 'Content-type': 'application/sparql-query', - 'Accept': 'application/sparql-results+json' - }, - data: '@./sample.sparql' -}); +const response = await axios.post( + 'http://localhost:28139/american-art/query', + '@./sample.sparql', + { + headers: { + 'Content-type': 'application/sparql-query', + 'Accept': 'application/sparql-results+json' + } + } +); diff --git a/test/fixtures/node-axios/post_data_binary_with_equals.js b/test/fixtures/node-axios/post_data_binary_with_equals.js index e80a0ca..3302791 100644 --- a/test/fixtures/node-axios/post_data_binary_with_equals.js +++ b/test/fixtures/node-axios/post_data_binary_with_equals.js @@ -1,35 +1,8 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/api/service.svc', { - params: { - 'action': 'CreateItem', - 'ID': '-37', - 'AC': '1' - }, - headers: { - 'Cookie': 'X-BackEndCookie=S-1-5-21-1234556-56678-12345-2345=alphanumericstring12345/anotheralphanumericstring12345/scsiAdf/P; ClientId=LoremIupsum; PrivateComputer=true; PBack=0; cadata=bx88rrCBehITlBWSozO2l2hlFGu//JjT1/k6dewX5shV32jANUZSMU6GR+M25B6YpBODEgXzxfIHDnvxNC6SJoaE/d8RWX3uDnbkd+m91jNhMXNSYIRYTJHVFdPG06AE; cadataTTL=NfDhBViTJMUdC+ir+6BYvg==; cadataKey=qUY+OLTD9V14CFK6/CUPyrJWMxl1FFqZFjB8/qcS0/q55eqGvP9bWvX+XuSYVv3hIGAn9QNPhIDK6NP9LwCBdu25f2BUFDUWJruGO8MW02izSWzRUnni00xWQq3Y3nNKvpvO+OIR641BPHVZ0+lzCw2Wt8uzEnryCWAjlleozF/XWjpTN4/AaTmcIjEZUDN+fo4494rD0mADtEHv2gmd5mhLe+iyii/L9nAB3UuiJomwbRbKgy22Tj8cyavmLC4ZaViqW9E102NOLU4FYLgdZVET+mbdg==; cadataIV=bTM88YL1zmz7FsBEB0y3nI2SrdSTy+KLxCpx2FRfIZYFo2spN1IHQMSCT76OXrg79sVPhyqXk+N9rOj6M9KsQl4KqMNVBcoXgp24POpgoTwd4FBmKtAYbd9SDErna3jrMO168ML9PDG18K3CnBf6YG1tsIs0gXOEP9LzHVmUPF7KCKqUFiOiZGWuwmPhl85eo77BbEpVN2JkPnzuQWn6tC0cY4f2cJDlr3Z23SrAUVwwXmgRg2DXfOF5MIEkpwYiiI6sABCD9rsSnE6zTXlvZg33hjiD/ywUV1ZWjI2M/4zBixa4s150+dOnMmvtEFs/nOMnvMJui4PEDlTA==; cadataSig=WL3hB+av7sO3bzjL+Efe5b4exnvQxSInH3U5jDvfnPcttSp0XUF3y/NB573C0CTBYuOH/40smFssXlrKhT9tG2ITivdSIIamOmarmC8XwFOv9qQIFMHofcO/jjRDMqF0qRk7WBAC2FgBQrf2Tvq7wk5IX/JHn6zhlgKALAAqH9L9JNC244etnjj9YNaMDYEHV2M2jVTu3FsELqw1rSSqp0hEBlh+aFBvYCBg5hS1mVI76ZCHZVa0OUejiH2yiZyJIKHUI+Sv0rpU3iiQNtIFmGEdwhoo/rga4s4Dc2UsJLQ8c0yGlZgflYs+7Q5gPr74/mTUin60ej/w3M0roUl3FQ==; UC=d8be544621964f3c9865b3ee872fd432; AppcacheVer=15.0.1236.3:en-usbase; X-OWA-CANARY=VOXQP6xtGkiNnv7E4rFt8TrmclqVFtQI4IJqZflrR7Wz9AMPkMsFoyAlquw1YGsTUxIkVouAcvk.', - 'Origin': 'https://nih.mail.edu.fr', - 'Accept-Encoding': 'gzip, deflate, br', - 'X-EWS-TargetVersion': '2.5', - 'Accept-Language': 'en-US,en;q=0.8', - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36', - 'Action': 'CreateItem', - 'X-Requested-With': 'XMLHttpRequest', - 'Connection': 'keep-alive', - 'X-OWA-CANARY': 'VOXQP6xtGkiNnv7E4rFt8TrmclqVFtQI4IJqZflrR7Wz9AMPkMsFoyAlquw1YGsTUxIkVouAcvk.', - 'X-OWA-ActionName': 'CreateMessageForComposeSend', - 'X-OWA-ActionId': '-37', - 'X-OWA-ServiceUnavailableOnTransientError': 'true', - 'Content-Type': 'application/json; charset=UTF-8', - 'Accept': '*/*', - 'Referer': 'https://localhost/api/', - 'X-OWA-ClientBuildVersion': '15.0.1236.3', - 'X-OWA-CorrelationId': '2f11f8fb-f6c6-43a5-881d-8a1b242a4e70_148023102251337', - 'DNT': '1', - 'X-OWA-ClientBegin': '2016-11-27T07:17:02.513', - 'X-OWA-Attempt': '1' - }, - data: JSON.stringify({ +const response = await axios.post( + 'http://localhost:28139/api/service.svc', + { '__type': 'CreateItemJsonRequest:#Exchange', 'Header': { '__type': 'JsonRequestHeaders:#Exchange', @@ -77,5 +50,35 @@ const response = await axios.post('http://localhost:28139/api/service.svc', { 'MessageDisposition': 'SendAndSaveCopy', 'ComposeOperation': 'newMail' } - }) -}); + }, + { + params: { + 'action': 'CreateItem', + 'ID': '-37', + 'AC': '1' + }, + headers: { + 'Cookie': 'X-BackEndCookie=S-1-5-21-1234556-56678-12345-2345=alphanumericstring12345/anotheralphanumericstring12345/scsiAdf/P; ClientId=LoremIupsum; PrivateComputer=true; PBack=0; cadata=bx88rrCBehITlBWSozO2l2hlFGu//JjT1/k6dewX5shV32jANUZSMU6GR+M25B6YpBODEgXzxfIHDnvxNC6SJoaE/d8RWX3uDnbkd+m91jNhMXNSYIRYTJHVFdPG06AE; cadataTTL=NfDhBViTJMUdC+ir+6BYvg==; cadataKey=qUY+OLTD9V14CFK6/CUPyrJWMxl1FFqZFjB8/qcS0/q55eqGvP9bWvX+XuSYVv3hIGAn9QNPhIDK6NP9LwCBdu25f2BUFDUWJruGO8MW02izSWzRUnni00xWQq3Y3nNKvpvO+OIR641BPHVZ0+lzCw2Wt8uzEnryCWAjlleozF/XWjpTN4/AaTmcIjEZUDN+fo4494rD0mADtEHv2gmd5mhLe+iyii/L9nAB3UuiJomwbRbKgy22Tj8cyavmLC4ZaViqW9E102NOLU4FYLgdZVET+mbdg==; cadataIV=bTM88YL1zmz7FsBEB0y3nI2SrdSTy+KLxCpx2FRfIZYFo2spN1IHQMSCT76OXrg79sVPhyqXk+N9rOj6M9KsQl4KqMNVBcoXgp24POpgoTwd4FBmKtAYbd9SDErna3jrMO168ML9PDG18K3CnBf6YG1tsIs0gXOEP9LzHVmUPF7KCKqUFiOiZGWuwmPhl85eo77BbEpVN2JkPnzuQWn6tC0cY4f2cJDlr3Z23SrAUVwwXmgRg2DXfOF5MIEkpwYiiI6sABCD9rsSnE6zTXlvZg33hjiD/ywUV1ZWjI2M/4zBixa4s150+dOnMmvtEFs/nOMnvMJui4PEDlTA==; cadataSig=WL3hB+av7sO3bzjL+Efe5b4exnvQxSInH3U5jDvfnPcttSp0XUF3y/NB573C0CTBYuOH/40smFssXlrKhT9tG2ITivdSIIamOmarmC8XwFOv9qQIFMHofcO/jjRDMqF0qRk7WBAC2FgBQrf2Tvq7wk5IX/JHn6zhlgKALAAqH9L9JNC244etnjj9YNaMDYEHV2M2jVTu3FsELqw1rSSqp0hEBlh+aFBvYCBg5hS1mVI76ZCHZVa0OUejiH2yiZyJIKHUI+Sv0rpU3iiQNtIFmGEdwhoo/rga4s4Dc2UsJLQ8c0yGlZgflYs+7Q5gPr74/mTUin60ej/w3M0roUl3FQ==; UC=d8be544621964f3c9865b3ee872fd432; AppcacheVer=15.0.1236.3:en-usbase; X-OWA-CANARY=VOXQP6xtGkiNnv7E4rFt8TrmclqVFtQI4IJqZflrR7Wz9AMPkMsFoyAlquw1YGsTUxIkVouAcvk.', + 'Origin': 'https://nih.mail.edu.fr', + 'Accept-Encoding': 'gzip, deflate, br', + 'X-EWS-TargetVersion': '2.5', + 'Accept-Language': 'en-US,en;q=0.8', + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36', + 'Action': 'CreateItem', + 'X-Requested-With': 'XMLHttpRequest', + 'Connection': 'keep-alive', + 'X-OWA-CANARY': 'VOXQP6xtGkiNnv7E4rFt8TrmclqVFtQI4IJqZflrR7Wz9AMPkMsFoyAlquw1YGsTUxIkVouAcvk.', + 'X-OWA-ActionName': 'CreateMessageForComposeSend', + 'X-OWA-ActionId': '-37', + 'X-OWA-ServiceUnavailableOnTransientError': 'true', + 'Content-Type': 'application/json; charset=UTF-8', + 'Accept': '*/*', + 'Referer': 'https://localhost/api/', + 'X-OWA-ClientBuildVersion': '15.0.1236.3', + 'X-OWA-CorrelationId': '2f11f8fb-f6c6-43a5-881d-8a1b242a4e70_148023102251337', + 'DNT': '1', + 'X-OWA-ClientBegin': '2016-11-27T07:17:02.513', + 'X-OWA-Attempt': '1' + } + } +); diff --git a/test/fixtures/node-axios/post_empty.js b/test/fixtures/node-axios/post_empty.js index fb2cfe5..d1c68b0 100644 --- a/test/fixtures/node-axios/post_empty.js +++ b/test/fixtures/node-axios/post_empty.js @@ -1,7 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' +const response = await axios.post( + 'http://localhost:28139', + '', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } } -}); +); diff --git a/test/fixtures/node-axios/post_escaped_double_quotes_in_single_quotes.js b/test/fixtures/node-axios/post_escaped_double_quotes_in_single_quotes.js index e88f400..947530e 100644 --- a/test/fixtures/node-axios/post_escaped_double_quotes_in_single_quotes.js +++ b/test/fixtures/node-axios/post_escaped_double_quotes_in_single_quotes.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'foo=\\"bar\\"' -}); +const response = await axios.post( + 'http://localhost:28139/', + 'foo=\\"bar\\"', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_escaped_single_quotes_in_double_quotes.js b/test/fixtures/node-axios/post_escaped_single_quotes_in_double_quotes.js index 65b8949..1661de4 100644 --- a/test/fixtures/node-axios/post_escaped_single_quotes_in_double_quotes.js +++ b/test/fixtures/node-axios/post_escaped_single_quotes_in_double_quotes.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'foo=\\\'bar\\\'' -}); +const response = await axios.post( + 'http://localhost:28139/', + 'foo=\\\'bar\\\'', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_form.js b/test/fixtures/node-axios/post_form.js index dde4380..90a1102 100644 --- a/test/fixtures/node-axios/post_form.js +++ b/test/fixtures/node-axios/post_form.js @@ -1,10 +1,16 @@ const axios = require('axios'); const FormData = require('form-data'); -const formData = new FormData(); -formData.append('username', 'davidwalsh'); -formData.append('password', 'something'); +const form = new FormData(); +form.append('username', 'davidwalsh'); +form.append('password', 'something'); -const response = await axios.post('http://localhost:28139/post-to-me.php', { - data: formData -}); +const response = await axios.post( + 'http://localhost:28139/post-to-me.php', + form, + { + headers: { + ...form.getHeaders() + } + } +); diff --git a/test/fixtures/node-axios/post_image.js b/test/fixtures/node-axios/post_image.js index 94db69f..71c465f 100644 --- a/test/fixtures/node-axios/post_image.js +++ b/test/fixtures/node-axios/post_image.js @@ -2,9 +2,15 @@ const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs'); -const formData = new FormData(); -formData.append('image', fs.readFileSync('image.jpg'), 'image.jpg'); +const form = new FormData(); +form.append('image', fs.readFileSync('image.jpg'), 'image.jpg'); -const response = await axios.post('http://localhost:28139/targetservice', { - data: formData -}); +const response = await axios.post( + 'http://localhost:28139/targetservice', + form, + { + headers: { + ...form.getHeaders() + } + } +); diff --git a/test/fixtures/node-axios/post_json.js b/test/fixtures/node-axios/post_json.js index 6538d81..9710dfd 100644 --- a/test/fixtures/node-axios/post_json.js +++ b/test/fixtures/node-axios/post_json.js @@ -1,12 +1,15 @@ const axios = require('axios'); -const response = await axios.post('https://localhost:28139', { - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/json' - }, - // data: '{ "drink": "coffe" }', - data: JSON.stringify({ +const response = await axios.post( + 'https://localhost:28139', + // '{ "drink": "coffe" }', + { 'drink': 'coffe' - }) -}); + }, + { + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + } +); diff --git a/test/fixtures/node-axios/post_json_multiple_headers.js b/test/fixtures/node-axios/post_json_multiple_headers.js index e1e9b76..e43f444 100644 --- a/test/fixtures/node-axios/post_json_multiple_headers.js +++ b/test/fixtures/node-axios/post_json_multiple_headers.js @@ -1,14 +1,17 @@ const axios = require('axios'); -const response = await axios.post('https://localhost:28139/rest/login-sessions', { - headers: { - 'Content-Type': 'application/json', - 'X-API-Version': '200' - }, - // data: '{"userName":"username123","password":"password123", "authLoginDomain":"local"}', - data: JSON.stringify({ +const response = await axios.post( + 'https://localhost:28139/rest/login-sessions', + // '{"userName":"username123","password":"password123", "authLoginDomain":"local"}', + { 'userName': 'username123', 'password': 'password123', 'authLoginDomain': 'local' - }) -}); + }, + { + headers: { + 'Content-Type': 'application/json', + 'X-API-Version': '200' + } + } +); diff --git a/test/fixtures/node-axios/post_number.js b/test/fixtures/node-axios/post_number.js index 7a1036b..0123feb 100644 --- a/test/fixtures/node-axios/post_number.js +++ b/test/fixtures/node-axios/post_number.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: '123' -}); +const response = await axios.post( + 'http://localhost:28139', + '123', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_quotes_inside_data.js b/test/fixtures/node-axios/post_quotes_inside_data.js index 0ae4c8f..97f4563 100644 --- a/test/fixtures/node-axios/post_quotes_inside_data.js +++ b/test/fixtures/node-axios/post_quotes_inside_data.js @@ -1,10 +1,8 @@ const axios = require('axios'); -const response = await axios.post('localhost:28139', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: { +const response = await axios.post( + 'localhost:28139', + new URLSearchParams({ 'field': 'don\'t you like quotes' - } -}); + }) +); diff --git a/test/fixtures/node-axios/post_same_field_multiple_times.js b/test/fixtures/node-axios/post_same_field_multiple_times.js index d120e99..4ce94b7 100644 --- a/test/fixtures/node-axios/post_same_field_multiple_times.js +++ b/test/fixtures/node-axios/post_same_field_multiple_times.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'foo=bar&foo=&foo=barbar' -}); +const response = await axios.post( + 'http://localhost:28139/', + 'foo=bar&foo=&foo=barbar', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_with_browser_headers.js b/test/fixtures/node-axios/post_with_browser_headers.js index 3d44cd3..97d053f 100644 --- a/test/fixtures/node-axios/post_with_browser_headers.js +++ b/test/fixtures/node-axios/post_with_browser_headers.js @@ -1,15 +1,19 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/ajax/demo_post.asp', { - headers: { - 'Origin': 'http://www.w3schools.com', - 'Accept-Encoding': 'gzip, deflate', - 'Accept-Language': 'en-US,en;q=0.8', - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', - 'Accept': '*/*', - 'Referer': 'http://www.w3schools.com/ajax/tryit_view.asp?x=0.07944501144811511', - 'Cookie': '_gat=1; ASPSESSIONIDACCRDTDC=MCMDKFMBLLLHGKCGNMKNGPKI; _ga=GA1.2.1424920226.1419478126', - 'Connection': 'keep-alive', - 'Content-Length': '0' +const response = await axios.post( + 'http://localhost:28139/ajax/demo_post.asp', + '', + { + headers: { + 'Origin': 'http://www.w3schools.com', + 'Accept-Encoding': 'gzip, deflate', + 'Accept-Language': 'en-US,en;q=0.8', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', + 'Accept': '*/*', + 'Referer': 'http://www.w3schools.com/ajax/tryit_view.asp?x=0.07944501144811511', + 'Cookie': '_gat=1; ASPSESSIONIDACCRDTDC=MCMDKFMBLLLHGKCGNMKNGPKI; _ga=GA1.2.1424920226.1419478126', + 'Connection': 'keep-alive', + 'Content-Length': '0' + } } -}); +); diff --git a/test/fixtures/node-axios/post_with_colon_in_header.js b/test/fixtures/node-axios/post_with_colon_in_header.js index de74abd..0bcdc0f 100644 --- a/test/fixtures/node-axios/post_with_colon_in_header.js +++ b/test/fixtures/node-axios/post_with_colon_in_header.js @@ -1,8 +1,12 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/endpoint', { - headers: { - 'Content-Type': 'application/json', - 'key': 'abcdefg' +const response = await axios.post( + 'http://localhost:28139/endpoint', + '', + { + headers: { + 'Content-Type': 'application/json', + 'key': 'abcdefg' + } } -}); +); diff --git a/test/fixtures/node-axios/post_with_data-ascii.js b/test/fixtures/node-axios/post_with_data-ascii.js index 41606d8..ec25d38 100644 --- a/test/fixtures/node-axios/post_with_data-ascii.js +++ b/test/fixtures/node-axios/post_with_data-ascii.js @@ -1,19 +1,22 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/echo/html/', { - headers: { - 'Origin': 'http://fiddle.jshell.net', - 'Accept-Encoding': 'gzip, deflate', - 'Accept-Language': 'en-US,en;q=0.8', - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'Accept': '*/*', - 'Referer': 'http://fiddle.jshell.net/_display/', - 'X-Requested-With': 'XMLHttpRequest', - 'Connection': 'keep-alive' - }, - data: { +const response = await axios.post( + 'http://localhost:28139/echo/html/', + new URLSearchParams({ 'msg1': 'wow', 'msg2': 'such' + }), + { + headers: { + 'Origin': 'http://fiddle.jshell.net', + 'Accept-Encoding': 'gzip, deflate', + 'Accept-Language': 'en-US,en;q=0.8', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'Accept': '*/*', + 'Referer': 'http://fiddle.jshell.net/_display/', + 'X-Requested-With': 'XMLHttpRequest', + 'Connection': 'keep-alive' + } } -}); +); diff --git a/test/fixtures/node-axios/post_with_data_binary.js b/test/fixtures/node-axios/post_with_data_binary.js index b7c97df..cb9481e 100644 --- a/test/fixtures/node-axios/post_with_data_binary.js +++ b/test/fixtures/node-axios/post_with_data_binary.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/post', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: '{"title":"china1"}' -}); +const response = await axios.post( + 'http://localhost:28139/post', + '{"title":"china1"}', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_with_data_raw.js b/test/fixtures/node-axios/post_with_data_raw.js index e21caf2..f680ca7 100644 --- a/test/fixtures/node-axios/post_with_data_raw.js +++ b/test/fixtures/node-axios/post_with_data_raw.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/post', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'msg1=wow&msg2=such&msg3=@rawmsg' -}); +const response = await axios.post( + 'http://localhost:28139/post', + 'msg1=wow&msg2=such&msg3=@rawmsg', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_with_data_with_percent_sign.js b/test/fixtures/node-axios/post_with_data_with_percent_sign.js index 357ade3..e60b866 100644 --- a/test/fixtures/node-axios/post_with_data_with_percent_sign.js +++ b/test/fixtures/node-axios/post_with_data_with_percent_sign.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('https://localhost:28139/post', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'secret=*%5*!' -}); +const response = await axios.post( + 'https://localhost:28139/post', + 'secret=*%5*!', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_with_double_quotes_inside_single_quotes.js b/test/fixtures/node-axios/post_with_double_quotes_inside_single_quotes.js index 5bc471b..dfe66a3 100644 --- a/test/fixtures/node-axios/post_with_double_quotes_inside_single_quotes.js +++ b/test/fixtures/node-axios/post_with_double_quotes_inside_single_quotes.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'foo="bar"' -}); +const response = await axios.post( + 'http://localhost:28139/', + 'foo="bar"', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_with_escaped_double_quotes.js b/test/fixtures/node-axios/post_with_escaped_double_quotes.js index 5bc471b..dfe66a3 100644 --- a/test/fixtures/node-axios/post_with_escaped_double_quotes.js +++ b/test/fixtures/node-axios/post_with_escaped_double_quotes.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'foo="bar"' -}); +const response = await axios.post( + 'http://localhost:28139/', + 'foo="bar"', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_with_extra_whitespace.js b/test/fixtures/node-axios/post_with_extra_whitespace.js index cabd143..88d4737 100644 --- a/test/fixtures/node-axios/post_with_extra_whitespace.js +++ b/test/fixtures/node-axios/post_with_extra_whitespace.js @@ -2,17 +2,21 @@ const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs'); -const formData = new FormData(); -formData.append('files', fs.readFileSync('47.htz'), '47.htz'); -formData.append('name', '47'); -formData.append('oldMediaId', '47'); -formData.append('updateInLayouts', '1'); -formData.append('deleteOldRevisions', '1'); +const form = new FormData(); +form.append('files', fs.readFileSync('47.htz'), '47.htz'); +form.append('name', '47'); +form.append('oldMediaId', '47'); +form.append('updateInLayouts', '1'); +form.append('deleteOldRevisions', '1'); -const response = await axios.post('http://localhost:28139/api/library', { - headers: { - 'accept': 'application/json', - 'Content-Type': 'multipart/form-data' - }, - data: formData -}); +const response = await axios.post( + 'http://localhost:28139/api/library', + form, + { + headers: { + ...form.getHeaders(), + 'accept': 'application/json', + 'Content-Type': 'multipart/form-data' + } + } +); diff --git a/test/fixtures/node-axios/post_with_quotes_anywhere.js b/test/fixtures/node-axios/post_with_quotes_anywhere.js index ed8582c..15b7096 100644 --- a/test/fixtures/node-axios/post_with_quotes_anywhere.js +++ b/test/fixtures/node-axios/post_with_quotes_anywhere.js @@ -1,15 +1,18 @@ const axios = require('axios'); -const response = await axios.post('https://localhost:28139', { - headers: { - 'A': '\'\'a\'', - 'B': '"', - 'Cookie': 'x=1\'; y=2"', - 'Content-Type': 'application/x-www-form-urlencoded' - }, - auth: { - username: 'ol\'', - password: 'asd"' - }, - data: 'a=b&c="&d=\'' -}); +const response = await axios.post( + 'https://localhost:28139', + 'a=b&c="&d=\'', + { + headers: { + 'A': '\'\'a\'', + 'B': '"', + 'Cookie': 'x=1\'; y=2"', + 'Content-Type': 'application/x-www-form-urlencoded' + }, + auth: { + username: 'ol\'', + password: 'asd"' + } + } +); diff --git a/test/fixtures/node-axios/post_with_single_quotes_inside_double_quotes.js b/test/fixtures/node-axios/post_with_single_quotes_inside_double_quotes.js index e0d36a8..8581ee1 100644 --- a/test/fixtures/node-axios/post_with_single_quotes_inside_double_quotes.js +++ b/test/fixtures/node-axios/post_with_single_quotes_inside_double_quotes.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: 'foo=\'bar\'' -}); +const response = await axios.post( + 'http://localhost:28139/', + 'foo=\'bar\'', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/post_with_urlencoded_data.js b/test/fixtures/node-axios/post_with_urlencoded_data.js index 41606d8..ec25d38 100644 --- a/test/fixtures/node-axios/post_with_urlencoded_data.js +++ b/test/fixtures/node-axios/post_with_urlencoded_data.js @@ -1,19 +1,22 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/echo/html/', { - headers: { - 'Origin': 'http://fiddle.jshell.net', - 'Accept-Encoding': 'gzip, deflate', - 'Accept-Language': 'en-US,en;q=0.8', - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'Accept': '*/*', - 'Referer': 'http://fiddle.jshell.net/_display/', - 'X-Requested-With': 'XMLHttpRequest', - 'Connection': 'keep-alive' - }, - data: { +const response = await axios.post( + 'http://localhost:28139/echo/html/', + new URLSearchParams({ 'msg1': 'wow', 'msg2': 'such' + }), + { + headers: { + 'Origin': 'http://fiddle.jshell.net', + 'Accept-Encoding': 'gzip, deflate', + 'Accept-Language': 'en-US,en;q=0.8', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'Accept': '*/*', + 'Referer': 'http://fiddle.jshell.net/_display/', + 'X-Requested-With': 'XMLHttpRequest', + 'Connection': 'keep-alive' + } } -}); +); diff --git a/test/fixtures/node-axios/post_with_urlencoded_data_and_headers.js b/test/fixtures/node-axios/post_with_urlencoded_data_and_headers.js index e447424..f137650 100644 --- a/test/fixtures/node-axios/post_with_urlencoded_data_and_headers.js +++ b/test/fixtures/node-axios/post_with_urlencoded_data_and_headers.js @@ -1,17 +1,8 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/api/Listing.svc/PropertySearch_Post', { - headers: { - 'Origin': 'http://www.realtor.ca', - 'Accept-Encoding': 'gzip, deflate', - 'Accept-Language': 'en-US,en;q=0.8', - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36', - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'Accept': '*/*', - 'Referer': 'http://www.realtor.ca/Residential/Map.aspx', - 'Connection': 'keep-alive' - }, - data: { +const response = await axios.post( + 'http://localhost:28139/api/Listing.svc/PropertySearch_Post', + new URLSearchParams({ 'CultureId': '1', 'ApplicationId': '1', 'RecordsPerPage': '200', @@ -33,5 +24,17 @@ const response = await axios.post('http://localhost:28139/api/Listing.svc/Proper 'Latitude': '43.6552047278685', 'ZoomLevel': '13', 'CurrentPage': '1' + }), + { + headers: { + 'Origin': 'http://www.realtor.ca', + 'Accept-Encoding': 'gzip, deflate', + 'Accept-Language': 'en-US,en;q=0.8', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36', + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'Accept': '*/*', + 'Referer': 'http://www.realtor.ca/Residential/Map.aspx', + 'Connection': 'keep-alive' + } } -}); +); diff --git a/test/fixtures/node-axios/post_xpost.js b/test/fixtures/node-axios/post_xpost.js index 2d92568..93a5ca5 100644 --- a/test/fixtures/node-axios/post_xpost.js +++ b/test/fixtures/node-axios/post_xpost.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.post('http://localhost:28139/api/xxxxxxxxxxxxxxxx', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: '{"keywords":"php","page":1,"searchMode":1}' -}); +const response = await axios.post( + 'http://localhost:28139/api/xxxxxxxxxxxxxxxx', + '{"keywords":"php","page":1,"searchMode":1}', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/put_basic_auth_json_data.js b/test/fixtures/node-axios/put_basic_auth_json_data.js index d618325..07a668f 100644 --- a/test/fixtures/node-axios/put_basic_auth_json_data.js +++ b/test/fixtures/node-axios/put_basic_auth_json_data.js @@ -1,12 +1,15 @@ const axios = require('axios'); -const response = await axios.put('http://localhost:28139/test/_security', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - auth: { - username: 'admin', - password: '123' - }, - data: '{"admins":{"names":[], "roles":[]}, "readers":{"names":["joe"],"roles":[]}}' -}); +const response = await axios.put( + 'http://localhost:28139/test/_security', + '{"admins":{"names":[], "roles":[]}, "readers":{"names":["joe"],"roles":[]}}', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + auth: { + username: 'admin', + password: '123' + } + } +); diff --git a/test/fixtures/node-axios/put_with_T_option.js b/test/fixtures/node-axios/put_with_T_option.js index 96e85ff..5a13b3b 100644 --- a/test/fixtures/node-axios/put_with_T_option.js +++ b/test/fixtures/node-axios/put_with_T_option.js @@ -1,15 +1,18 @@ const axios = require('axios'); -const response = await axios.put('http://localhost:28139/twitter/_mapping/user?pretty', { - headers: { - 'Content-Type': 'application/json' - }, - // data: '{"properties": {"email": {"type": "keyword"}}}', - data: JSON.stringify({ +const response = await axios.put( + 'http://localhost:28139/twitter/_mapping/user?pretty', + // '{"properties": {"email": {"type": "keyword"}}}', + { 'properties': { 'email': { 'type': 'keyword' } } - }) -}); + }, + { + headers: { + 'Content-Type': 'application/json' + } + } +); diff --git a/test/fixtures/node-axios/put_with_file.js b/test/fixtures/node-axios/put_with_file.js index e7dabe7..16a985d 100644 --- a/test/fixtures/node-axios/put_with_file.js +++ b/test/fixtures/node-axios/put_with_file.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const response = await axios.put('http://localhost:28139/upload', { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - data: '@new_file' -}); +const response = await axios.put( + 'http://localhost:28139/upload', + '@new_file', + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + } + } +); diff --git a/test/fixtures/node-axios/put_xput.js b/test/fixtures/node-axios/put_xput.js index 96e85ff..5a13b3b 100644 --- a/test/fixtures/node-axios/put_xput.js +++ b/test/fixtures/node-axios/put_xput.js @@ -1,15 +1,18 @@ const axios = require('axios'); -const response = await axios.put('http://localhost:28139/twitter/_mapping/user?pretty', { - headers: { - 'Content-Type': 'application/json' - }, - // data: '{"properties": {"email": {"type": "keyword"}}}', - data: JSON.stringify({ +const response = await axios.put( + 'http://localhost:28139/twitter/_mapping/user?pretty', + // '{"properties": {"email": {"type": "keyword"}}}', + { 'properties': { 'email': { 'type': 'keyword' } } - }) -}); + }, + { + headers: { + 'Content-Type': 'application/json' + } + } +); diff --git a/test/fixtures/node-axios/strange_http_method.js b/test/fixtures/node-axios/strange_http_method.js index 28c64a2..f0eda42 100644 --- a/test/fixtures/node-axios/strange_http_method.js +++ b/test/fixtures/node-axios/strange_http_method.js @@ -1,5 +1,3 @@ const axios = require('axios'); -const response = await axios.request('localhost:28139', { - method: 'what' -}); +const response = await axios('localhost:28139');