1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Foundation
import XCTest
@testable import OldFella
final class NetworkFailureTests: XCTestCase {
func testCancellationIncludesWrappedErrors() {
XCTAssertTrue(NetworkFailure.isCancellation(CancellationError()))
XCTAssertTrue(NetworkFailure.isCancellation(URLError(.cancelled)))
let wrapped = NSError(domain: "wrapper", code: 1, userInfo: [NSUnderlyingErrorKey: URLError(.cancelled)])
XCTAssertTrue(NetworkFailure.isCancellation(ActionNotSentError(underlying: wrapped)))
XCTAssertFalse(NetworkFailure.isConnectionFailure(wrapped))
XCTAssertFalse(NetworkFailure.isCancellation(URLError(.timedOut)))
}
func testConnectionMessagesIncludeWrappedErrors() {
let offline = ActionNotSentError(underlying: URLError(.notConnectedToInternet))
XCTAssertTrue(NetworkFailure.isConnectionFailure(offline))
XCTAssertEqual(
NetworkFailure.message(for: offline),
"You are not connected to the internet. Check your connection and try again.")
XCTAssertEqual(NetworkFailure.message(for: URLError(.timedOut)), "The request timed out. Try again.")
XCTAssertEqual(
NetworkFailure.message(for: URLError(.networkConnectionLost)),
"The connection to the server failed. Try again.")
XCTAssertEqual(
NetworkFailure.message(for: URLError(.cannotFindHost)), "The connection to the server failed. Try again.")
}
func testOtherErrorsKeepTheirDescription() {
let error = NSError(domain: "example", code: 7, userInfo: [NSLocalizedDescriptionKey: "Example failure"])
XCTAssertFalse(NetworkFailure.isConnectionFailure(error))
XCTAssertEqual(NetworkFailure.message(for: error), "Example failure")
}
}