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

Fix uploads not getting additionalHeaders #1644

Merged
merged 4 commits into from
Jan 29, 2021
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
8 changes: 6 additions & 2 deletions Sources/Apollo/RequestChainNetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ open class RequestChainNetworkTransport: NetworkTransport {
contextIdentifier: contextIdentifier,
clientName: self.clientName,
clientVersion: self.clientVersion,
additionalHeaders: additionalHeaders,
additionalHeaders: self.additionalHeaders,
cachePolicy: cachePolicy,
autoPersistQueries: self.autoPersistQueries,
useGETForQueries: self.useGETForQueries,
Expand Down Expand Up @@ -110,16 +110,20 @@ extension RequestChainNetworkTransport: UploadingNetworkTransport {
/// - Parameters:
/// - operation: The operation to create a request for
/// - files: The files you wish to upload
/// - manualBoundary: [optional] A manually set boundary for your upload request. Defaults to nil.
/// - Returns: The created request.
open func constructUploadRequest<Operation: GraphQLOperation>(
for operation: Operation,
with files: [GraphQLFile]) -> HTTPRequest<Operation> {
with files: [GraphQLFile],
manualBoundary: String? = nil) -> HTTPRequest<Operation> {

UploadRequest(graphQLEndpoint: self.endpointURL,
operation: operation,
clientName: self.clientName,
clientVersion: self.clientVersion,
additionalHeaders: self.additionalHeaders,
files: files,
manualBoundary: manualBoundary,
requestBodyCreator: self.requestBodyCreator)
}

Expand Down
53 changes: 34 additions & 19 deletions Tests/ApolloTests/UploadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class UploadTests: XCTestCase {
let store = ApolloStore()
let provider = LegacyInterceptorProvider(store: store)
let transport = RequestChainNetworkTransport(interceptorProvider: provider,
endpointURL: self.uploadClientURL)
endpointURL: self.uploadClientURL,
additionalHeaders: ["headerKey": "headerValue"])
transport.clientName = "test"
transport.clientVersion = "test"

return ApolloClient(networkTransport: transport, store: store)
}()
Expand Down Expand Up @@ -295,12 +298,17 @@ class UploadTests: XCTestCase {
mimeType: "text/plain",
fileURL: alphaFileUrl)
let operation = UploadOneFileMutation(file: alphaFile.originalName)
let uploadRequest = UploadRequest(graphQLEndpoint: TestURL.mockServer.url,
operation: operation,
clientName: "test",
clientVersion: "test",
files: [alphaFile],
manualBoundary: "TEST.BOUNDARY")

let transport = try XCTUnwrap(self.client.networkTransport as? RequestChainNetworkTransport)

let httpRequest = transport.constructUploadRequest(for: operation,
with: [alphaFile],
manualBoundary: "TEST.BOUNDARY")
let uploadRequest = try XCTUnwrap(httpRequest as? UploadRequest)

let urlRequest = try uploadRequest.toURLRequest()
XCTAssertEqual(urlRequest.allHTTPHeaderFields?["headerKey"], "headerValue")

let formData = try uploadRequest.requestMultipartFormData()
let stringToCompare = try formData.toTestString()

Expand Down Expand Up @@ -340,12 +348,16 @@ Alpha file content.

let files = [alphaFile, betaFile]
let operation = UploadMultipleFilesToTheSameParameterMutation(files: files.map { $0.originalName })
let uploadRequest = UploadRequest(graphQLEndpoint: TestURL.mockServer.url,
operation: operation,
clientName: "test",
clientVersion: "test",
files: files,
manualBoundary: "TEST.BOUNDARY")
let transport = try XCTUnwrap(self.client.networkTransport as? RequestChainNetworkTransport)

let httpRequest = transport.constructUploadRequest(for: operation,
with: [alphaFile, betaFile],
manualBoundary: "TEST.BOUNDARY")
let uploadRequest = try XCTUnwrap(httpRequest as? UploadRequest)

let urlRequest = try uploadRequest.toURLRequest()
XCTAssertEqual(urlRequest.allHTTPHeaderFields?["headerKey"], "headerValue")

let multipartData = try uploadRequest.requestMultipartFormData()
let stringToCompare = try multipartData.toTestString()

Expand Down Expand Up @@ -394,12 +406,15 @@ Bravo file content.
mimeType: "text/plain",
fileURL: charlieFileUrl)

let uploadRequest = UploadRequest(graphQLEndpoint: TestURL.mockServer.url,
operation: HeroNameQuery(),
clientName: "test",
clientVersion: "test",
files: [alphaFile, betaFile, charlieFile],
manualBoundary: "TEST.BOUNDARY")
let transport = try XCTUnwrap(self.client.networkTransport as? RequestChainNetworkTransport)

let httpRequest = transport.constructUploadRequest(for: HeroNameQuery(),
with: [alphaFile, betaFile, charlieFile],
manualBoundary: "TEST.BOUNDARY")
let uploadRequest = try XCTUnwrap(httpRequest as? UploadRequest)

let urlRequest = try uploadRequest.toURLRequest()
XCTAssertEqual(urlRequest.allHTTPHeaderFields?["headerKey"], "headerValue")

let multipartData = try uploadRequest.requestMultipartFormData()
let stringToCompare = try multipartData.toTestString()
Expand Down