2019-01-05 17:37:34 +00:00
|
|
|
//
|
|
|
|
// NetworkManagerTests.swift
|
2024-10-01 18:05:41 +00:00
|
|
|
// masTests
|
2019-01-05 17:37:34 +00:00
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 1/5/19.
|
|
|
|
// Copyright © 2019 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
|
2024-10-01 18:05:41 +00:00
|
|
|
@testable import mas
|
2021-03-22 05:25:18 +00:00
|
|
|
|
2019-01-05 17:37:34 +00:00
|
|
|
class NetworkManagerTests: XCTestCase {
|
2024-10-21 11:40:14 +00:00
|
|
|
override func setUp() {
|
2021-04-23 07:01:18 +00:00
|
|
|
super.setUp()
|
2024-10-25 17:21:19 +00:00
|
|
|
MAS.initialize()
|
2021-04-23 07:01:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 02:15:57 +00:00
|
|
|
func testSuccessfulAsyncResponse() throws {
|
2019-01-05 17:37:34 +00:00
|
|
|
// Setup our objects
|
2024-10-26 03:09:31 +00:00
|
|
|
let session = MockNetworkSession()
|
2019-01-05 17:37:34 +00:00
|
|
|
let manager = NetworkManager(session: session)
|
|
|
|
|
|
|
|
// Create data and tell the session to always return it
|
2019-03-18 22:56:20 +00:00
|
|
|
let data = Data([0, 1, 0, 1])
|
2019-01-05 17:37:34 +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-04-23 02:15:57 +00:00
|
|
|
let response = try manager.loadData(from: url).wait()
|
2021-04-22 00:04:44 +00:00
|
|
|
XCTAssertEqual(response, data)
|
2019-01-05 17:37:34 +00:00
|
|
|
}
|
2019-01-06 02:59:12 +00:00
|
|
|
|
2021-04-17 02:22:55 +00:00
|
|
|
func testSuccessfulSyncResponse() throws {
|
2019-01-06 02:59:12 +00:00
|
|
|
// Setup our objects
|
2024-10-26 03:09:31 +00:00
|
|
|
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
|
2019-03-18 22:56:20 +00:00
|
|
|
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()
|
2021-04-17 02:22:55 +00:00
|
|
|
XCTAssertEqual(result, data)
|
2019-01-06 02:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFailureAsyncResponse() {
|
|
|
|
// Setup our objects
|
2024-10-26 03:09:31 +00:00
|
|
|
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-04-23 02:15:57 +00:00
|
|
|
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
|
|
|
|
2021-04-23 02:15:57 +00:00
|
|
|
XCTAssertEqual(masError, MASError.noData)
|
|
|
|
}
|
2019-01-06 02:59:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFailureSyncResponse() {
|
|
|
|
// Setup our objects
|
2024-10-26 03:09:31 +00:00
|
|
|
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 {
|
2021-04-17 02:22:55 +00:00
|
|
|
XCTFail("Error is of unexpected type.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-22 00:04:44 +00:00
|
|
|
XCTAssertEqual(error, MASError.noData)
|
2021-04-17 02:22:55 +00:00
|
|
|
}
|
2019-01-06 02:59:12 +00:00
|
|
|
}
|
2019-01-05 17:37:34 +00:00
|
|
|
}
|