@@ -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