mirror of
https://github.com/mas-cli/mas
synced 2024-11-24 12:33:08 +00:00
d413d8cfa1
Move MasKitTests module to masTests. Rename MasKit enum as Mas. Upgrade swift-tools-version from 5.3 to 5.6.1. swift-tools-version 5.5+ is necessary to allow test code to import executable target code, to allow MasKit library code to be moved into the mas executable. Upgrade to swift-tools-version to 5.6.1 instead of to 5.5 because they support all the same macOS versions. Standardize comments. Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
94 lines
2.8 KiB
Swift
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 public func setUp() {
|
|
super.setUp()
|
|
Mas.initialize()
|
|
}
|
|
|
|
func testSuccessfulAsyncResponse() throws {
|
|
// Setup our objects
|
|
let session = NetworkSessionMock()
|
|
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 = NetworkSessionMock()
|
|
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 = NetworkSessionMock()
|
|
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 = NetworkSessionMock()
|
|
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)
|
|
}
|
|
}
|
|
}
|