Skip to content

Commit

Permalink
refactor for fetchOption
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Hong committed Jul 3, 2019
1 parent 5a05122 commit b4dfc58
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Sources/Apollo/HTTPNetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class HTTPNetworkTransport: NetworkTransport {
/// - error: An error that indicates why a request failed, or `nil` if the request was succesful.
/// - Returns: An object that can be used to cancel an in progress request.
public func send<Operation>(operation: Operation, completionHandler: @escaping (_ response: GraphQLResponse<Operation>?, _ error: Error?) -> Void) -> Cancellable {

let headers = decorateRequestHeaders(for: operation)

let request = { () -> URLRequest in
Expand Down Expand Up @@ -73,7 +72,7 @@ public class HTTPNetworkTransport: NetworkTransport {
let requestRetry = { () -> URLRequest in
guard operation.operationType == .query else {
// fallback to normal call
return self.httpPostRequest(operation: operation, requestHeaders: headers, sendQueryDocument: true, autoPersistQueries: false)
return self.httpGetRequest(operation: operation, requestHeaders: headers, sendQueryDocument: true, autoPersistQueries: false)
}

// retry with query document
Expand Down
110 changes: 109 additions & 1 deletion Tests/Apollo/Network/HttpNetworkTransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,114 @@ class HttpNetworkTransportTests: XCTestCase {
persistedQuery: true,
variable: true)
}

func testUseGETForQueriesRequest() {
let network = HTTPNetworkTransport(url: URL(string: endpoint)!,
useGETForQueries: true,
enableAutoPersistedQueries: false,
useHttpGetMethodForPersistedQueries: false)
let mockSession = MockURLSession()
network.session = mockSession
let query = HeroNameQuery()
let _ = network.send(operation: query) { _,_ in }
let request = mockSession.lastRequest

XCTAssertNotNil(request)
XCTAssert(request!.url?.host == network.url.host)
XCTAssert(request!.httpMethod == "GET")

validateUrlParams(with: request!,
query: query,
queryDocument: true,
persistedQuery: false,
variable: false)
}

func testNotUseGETForQueriesRequest() {
let network = HTTPNetworkTransport(url: URL(string: endpoint)!,
useGETForQueries: false,
enableAutoPersistedQueries: false,
useHttpGetMethodForPersistedQueries: false)
let mockSession = MockURLSession()
network.session = mockSession
let query = HeroNameQuery()
let _ = network.send(operation: query) { _,_ in }
let request = mockSession.lastRequest

XCTAssertNotNil(request)
XCTAssert(request!.url?.host == network.url.host)
XCTAssert(request!.httpMethod == "POST")

validatePostBody(with: request!,
query: query,
queryDocument: true)
}

func testNotUseGETForQueriesAPQsRequest() {
let network = HTTPNetworkTransport(url: URL(string: endpoint)!,
useGETForQueries: false,
enableAutoPersistedQueries: true,
useHttpGetMethodForPersistedQueries: false)
let mockSession = MockURLSession()
network.session = mockSession
let query = HeroNameQuery(episode: .empire)
let _ = network.send(operation: query) { _,_ in }
let request = mockSession.lastRequest

XCTAssertNotNil(request)
XCTAssert(request!.url?.host == network.url.host)
XCTAssert(request!.httpMethod == "POST")

validatePostBody(with: request!,
query: query,
queryDocument: false,
persistedQuery: true,
variable: true)
}

func testUseGETForQueriesAPQsRequest() {
let network = HTTPNetworkTransport(url: URL(string: endpoint)!,
useGETForQueries: true,
enableAutoPersistedQueries: true,
useHttpGetMethodForPersistedQueries: false)
let mockSession = MockURLSession()
network.session = mockSession
let query = HeroNameQuery(episode: .empire)
let _ = network.send(operation: query) { _,_ in }
let request = mockSession.lastRequest

XCTAssertNotNil(request)
XCTAssert(request!.url?.host == network.url.host)
XCTAssert(request!.httpMethod == "GET")

validateUrlParams(with: request!,
query: query,
queryDocument: false,
persistedQuery: true,
variable: true)
}

func testNotUseGETForQueriesAPQsGETRequest() {
let network = HTTPNetworkTransport(url: URL(string: endpoint)!,
useGETForQueries: false,
enableAutoPersistedQueries: true,
useHttpGetMethodForPersistedQueries: true)
let mockSession = MockURLSession()
network.session = mockSession
let query = HeroNameQuery(episode: .empire)
let _ = network.send(operation: query) { _,_ in }
let request = mockSession.lastRequest

XCTAssertNotNil(request)
XCTAssert(request!.url?.host == network.url.host)
XCTAssert(request!.httpMethod == "GET")

validateUrlParams(with: request!,
query: query,
queryDocument: false,
persistedQuery: true,
variable: true)
}
}

// Mark: Helpers
Expand Down Expand Up @@ -135,7 +243,7 @@ private func validateUrlParams(with request: URLRequest,
XCTAssertNil(variables)
}

let ext = url.queryItems?["extensions"]!
let ext = url.queryItems?["extensions"]
if persistedQuery {
XCTAssertNotNil(ext)

Expand Down

0 comments on commit b4dfc58

Please sign in to comment.