Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Sources/Functions/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ public struct FunctionInvokeOptions {
let body: Data?

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

switch body {
case let string as String:
headers["Content-Type"] = "text/plain"
defaultHeaders["Content-Type"] = "text/plain"
self.body = string.data(using: .utf8)
case let data as Data:
headers["Content-Type"] = "application/octet-stream"
defaultHeaders["Content-Type"] = "application/octet-stream"
self.body = data
default:
// default, assume this is JSON
headers["Content-Type"] = "application/json"
defaultHeaders["Content-Type"] = "application/json"
self.body = try? JSONEncoder().encode(body)
}

self.method = method
self.headers = headers
self.headers = defaultHeaders.merging(headers) { _, new in new }
}

public init(method: Method? = nil, headers: [String: String] = [:]) {
Expand Down
11 changes: 11 additions & 0 deletions Tests/FunctionsTests/FunctionInvokeOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ final class FunctionInvokeOptionsTests: XCTestCase {
XCTAssertEqual(options.headers["Content-Type"], "application/json")
XCTAssertNotNil(options.body)
}

func testMultipartFormDataBody() {
let boundary = "Boundary-\(UUID().uuidString)"
let contentType = "multipart/form-data; boundary=\(boundary)"
let options = FunctionInvokeOptions(
headers: ["Content-Type": contentType],
body: "binary value".data(using: .utf8)!
)
XCTAssertEqual(options.headers["Content-Type"], contentType)
XCTAssertNotNil(options.body)
}
}