88
99import Apollo
1010import XCTest
11+ import StarWarsAPI
1112
1213class ErrorGenerationTests : XCTestCase {
1314
14- func testLocalizedStringFromErrorResponse( ) throws {
15+ private func checkExtensions( on error: GraphQLError ,
16+ expectedDict: [ String : Any ? ] ,
17+ file: StaticString = #filePath,
18+ line: UInt = #line) {
19+ XCTAssertEqual ( error. extensions? . count,
20+ expectedDict. count,
21+ file: file,
22+ line: line)
23+
24+ for (key, expectedValue) in expectedDict {
25+ let actualValue = error. extensions ? [ key] as? String
26+ let stringExpectedValue = expectedValue as? String
27+ XCTAssertEqual ( actualValue,
28+ stringExpectedValue,
29+ " Value for key \( key) did not match expected value \( String ( describing: stringExpectedValue) ) , it was \( String ( describing: actualValue) ) " ,
30+ file: file,
31+ line: line)
32+ }
33+ }
34+
35+ func testSingleErrorParsing( ) throws {
1536 let json = """
1637{
38+ " data " : {
39+ " hero " : null
40+ },
1741 " errors " : [
1842 {
1943 " message " : " Invalid client auth token. " ,
@@ -25,24 +49,30 @@ class ErrorGenerationTests: XCTestCase {
2549}
2650"""
2751
28- let response = HTTPURLResponse ( url: URL ( string: " https://www.fake.com " ) !,
29- statusCode: 403 ,
30- httpVersion: nil ,
31- headerFields: nil ) !
32-
3352 let data = try XCTUnwrap ( json. data ( using: . utf8) ,
3453 " Couldn't create json data " )
54+ let deserialized = try JSONSerializationFormat . deserialize ( data: data)
55+ let jsonObject = try XCTUnwrap ( deserialized as? JSONObject )
56+ let response = GraphQLResponse ( operation: HeroNameQuery ( ) , body: jsonObject)
57+ let result = try response. parseResultFast ( )
58+ XCTAssertNotNil ( result. data)
59+ XCTAssertNil ( result. data? . hero)
3560
36- let httpResponseError = GraphQLHTTPResponseError ( body: data,
37- response: response,
38- kind: . errorResponse)
39- XCTAssertEqual ( httpResponseError. graphQLErrors? . count, 1 )
40- XCTAssertEqual ( httpResponseError. localizedDescription, " Received error response: Invalid client auth token. " )
61+ XCTAssertEqual ( result. errors? . count, 1 )
62+ let error = try XCTUnwrap ( result. errors? . first)
63+ XCTAssertEqual ( error. message, " Invalid client auth token. " )
64+
65+ self . checkExtensions ( on: error, expectedDict: [
66+ " code " : " INTERNAL_SERVER_ERROR "
67+ ] )
4168 }
4269
4370 func testLocalizedStringFromErrorResponseWithMultipleErrors( ) throws {
4471 let json = """
4572{
73+ " data " : {
74+ " hero " : null
75+ },
4676 " errors " : [
4777 {
4878 " message " : " Invalid client auth token. " ,
@@ -60,52 +90,27 @@ class ErrorGenerationTests: XCTestCase {
6090}
6191"""
6292
63- let response = HTTPURLResponse ( url: URL ( string: " https://www.fake.com " ) !,
64- statusCode: 403 ,
65- httpVersion: nil ,
66- headerFields: nil ) !
67-
6893 let data = try XCTUnwrap ( json. data ( using: . utf8) ,
6994 " Couldn't create json data " )
95+ let deserialized = try JSONSerializationFormat . deserialize ( data: data)
96+ let jsonObject = try XCTUnwrap ( deserialized as? JSONObject )
97+ let response = GraphQLResponse ( operation: HeroNameQuery ( ) , body: jsonObject)
98+ let result = try response. parseResultFast ( )
99+ XCTAssertNotNil ( result. data)
100+ XCTAssertNil ( result. data? . hero)
70101
71- let httpResponseError = GraphQLHTTPResponseError ( body: data,
72- response: response,
73- kind: . errorResponse)
74- XCTAssertEqual ( httpResponseError. graphQLErrors? . count, 2 )
75- XCTAssertEqual ( httpResponseError. localizedDescription, " Received error response: Invalid client auth token. \n Server is having a sad. " )
76- }
77-
78- func testLocalizedStringFromPlaintextResponse( ) throws {
79- let text = " The server is having a sad. "
80-
81- let response = HTTPURLResponse ( url: URL ( string: " https://www.fake.com " ) !,
82- statusCode: 500 ,
83- httpVersion: nil ,
84- headerFields: nil ) !
85-
86- let data = try XCTUnwrap ( text. data ( using: . utf8) ,
87- " Couldn't create text data " )
88-
102+ let errors = try XCTUnwrap ( result. errors)
89103
90- let httpResponseError = GraphQLHTTPResponseError ( body: data,
91- response: response,
92- kind: . errorResponse)
104+ XCTAssertEqual ( errors. count, 2 )
105+ XCTAssertEqual ( errors. map { $0. message } , [
106+ " Invalid client auth token. " ,
107+ " Server is having a sad. " ,
108+ ] )
93109
94- XCTAssertNil ( httpResponseError. graphQLErrors)
95- XCTAssertEqual ( httpResponseError. localizedDescription, " Received error response (500 internal server error): The server is having a sad. " )
96- }
97-
98- func testLocalizedStringFromNullDataResponse( ) {
99- let response = HTTPURLResponse ( url: URL ( string: " https://www.fake.com " ) !,
100- statusCode: 500 ,
101- httpVersion: nil ,
102- headerFields: nil ) !
103-
104- let httpResponseError = GraphQLHTTPResponseError ( body: nil ,
105- response: response,
106- kind: . errorResponse)
107-
108- XCTAssertNil ( httpResponseError. graphQLErrors)
109- XCTAssertEqual ( httpResponseError. localizedDescription, " Received error response (500 internal server error): [Empty response body] " )
110+ for error in errors {
111+ self . checkExtensions ( on: error, expectedDict: [
112+ " code " : " INTERNAL_SERVER_ERROR "
113+ ] )
114+ }
110115 }
111116}
0 commit comments