Skip to content

Commit

Permalink
feat(requestSnippets): handle type: string, format: binary or base64 …
Browse files Browse the repository at this point in the history
…file upload data (#7545)

* test(curlify): assert that data-binary is generated

Co-authored-by: Tim Lai <timothy.lai@gmail.com>
  • Loading branch information
mathis-m and tim-lai committed Jan 31, 2022
1 parent e8cc851 commit 2b30a34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/core/plugins/request-snippets/fn.js
Expand Up @@ -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"))) {
for (let [k, v] of request.get("body").entrySeq()) {
for (let [k, v] of body.entrySeq()) {
let extractedKey = extractKey(k)
addNewLine()
addIndent()
Expand All @@ -116,11 +117,15 @@ const curlify = (request, escape, newLine, ext = "") => {
addWords(`${extractedKey}=${v}`)
}
}
} else if(body instanceof win.File) {
addNewLine()
addIndent()
addWordsWithoutLeadingSpace(`--data-binary '@${body.name}'`)
} else {
addNewLine()
addIndent()
addWordsWithoutLeadingSpace("-d ")
let reqBody = request.get("body")
let reqBody = body
if (!Map.isMap(reqBody)) {
if (typeof reqBody !== "string") {
reqBody = JSON.stringify(reqBody)
Expand All @@ -130,7 +135,7 @@ const curlify = (request, escape, newLine, ext = "") => {
addWordsWithoutLeadingSpace(getStringBodyOfMap(request))
}
}
} else if (!request.get("body") && request.get("method") === "POST") {
} else if (!body && request.get("method") === "POST") {
addNewLine()
addIndent()
addWordsWithoutLeadingSpace("-d ''")
Expand Down
15 changes: 15 additions & 0 deletions test/unit/core/curlify.js
Expand Up @@ -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'")
})

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 () {
let req = {
url: "http://example.com",
Expand Down

0 comments on commit 2b30a34

Please sign in to comment.