Skip to content

Commit e8ba6d1

Browse files
authored
fix(functions): functions overrides headers (#160)
1 parent 6b918c3 commit e8ba6d1

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Sources/Functions/Types.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ public struct FunctionInvokeOptions {
3434
/// - headers: Headers to be included in the function invocation. (Default: empty dictionary)
3535
/// - body: The body data to be sent with the function invocation. (Default: nil)
3636
public init(method: Method? = nil, headers: [String: String] = [:], body: some Encodable) {
37-
var headers = headers
37+
var defaultHeaders = headers
3838

3939
switch body {
4040
case let string as String:
41-
headers["Content-Type"] = "text/plain"
41+
defaultHeaders["Content-Type"] = "text/plain"
4242
self.body = string.data(using: .utf8)
4343
case let data as Data:
44-
headers["Content-Type"] = "application/octet-stream"
44+
defaultHeaders["Content-Type"] = "application/octet-stream"
4545
self.body = data
4646
default:
4747
// default, assume this is JSON
48-
headers["Content-Type"] = "application/json"
48+
defaultHeaders["Content-Type"] = "application/json"
4949
self.body = try? JSONEncoder().encode(body)
5050
}
5151

5252
self.method = method
53-
self.headers = headers
53+
self.headers = defaultHeaders.merging(headers) { _, new in new }
5454
}
5555

5656
/// Initializes the `FunctionInvokeOptions` structure.

Tests/FunctionsTests/FunctionInvokeOptionsTests.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,35 @@ import XCTest
33
@testable import Functions
44

55
final class FunctionInvokeOptionsTests: XCTestCase {
6-
func testStringBody() {
6+
func test_initWithStringBody() {
77
let options = FunctionInvokeOptions(body: "string value")
88
XCTAssertEqual(options.headers["Content-Type"], "text/plain")
99
XCTAssertNotNil(options.body)
1010
}
1111

12-
func testDataBody() {
12+
func test_initWithDataBody() {
1313
let options = FunctionInvokeOptions(body: "binary value".data(using: .utf8)!)
1414
XCTAssertEqual(options.headers["Content-Type"], "application/octet-stream")
1515
XCTAssertNotNil(options.body)
1616
}
1717

18-
func testEncodableBody() {
18+
func test_initWithEncodableBody() {
1919
struct Body: Encodable {
2020
let value: String
2121
}
2222
let options = FunctionInvokeOptions(body: Body(value: "value"))
2323
XCTAssertEqual(options.headers["Content-Type"], "application/json")
2424
XCTAssertNotNil(options.body)
2525
}
26+
27+
func test_initWithCustomContentType() {
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)