Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: HTTP Accept header #127

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Tests/ApolloInternalTestHelpers/MockOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ open class MockSubscription<SelectionSet: RootSelectionSet>: MockOperation<Selec
}
}

open class MockDeferredQuery<SelectionSet: RootSelectionSet>: MockOperation<SelectionSet>, GraphQLQuery {

public override class var operationType: GraphQLOperationType { .query }
public override class var hasDeferredFragments: Bool { true }

public static func mock() -> MockDeferredQuery<MockSelectionSet> where SelectionSet == MockSelectionSet {
MockDeferredQuery<MockSelectionSet>()
}
}

// MARK: - MockSelectionSets

@dynamicMemberLookup
Expand Down
45 changes: 27 additions & 18 deletions Tests/ApolloTests/RequestChainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions apollo-ios/Sources/Apollo/RequestChainNetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading