Skip to content

Commit afa7ab9

Browse files
committed
fix(headers): always invoke generator without custom headers
1 parent 42b36fa commit afa7ab9

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

Sources/TUSKit/TUSClient.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,10 +836,6 @@ final class HeaderGenerator {
836836

837837
func resolveHeaders(for metaData: UploadMetadata, completion: @escaping ([String: String]) -> Void) {
838838
let baseHeaders = storedHeaders(for: metaData)
839-
guard !baseHeaders.isEmpty else {
840-
completion([:])
841-
return
842-
}
843839
guard let handler = handler else {
844840
store(headers: baseHeaders, for: metaData)
845841
completion(baseHeaders)

Tests/TUSKitTests/TUSClient/TUSClient_HeaderGenerationTests.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,37 @@ final class TUSClient_HeaderGenerationTests: XCTestCase {
6161
XCTAssertEqual(receivedRequestID, uploadID)
6262
}
6363

64-
/// Verifies we don't bother clients when there are no custom headers to override.
65-
func testGenerateHeadersNotCalledWhenNoCustomHeaders() throws {
64+
/// Verifies the generator is called even when no custom headers were supplied, enabling clients to inject headers.
65+
func testGenerateHeadersCalledWithoutCustomHeaders() throws {
6666
let configuration = makeConfiguration()
67+
prepareNetworkForSuccesfulUploads(data: data)
6768

68-
let generatorNotCalled = expectation(description: "Header generator should not be invoked")
69-
generatorNotCalled.isInverted = true
69+
let generatorCalled = expectation(description: "Header generator should be invoked")
70+
tusDelegate.finishUploadExpectation = expectation(description: "Upload finished")
7071

71-
client = try makeClient(configuration: configuration) { _, _, onHeadersGenerated in
72-
generatorNotCalled.fulfill()
73-
onHeadersGenerated([:])
72+
var firstReceivedHeaders: [String: String]?
73+
var fulfilledGenerator = false
74+
client = try makeClient(configuration: configuration) { _, headers, onHeadersGenerated in
75+
if !fulfilledGenerator {
76+
firstReceivedHeaders = headers
77+
generatorCalled.fulfill()
78+
fulfilledGenerator = true
79+
}
80+
onHeadersGenerated(["Authorization": "Bearer injected"])
7481
}
7582
client.delegate = tusDelegate
7683

7784
XCTAssertNoThrow(try client.upload(data: data))
78-
wait(for: [generatorNotCalled], timeout: 0.5)
85+
wait(for: [generatorCalled, tusDelegate.finishUploadExpectation!], timeout: 5)
86+
87+
XCTAssertEqual(firstReceivedHeaders ?? [:], [:])
88+
let createRequests = MockURLProtocol.receivedRequests.filter { $0.httpMethod == "POST" }
89+
XCTAssertFalse(createRequests.isEmpty)
90+
guard let postHeaders = createRequests.first?.allHTTPHeaderFields else {
91+
XCTFail("Expected POST request headers")
92+
return
93+
}
94+
XCTAssertEqual(postHeaders["Authorization"], "Bearer injected")
7995
}
8096

8197
/// Ensures the generator receives the headers that were actually used on the previous request when retrying.

0 commit comments

Comments
 (0)