feat: respect Encoding Object while building requests (#9105)
This change fixes both: 1. making multipart/form-data requests with content-type header for every individual boundary 2. generating correct CURL command for multipart/form-data request, allowing specifying content-type header for every individual boundary Refs #4826 Refs #5356
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import Im from "immutable"
|
||||
import { requestSnippetGenerator_curl_bash as curl } from "core/plugins/request-snippets/fn.js"
|
||||
import win from "core/window"
|
||||
import { fromJSOrdered } from "core/utils"
|
||||
|
||||
describe("curlify", function () {
|
||||
|
||||
@@ -200,25 +201,50 @@ describe("curlify", function () {
|
||||
expect(curlified).toEqual("curl -X 'POST' \\\n 'http://example.com' \\\n -H 'content-type: multipart/form-data' \\\n -F 'id=123' \\\n -F 'file=@file.txt;type=text/plain'")
|
||||
})
|
||||
|
||||
it("should print a curl with object formData and file", function () {
|
||||
let file = new win.File([""], "file.txt", { type: "text/plain" })
|
||||
it("should print a curl with formData containing JSON and file", async function () {
|
||||
/**
|
||||
* Specialized sub-class of File class, that only
|
||||
* accepts string data and retain this data in `data`
|
||||
* public property throughout the lifecycle of its instances.
|
||||
*
|
||||
* This sub-class is exclusively used only when Encoding Object
|
||||
* is defined within the Media Type Object (OpenAPI 3.x.y).
|
||||
*
|
||||
* Instances of a similar sub-class are produced by swagger-client request builder.
|
||||
*/
|
||||
class FileWithData extends win.File {
|
||||
constructor(data, name = "", options = {}) {
|
||||
super([data], name, options)
|
||||
this.data = data
|
||||
}
|
||||
|
||||
valueOf() {
|
||||
return this.data
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.valueOf()
|
||||
}
|
||||
}
|
||||
|
||||
let file = new win.File(["data"], "file.txt", { type: "text/plain" })
|
||||
let optionsJSON = JSON.stringify({ some_array: ["string"], max_bar: 300 })
|
||||
let options = new FileWithData(optionsJSON, "", { type: "application/json;charset=utf-8" })
|
||||
|
||||
let formData = new win.FormData()
|
||||
formData.set("options", options)
|
||||
formData.set("file", file)
|
||||
|
||||
let req = {
|
||||
url: "http://example.com",
|
||||
method: "POST",
|
||||
headers: { "content-type": "multipart/form-data" },
|
||||
body: {
|
||||
options: JSON.stringify({
|
||||
some_array: ["string"],
|
||||
max_bar: 300,
|
||||
}),
|
||||
file
|
||||
}
|
||||
body: formData,
|
||||
}
|
||||
|
||||
let curlified = curl(Im.fromJS(req))
|
||||
let curlified = curl(fromJSOrdered(req))
|
||||
|
||||
expect(curlified).toEqual(`curl -X 'POST' \\\n 'http://example.com' \\\n -H 'content-type: multipart/form-data' \\\n -F 'options={"some_array":["string"],"max_bar":300}' \\\n -F 'file=@file.txt;type=text/plain'`)
|
||||
expect(curlified).toEqual(`curl -X 'POST' \\\n 'http://example.com' \\\n -H 'content-type: multipart/form-data' \\\n -F 'options={"some_array":["string"],"max_bar":300};type=application/json;charset=utf-8' \\\n -F 'file=@file.txt;type=text/plain'`)
|
||||
})
|
||||
|
||||
it("should print a curl without form data type if type is unknown", function () {
|
||||
|
||||
Reference in New Issue
Block a user