From 900e8b1b5ec5eb8c9cd4f7ec5f835756bc6ac22f Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Tue, 7 Nov 2023 11:47:21 -0800 Subject: [PATCH] Sends multipart accept header for all query and mutation operations --- .../MockOperation.swift | 10 ----- Tests/ApolloTests/RequestChainTests.swift | 45 +++++++++++-------- .../Apollo/RequestChainNetworkTransport.swift | 3 +- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/Tests/ApolloInternalTestHelpers/MockOperation.swift b/Tests/ApolloInternalTestHelpers/MockOperation.swift index a6d05beab..faef67c5c 100644 --- a/Tests/ApolloInternalTestHelpers/MockOperation.swift +++ b/Tests/ApolloInternalTestHelpers/MockOperation.swift @@ -43,16 +43,6 @@ open class MockSubscription: MockOperation: MockOperation, GraphQLQuery { - - public override class var operationType: GraphQLOperationType { .query } - public override class var hasDeferredFragments: Bool { true } - - public static func mock() -> MockDeferredQuery where SelectionSet == MockSelectionSet { - MockDeferredQuery() - } -} - // MARK: - MockSelectionSets @dynamicMemberLookup diff --git a/Tests/ApolloTests/RequestChainTests.swift b/Tests/ApolloTests/RequestChainTests.swift index f24471196..6b99a66dc 100644 --- a/Tests/ApolloTests/RequestChainTests.swift +++ b/Tests/ApolloTests/RequestChainTests.swift @@ -371,7 +371,7 @@ class RequestChainTests: XCTestCase { wait(for: [expectation], timeout: 1) } - func test__request__givenDeferredOperation_shouldAddMultipartAcceptHeader() { + func test__request__givenQuery_shouldAddMultipartAcceptHeader() { let expectation = self.expectation(description: "Request header verified") let interceptor = RequestTrapInterceptor { request in @@ -389,14 +389,14 @@ class RequestChainTests: XCTestCase { endpointURL: TestURL.mockServer.url ) - _ = transport.send(operation: MockDeferredQuery.mock()) { result in + _ = transport.send(operation: MockQuery.mock()) { result in // noop } wait(for: [expectation], timeout: 1) } - func test__request__givenDeferredOperation_whenTransportInitializedWithAdditionalHeaders_shouldOverwriteOnlyAcceptHeader() { + func test__request__givenMutation_shouldAddMultipartAcceptHeader() { let expectation = self.expectation(description: "Request header verified") let interceptor = RequestTrapInterceptor { request in @@ -406,40 +406,42 @@ class RequestChainTests: XCTestCase { } XCTAssertEqual(header, "multipart/mixed;boundary=\"graphql\";\(MultipartResponseDeferParser.protocolSpec),application/json") - XCTAssertNotNil(request.allHTTPHeaderFields?["Random"]) expectation.fulfill() } let transport = RequestChainNetworkTransport( interceptorProvider: MockInterceptorProvider([interceptor]), - endpointURL: TestURL.mockServer.url, - additionalHeaders: [ - "Accept": "multipart/mixed", - "Random": "still-here" - ] + endpointURL: TestURL.mockServer.url ) - _ = transport.send(operation: MockDeferredQuery.mock()) { result in + _ = transport.send(operation: MockMutation.mock()) { result in // noop } wait(for: [expectation], timeout: 1) } - func test__request__givenQuery_shouldNotAddMultipartAcceptHeader() { + func test__request__givenQuery_whenTransportInitializedWithAdditionalHeaders_shouldOverwriteOnlyAcceptHeader() { let expectation = self.expectation(description: "Request header verified") let interceptor = RequestTrapInterceptor { request in - if let header = request.allHTTPHeaderFields?["Accept"] { - XCTAssertFalse(header.contains("multipart/mixed")) + guard let header = request.allHTTPHeaderFields?["Accept"] else { + XCTFail() + return } + XCTAssertEqual(header, "multipart/mixed;boundary=\"graphql\";\(MultipartResponseDeferParser.protocolSpec),application/json") + XCTAssertNotNil(request.allHTTPHeaderFields?["Random"]) expectation.fulfill() } let transport = RequestChainNetworkTransport( interceptorProvider: MockInterceptorProvider([interceptor]), - endpointURL: TestURL.mockServer.url + endpointURL: TestURL.mockServer.url, + additionalHeaders: [ + "Accept": "multipart/mixed", + "Random": "still-here" + ] ) _ = transport.send(operation: MockQuery.mock()) { result in @@ -449,20 +451,27 @@ class RequestChainTests: XCTestCase { wait(for: [expectation], timeout: 1) } - func test__request__givenMutation_shouldNotAddMultipartAcceptHeader() { + func test__request__givenMutation_whenTransportInitializedWithAdditionalHeaders_shouldOverwriteOnlyAcceptHeader() { let expectation = self.expectation(description: "Request header verified") let interceptor = RequestTrapInterceptor { request in - if let header = request.allHTTPHeaderFields?["Accept"] { - XCTAssertFalse(header.contains("multipart/mixed")) + guard let header = request.allHTTPHeaderFields?["Accept"] else { + XCTFail() + return } + XCTAssertEqual(header, "multipart/mixed;boundary=\"graphql\";\(MultipartResponseDeferParser.protocolSpec),application/json") + XCTAssertNotNil(request.allHTTPHeaderFields?["Random"]) expectation.fulfill() } let transport = RequestChainNetworkTransport( interceptorProvider: MockInterceptorProvider([interceptor]), - endpointURL: TestURL.mockServer.url + endpointURL: TestURL.mockServer.url, + additionalHeaders: [ + "Accept": "multipart/mixed", + "Random": "still-here" + ] ) _ = transport.send(operation: MockMutation.mock()) { result in diff --git a/apollo-ios/Sources/Apollo/RequestChainNetworkTransport.swift b/apollo-ios/Sources/Apollo/RequestChainNetworkTransport.swift index 89984f25d..b60764b2c 100644 --- a/apollo-ios/Sources/Apollo/RequestChainNetworkTransport.swift +++ b/apollo-ios/Sources/Apollo/RequestChainNetworkTransport.swift @@ -105,9 +105,8 @@ open class RequestChainNetworkTransport: NetworkTransport { name: "Accept", value: "multipart/mixed;boundary=\"graphql\";\(MultipartResponseSubscriptionParser.protocolSpec),application/json" ) - } - if Operation.hasDeferredFragments { + } else { request.addHeader( name: "Accept", value: "multipart/mixed;boundary=\"graphql\";\(MultipartResponseDeferParser.protocolSpec),application/json"