Skip to content

Commit

Permalink
Use ResponseCodeError instead of obsolete error
Browse files Browse the repository at this point in the history
See apollographql/apollo-ios#1425,
which affects on not only 0.35.0 but 0.34.0.
  • Loading branch information
manicmaniac committed Jun 2, 2021
1 parent 5da2fda commit 807a921
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/Classes/ApolloDebugServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ extension ApolloDebugServer: HTTPServerDelegate {
let graphQLResult = try result.get()
let body = try JSONSerialization.data(withJSONObject: graphQLResult.jsonValue)
context.respondJSONData(body)
} catch let error as GraphQLHTTPResponseError {
let stream = context.respond(proxying: error.response)
if let body = error.body {
stream.write(data: body)
} catch ResponseCodeInterceptor.ResponseCodeError.invalidResponseCode(let response?, let rawData) {
let stream = context.respond(proxying: response)
if let rawData = rawData {
stream.write(data: rawData)
}
stream.close()
} catch let error {
Expand Down
31 changes: 31 additions & 0 deletions Tests/Classes/ApolloDebugServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,28 @@ class ApolloDebugServerTests: XCTestCase {
task.resume()
waitForExpectations(timeout: 10.0, handler: nil)
}

func testPostRequest_withServerInternalError() {
let url = server.serverURL!.appendingPathComponent("request")
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = serverInternalError
let expectation = self.expectation(description: "response should be received")
let task = session.dataTask(with: request) { data, response, error in
defer { expectation.fulfill() }
if let error = error {
return XCTFail(String(describing: error))
}
guard let response = response as? HTTPURLResponse else {
return XCTFail("unexpected response type")
}
XCTAssertEqual(response.statusCode, 500)
}
task.resume()
waitForExpectations(timeout: 10.0, handler: nil)
}
}

private class MockHTTPURLProtocol: URLProtocol {
Expand Down Expand Up @@ -627,6 +649,8 @@ private class MockHTTPURLProtocol: URLProtocol {
sendDataResponse(url: url, data: mutationResponse)
} else if query.hasPrefix("serverError") {
sendDataResponse(url: url, data: serverErrorResponse, statusCode: 200)
} else if query.hasPrefix("serverInternalError") {
sendErrorResponse(url: url, statusCode: 500)
} else {
sendErrorResponse(url: url, statusCode: 400)
}
Expand Down Expand Up @@ -779,3 +803,10 @@ private let serverErrorResponse = Data("""
""".utf8)

private let serverErrorResponseJSONObject = try! JSONSerialization.jsonObject(with: serverErrorResponse) as! NSDictionary

private let serverInternalError = Data("""
{
"operationName": "serverInternalError",
"query": "serverInternalError"
}
""".utf8)

0 comments on commit 807a921

Please sign in to comment.