Skip to content

Commit

Permalink
feat: add stored property for adding additional headers in SdkHttpReq…
Browse files Browse the repository at this point in the history
…uest (#593)
  • Loading branch information
dayaffe authored Sep 26, 2023
1 parent 1766d82 commit 2e4e744
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 14 additions & 1 deletion Sources/ClientRuntime/Networking/Http/SdkHttpRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public class SdkHttpRequest {
public let body: HttpBody
public let endpoint: Endpoint
public let method: HttpMethodType
public var headers: Headers { endpoint.headers ?? Headers() }
private var additionalHeaders: Headers = Headers()
public var headers: Headers {
var allHeaders = endpoint.headers ?? Headers()
allHeaders.addAll(headers: additionalHeaders)
return allHeaders
}
public var path: String { endpoint.path }
public var host: String { endpoint.host }
public var queryItems: [URLQueryItem]? { endpoint.queryItems }
Expand All @@ -40,6 +45,14 @@ public class SdkHttpRequest {
}
return builder
}

public func withHeader(name: String, value: String) {
self.additionalHeaders.add(name: name, value: value)
}

public func withoutHeader(name: String) {
self.additionalHeaders.remove(name: name)
}
}

extension SdkHttpRequest {
Expand Down
13 changes: 12 additions & 1 deletion Tests/ClientRuntimeTests/NetworkingTests/HttpRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,30 @@ class HttpRequestTests: NetworkingTestUtils {

let httpBody = HttpBody.data(expectedMockRequestData)
let mockHttpRequest = SdkHttpRequest(method: .get, endpoint: endpoint, body: httpBody)
mockHttpRequest.withHeader(name: "foo", value: "bar")
let httpRequest = try mockHttpRequest.toHttpRequest()

XCTAssertNotNil(httpRequest)
let headersFromRequest = httpRequest.getHeaders()
XCTAssertNotNil(headers)
for index in 0...(httpRequest.headerCount - 1) {

// Check headers
var additionalHeaderFound = false
for index in 0...(httpRequest.headerCount - 1) {
let header1 = headersFromRequest[index]
let header2 = mockHttpRequest.headers.headers[index]

// Check for additional header
if header1.name == "foo" {
XCTAssertEqual(header1.value, "bar")
additionalHeaderFound = true
}

XCTAssertEqual(header1.name, header2.name)
XCTAssertEqual(header1.value, header2.value.joined(separator: ","))
}

XCTAssertTrue(additionalHeaderFound, "Additional header 'foo' not found in httpRequest")
XCTAssertEqual(httpRequest.method, "GET")

if let bodyLength = try httpRequest.body?.length() {
Expand Down

0 comments on commit 2e4e744

Please sign in to comment.