feat(requestSnippets): handle type: string, format: binary or base64 file upload data (#7545)
* test(curlify): assert that data-binary is generated Co-authored-by: Tim Lai <timothy.lai@gmail.com>
This commit is contained in:
@@ -103,9 +103,10 @@ const curlify = (request, escape, newLine, ext = "") => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.get("body")) {
|
const body = request.get("body")
|
||||||
|
if (body) {
|
||||||
if (isMultipartFormDataRequest && ["POST", "PUT", "PATCH"].includes(request.get("method"))) {
|
if (isMultipartFormDataRequest && ["POST", "PUT", "PATCH"].includes(request.get("method"))) {
|
||||||
for (let [k, v] of request.get("body").entrySeq()) {
|
for (let [k, v] of body.entrySeq()) {
|
||||||
let extractedKey = extractKey(k)
|
let extractedKey = extractKey(k)
|
||||||
addNewLine()
|
addNewLine()
|
||||||
addIndent()
|
addIndent()
|
||||||
@@ -116,11 +117,15 @@ const curlify = (request, escape, newLine, ext = "") => {
|
|||||||
addWords(`${extractedKey}=${v}`)
|
addWords(`${extractedKey}=${v}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if(body instanceof win.File) {
|
||||||
|
addNewLine()
|
||||||
|
addIndent()
|
||||||
|
addWordsWithoutLeadingSpace(`--data-binary '@${body.name}'`)
|
||||||
} else {
|
} else {
|
||||||
addNewLine()
|
addNewLine()
|
||||||
addIndent()
|
addIndent()
|
||||||
addWordsWithoutLeadingSpace("-d ")
|
addWordsWithoutLeadingSpace("-d ")
|
||||||
let reqBody = request.get("body")
|
let reqBody = body
|
||||||
if (!Map.isMap(reqBody)) {
|
if (!Map.isMap(reqBody)) {
|
||||||
if (typeof reqBody !== "string") {
|
if (typeof reqBody !== "string") {
|
||||||
reqBody = JSON.stringify(reqBody)
|
reqBody = JSON.stringify(reqBody)
|
||||||
@@ -130,7 +135,7 @@ const curlify = (request, escape, newLine, ext = "") => {
|
|||||||
addWordsWithoutLeadingSpace(getStringBodyOfMap(request))
|
addWordsWithoutLeadingSpace(getStringBodyOfMap(request))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (!request.get("body") && request.get("method") === "POST") {
|
} else if (!body && request.get("method") === "POST") {
|
||||||
addNewLine()
|
addNewLine()
|
||||||
addIndent()
|
addIndent()
|
||||||
addWordsWithoutLeadingSpace("-d ''")
|
addWordsWithoutLeadingSpace("-d ''")
|
||||||
|
|||||||
@@ -220,6 +220,21 @@ 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'")
|
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'")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should print a curl with data-binary if body is instance of File and it is not a multipart form data request", function () {
|
||||||
|
let file = new win.File([""], "file.txt", { type: "" })
|
||||||
|
|
||||||
|
let req = {
|
||||||
|
url: "http://example.com",
|
||||||
|
method: "POST",
|
||||||
|
headers: { "content-type": "application/octet-stream" },
|
||||||
|
body: file
|
||||||
|
}
|
||||||
|
|
||||||
|
let curlified = curl(Im.fromJS(req))
|
||||||
|
|
||||||
|
expect(curlified).toEqual("curl -X 'POST' \\\n 'http://example.com' \\\n -H 'content-type: application/octet-stream' \\\n --data-binary '@file.txt'")
|
||||||
|
})
|
||||||
|
|
||||||
it("prints a curl post statement from an object", function () {
|
it("prints a curl post statement from an object", function () {
|
||||||
let req = {
|
let req = {
|
||||||
url: "http://example.com",
|
url: "http://example.com",
|
||||||
|
|||||||
Reference in New Issue
Block a user