mas/Tests/masTests/Network/NetworkManagerTests.swift

95 lines
2.8 KiB
Swift
Raw Permalink Normal View History

//
// NetworkManagerTests.swift
// masTests
//
// Created by Ben Chatelain on 1/5/19.
// Copyright © 2019 mas-cli. All rights reserved.
//
import XCTest
@testable import mas
2021-03-22 05:25:18 +00:00
class NetworkManagerTests: XCTestCase {
override func setUp() {
2021-04-23 07:01:18 +00:00
super.setUp()
MAS.initialize()
2021-04-23 07:01:18 +00:00
}
func testSuccessfulAsyncResponse() throws {
// Setup our objects
let session = MockNetworkSession()
let manager = NetworkManager(session: session)
// Create data and tell the session to always return it
let data = Data([0, 1, 0, 1])
session.data = data
// Create a URL (using the file path API to avoid optionals)
let url = URL(fileURLWithPath: "url")
// Perform the request and verify the result
let response = try manager.loadData(from: url).wait()
2021-04-22 00:04:44 +00:00
XCTAssertEqual(response, data)
}
2019-01-06 02:59:12 +00:00
func testSuccessfulSyncResponse() throws {
2019-01-06 02:59:12 +00:00
// Setup our objects
let session = MockNetworkSession()
2019-01-06 02:59:12 +00:00
let manager = NetworkManager(session: session)
// Create data and tell the session to always return it
let data = Data([0, 1, 0, 1])
2019-01-06 02:59:12 +00:00
session.data = data
// Create a URL (using the file path API to avoid optionals)
let url = URL(fileURLWithPath: "url")
// Perform the request and verify the result
2021-05-08 17:17:43 +00:00
let result = try manager.loadData(from: url).wait()
XCTAssertEqual(result, data)
2019-01-06 02:59:12 +00:00
}
func testFailureAsyncResponse() {
// Setup our objects
let session = MockNetworkSession()
2019-01-06 02:59:12 +00:00
let manager = NetworkManager(session: session)
2021-04-22 00:04:44 +00:00
session.error = MASError.noData
2019-01-06 02:59:12 +00:00
// Create a URL (using the file path API to avoid optionals)
let url = URL(fileURLWithPath: "url")
// Perform the request and verify the result
XCTAssertThrowsError(try manager.loadData(from: url).wait()) { error in
guard let masError = error as? MASError else {
XCTFail("Error is of unexpected type.")
return
}
2021-04-22 00:04:44 +00:00
XCTAssertEqual(masError, MASError.noData)
}
2019-01-06 02:59:12 +00:00
}
func testFailureSyncResponse() {
// Setup our objects
let session = MockNetworkSession()
2019-01-06 02:59:12 +00:00
let manager = NetworkManager(session: session)
2021-04-22 00:04:44 +00:00
session.error = MASError.noData
2019-01-06 02:59:12 +00:00
// Create a URL (using the file path API to avoid optionals)
let url = URL(fileURLWithPath: "url")
// Perform the request and verify the result
2021-05-08 17:17:43 +00:00
XCTAssertThrowsError(try manager.loadData(from: url).wait()) { error in
2021-04-22 00:04:44 +00:00
guard let error = error as? MASError else {
XCTFail("Error is of unexpected type.")
return
}
2021-04-22 00:04:44 +00:00
XCTAssertEqual(error, MASError.noData)
}
2019-01-06 02:59:12 +00:00
}
}