mas/Tests/masTests/Network/NetworkManagerTests.swift
Ross Goldberg 880843d8e6
Rename *Mock as Mock*.
Resolve #585

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2024-10-25 23:39:52 -04:00

94 lines
2.8 KiB
Swift

//
// NetworkManagerTests.swift
// masTests
//
// Created by Ben Chatelain on 1/5/19.
// Copyright © 2019 mas-cli. All rights reserved.
//
import XCTest
@testable import mas
class NetworkManagerTests: XCTestCase {
override func setUp() {
super.setUp()
MAS.initialize()
}
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()
XCTAssertEqual(response, data)
}
func testSuccessfulSyncResponse() 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 result = try manager.loadData(from: url).wait()
XCTAssertEqual(result, data)
}
func testFailureAsyncResponse() {
// Setup our objects
let session = MockNetworkSession()
let manager = NetworkManager(session: session)
session.error = MASError.noData
// 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
}
XCTAssertEqual(masError, MASError.noData)
}
}
func testFailureSyncResponse() {
// Setup our objects
let session = MockNetworkSession()
let manager = NetworkManager(session: session)
session.error = MASError.noData
// 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 error = error as? MASError else {
XCTFail("Error is of unexpected type.")
return
}
XCTAssertEqual(error, MASError.noData)
}
}
}