Skip to content

Commit 272715d

Browse files
authored
fix: do not override Content-Type header (#6)
Co-authored-by: James Im <[email protected]>
1 parent 98878a6 commit 272715d

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Diff for: Sources/Functions/Types.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ public struct FunctionInvokeOptions {
1818
let body: Data?
1919

2020
public init(method: Method? = nil, headers: [String: String] = [:], body: some Encodable) {
21-
var headers = headers
21+
var defaultHeaders = [String: String]()
2222

2323
switch body {
2424
case let string as String:
25-
headers["Content-Type"] = "text/plain"
25+
defaultHeaders["Content-Type"] = "text/plain"
2626
self.body = string.data(using: .utf8)
2727
case let data as Data:
28-
headers["Content-Type"] = "application/octet-stream"
28+
defaultHeaders["Content-Type"] = "application/octet-stream"
2929
self.body = data
3030
default:
3131
// default, assume this is JSON
32-
headers["Content-Type"] = "application/json"
32+
defaultHeaders["Content-Type"] = "application/json"
3333
self.body = try? JSONEncoder().encode(body)
3434
}
3535

3636
self.method = method
37-
self.headers = headers
37+
self.headers = defaultHeaders.merging(headers) { _, new in new }
3838
}
3939

4040
public init(method: Method? = nil, headers: [String: String] = [:]) {

Diff for: Tests/FunctionsTests/FunctionInvokeOptionsTests.swift

+11
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,15 @@ final class FunctionInvokeOptionsTests: XCTestCase {
2323
XCTAssertEqual(options.headers["Content-Type"], "application/json")
2424
XCTAssertNotNil(options.body)
2525
}
26+
27+
func testMultipartFormDataBody() {
28+
let boundary = "Boundary-\(UUID().uuidString)"
29+
let contentType = "multipart/form-data; boundary=\(boundary)"
30+
let options = FunctionInvokeOptions(
31+
headers: ["Content-Type": contentType],
32+
body: "binary value".data(using: .utf8)!
33+
)
34+
XCTAssertEqual(options.headers["Content-Type"], contentType)
35+
XCTAssertNotNil(options.body)
36+
}
2637
}

0 commit comments

Comments
 (0)