|
| 1 | +// |
| 2 | +// ErrorGenerationTests.swift |
| 3 | +// Apollo |
| 4 | +// |
| 5 | +// Created by Ellen Shapiro on 9/9/19. |
| 6 | +// Copyright © 2019 Apollo GraphQL. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Apollo |
| 10 | +import XCTest |
| 11 | + |
| 12 | +class ErrorGenerationTests: XCTestCase { |
| 13 | + |
| 14 | + func testLocalizedStringFromErrorResponse() { |
| 15 | + let json = """ |
| 16 | +{ |
| 17 | + "errors": [ |
| 18 | + { |
| 19 | + "message": "Invalid client auth token.", |
| 20 | + "extensions": { |
| 21 | + "code": "INTERNAL_SERVER_ERROR" |
| 22 | + } |
| 23 | + } |
| 24 | + ] |
| 25 | +} |
| 26 | +""" |
| 27 | + |
| 28 | + let response = HTTPURLResponse(url: URL(string: "https://www.fake.com")!, |
| 29 | + statusCode: 403, |
| 30 | + httpVersion: nil, |
| 31 | + headerFields: nil)! |
| 32 | + |
| 33 | + guard let data = json.data(using: .utf8) else { |
| 34 | + XCTFail("Couldn't create json data") |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + let httpResponseError = GraphQLHTTPResponseError(body: data, |
| 39 | + response: response, |
| 40 | + kind: .errorResponse) |
| 41 | + XCTAssertEqual(httpResponseError.graphQLErrors?.count, 1) |
| 42 | + XCTAssertEqual(httpResponseError.localizedDescription, "Received error response: Invalid client auth token.") |
| 43 | + } |
| 44 | + |
| 45 | + func testLocalizedStringFromErrorResponseWithMultipleErrors() { |
| 46 | + let json = """ |
| 47 | +{ |
| 48 | + "errors": [ |
| 49 | + { |
| 50 | + "message": "Invalid client auth token.", |
| 51 | + "extensions": { |
| 52 | + "code": "INTERNAL_SERVER_ERROR" |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + "message": "Server is having a sad.", |
| 57 | + "extensions": { |
| 58 | + "code": "INTERNAL_SERVER_ERROR" |
| 59 | + } |
| 60 | + } |
| 61 | + ] |
| 62 | +} |
| 63 | +""" |
| 64 | + |
| 65 | + let response = HTTPURLResponse(url: URL(string: "https://www.fake.com")!, |
| 66 | + statusCode: 403, |
| 67 | + httpVersion: nil, |
| 68 | + headerFields: nil)! |
| 69 | + |
| 70 | + guard let data = json.data(using: .utf8) else { |
| 71 | + XCTFail("Couldn't create json data") |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + let httpResponseError = GraphQLHTTPResponseError(body: data, |
| 76 | + response: response, |
| 77 | + kind: .errorResponse) |
| 78 | + XCTAssertEqual(httpResponseError.graphQLErrors?.count, 2) |
| 79 | + XCTAssertEqual(httpResponseError.localizedDescription, "Received error response: Invalid client auth token.\nServer is having a sad.") |
| 80 | + } |
| 81 | + |
| 82 | + func testLocalizedStringFromPlaintextResponse() { |
| 83 | + let text = "The server is having a sad." |
| 84 | + |
| 85 | + let response = HTTPURLResponse(url: URL(string: "https://www.fake.com")!, |
| 86 | + statusCode: 500, |
| 87 | + httpVersion: nil, |
| 88 | + headerFields: nil)! |
| 89 | + |
| 90 | + guard let data = text.data(using: .utf8) else { |
| 91 | + XCTFail("Couldn't create text data") |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + let httpResponseError = GraphQLHTTPResponseError(body: data, |
| 96 | + response: response, |
| 97 | + kind: .errorResponse) |
| 98 | + |
| 99 | + XCTAssertNil(httpResponseError.graphQLErrors) |
| 100 | + XCTAssertEqual(httpResponseError.localizedDescription, "Received error response (500 internal server error): The server is having a sad.") |
| 101 | + } |
| 102 | + |
| 103 | + func testLocalizedStringFromNullDataResponse() { |
| 104 | + let response = HTTPURLResponse(url: URL(string: "https://www.fake.com")!, |
| 105 | + statusCode: 500, |
| 106 | + httpVersion: nil, |
| 107 | + headerFields: nil)! |
| 108 | + |
| 109 | + let httpResponseError = GraphQLHTTPResponseError(body: nil, |
| 110 | + response: response, |
| 111 | + kind: .errorResponse) |
| 112 | + |
| 113 | + XCTAssertNil(httpResponseError.graphQLErrors) |
| 114 | + XCTAssertEqual(httpResponseError.localizedDescription, "Received error response (500 internal server error): [Empty response body]") |
| 115 | + } |
| 116 | +} |
0 commit comments