fix: curlify agnostic to order of header values (#6152)
Refs #6082 * use curlify flag isMultipartFormDataRequest * curlify test updated Co-authored-by: Vladimir Gorej <vladimir.gorej@gmail.com>
This commit is contained in:
@@ -16,7 +16,7 @@ const extractKey = (k) => {
|
|||||||
|
|
||||||
export default function curl( request ){
|
export default function curl( request ){
|
||||||
let curlified = []
|
let curlified = []
|
||||||
let type = ""
|
let isMultipartFormDataRequest = false
|
||||||
let headers = request.get("headers")
|
let headers = request.get("headers")
|
||||||
curlified.push( "curl" )
|
curlified.push( "curl" )
|
||||||
curlified.push( "-X", request.get("method") )
|
curlified.push( "-X", request.get("method") )
|
||||||
@@ -25,15 +25,14 @@ export default function curl( request ){
|
|||||||
if ( headers && headers.size ) {
|
if ( headers && headers.size ) {
|
||||||
for( let p of request.get("headers").entries() ){
|
for( let p of request.get("headers").entries() ){
|
||||||
let [ h,v ] = p
|
let [ h,v ] = p
|
||||||
type = v
|
|
||||||
curlified.push( "-H " )
|
curlified.push( "-H " )
|
||||||
curlified.push( `"${h}: ${v}"` )
|
curlified.push( `"${h}: ${v}"` )
|
||||||
|
isMultipartFormDataRequest = isMultipartFormDataRequest || /^content-type$/i.test(h) && /^multipart\/form-data$/i.test(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( request.get("body") ){
|
if ( request.get("body") ){
|
||||||
|
if (isMultipartFormDataRequest && ["POST", "PUT", "PATCH"].includes(request.get("method"))) {
|
||||||
if(type === "multipart/form-data" && ["POST", "PUT", "PATCH"].includes(request.get("method"))) {
|
|
||||||
for( let [ k,v ] of request.get("body").entrySeq()) {
|
for( let [ k,v ] of request.get("body").entrySeq()) {
|
||||||
let extractedKey = extractKey(k)
|
let extractedKey = extractKey(k)
|
||||||
curlified.push( "-F" )
|
curlified.push( "-F" )
|
||||||
|
|||||||
@@ -246,4 +246,77 @@ describe("curlify", function() {
|
|||||||
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d \"{\\\"id\\\":\\\"foo'bar\\\"}\"")
|
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"accept: application/json\" -d \"{\\\"id\\\":\\\"foo'bar\\\"}\"")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
context("given multiple entries with file", function() {
|
||||||
|
context("and with leading custom header", function() {
|
||||||
|
it("should print a proper curl -F", function() {
|
||||||
|
let file = new win.File()
|
||||||
|
file.name = "file.txt"
|
||||||
|
file.type = "text/plain"
|
||||||
|
|
||||||
|
let req = {
|
||||||
|
url: "http://example.com",
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"x-custom-name": "multipart/form-data",
|
||||||
|
"content-type": "multipart/form-data"
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
id: "123",
|
||||||
|
file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let curlified = curl(Im.fromJS(req))
|
||||||
|
|
||||||
|
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"x-custom-name: multipart/form-data\" -H \"content-type: multipart/form-data\" -F \"id=123\" -F \"file=@file.txt;type=text/plain\"")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
context("and with trailing custom header; e.g. from requestInterceptor appending req.headers", function() {
|
||||||
|
it("should print a proper curl -F", function() {
|
||||||
|
let file = new win.File()
|
||||||
|
file.name = "file.txt"
|
||||||
|
file.type = "text/plain"
|
||||||
|
|
||||||
|
let req = {
|
||||||
|
url: "http://example.com",
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"content-type": "multipart/form-data",
|
||||||
|
"x-custom-name": "any-value"
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
id: "123",
|
||||||
|
file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let curlified = curl(Im.fromJS(req))
|
||||||
|
|
||||||
|
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"content-type: multipart/form-data\" -H \"x-custom-name: any-value\" -F \"id=123\" -F \"file=@file.txt;type=text/plain\"")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
context("POST when header value is 'multipart/form-data' but header name is not 'content-type'", function() {
|
||||||
|
it("shoud print a proper curl as -d <data>", function() {
|
||||||
|
let file = new win.File()
|
||||||
|
file.name = "file.txt"
|
||||||
|
file.type = "text/plain"
|
||||||
|
|
||||||
|
let req = {
|
||||||
|
url: "http://example.com",
|
||||||
|
method: "POST",
|
||||||
|
headers: { "x-custom-name": "multipart/form-data" },
|
||||||
|
body: {
|
||||||
|
id: "123",
|
||||||
|
file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let curlified = curl(Im.fromJS(req))
|
||||||
|
|
||||||
|
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"x-custom-name: multipart/form-data\" -d {\"id\":\"123\",\"file\":{\"name\":\"file.txt\",\"type\":\"text/plain\"}}")
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user