128 lines
4.5 KiB
Swift
128 lines
4.5 KiB
Swift
import XCTest
|
|
@testable import Hermes
|
|
|
|
final class HermesTests: XCTestCase {
|
|
|
|
func testGetRequest() async throws {
|
|
let hermes = Hermes()
|
|
let queryParams = [URLQueryItem(name: "foo", value: "bar")]
|
|
let resource = Resource(url: URL(string: "https://httpbin.org/get")!, method: .get(queryParams), modelType: GetResponse.self)
|
|
|
|
let getResponse = try await hermes.load(resource)
|
|
|
|
XCTAssertEqual(getResponse.url, "https://httpbin.org/get?foo=bar")
|
|
XCTAssertEqual(getResponse.args.count, 1)
|
|
XCTAssertGreaterThan(getResponse.headers.count, 0)
|
|
}
|
|
|
|
func testPostRequest() async throws {
|
|
let hermes = Hermes()
|
|
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
|
let resource = Resource(url: URL(string: "https://httpbin.org/post")!, method: .post(postDatas), modelType: PostResponse.self)
|
|
|
|
let postResponse = try await hermes.load(resource)
|
|
|
|
XCTAssertEqual(postResponse.url, "https://httpbin.org/post")
|
|
XCTAssertEqual(postResponse.args.count, 0)
|
|
XCTAssertNotNil(postResponse.json)
|
|
XCTAssertEqual(postResponse.json!.count, 1)
|
|
XCTAssertGreaterThan(postResponse.headers.count, 0)
|
|
}
|
|
|
|
func testPutRequest() async throws {
|
|
let hermes = Hermes()
|
|
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
|
let resource = Resource(url: URL(string: "https://httpbin.org/put")!, method: .put(postDatas), modelType: PostResponse.self)
|
|
|
|
let putResponse = try await hermes.load(resource)
|
|
|
|
XCTAssertEqual(putResponse.url, "https://httpbin.org/put")
|
|
XCTAssertEqual(putResponse.args.count, 0)
|
|
XCTAssertNotNil(putResponse.json)
|
|
XCTAssertEqual(putResponse.json!.count, 1)
|
|
XCTAssertGreaterThan(putResponse.headers.count, 0)
|
|
}
|
|
|
|
func testPatchRequest() async throws {
|
|
let hermes = Hermes()
|
|
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
|
let resource = Resource(url: URL(string: "https://httpbin.org/patch")!, method: .patch(postDatas), modelType: PostResponse.self)
|
|
|
|
let patchResponse = try await hermes.load(resource)
|
|
|
|
XCTAssertEqual(patchResponse.url, "https://httpbin.org/patch")
|
|
XCTAssertEqual(patchResponse.args.count, 0)
|
|
XCTAssertNotNil(patchResponse.json)
|
|
XCTAssertEqual(patchResponse.json!.count, 1)
|
|
XCTAssertGreaterThan(patchResponse.headers.count, 0)
|
|
}
|
|
|
|
func testDeleteRequest() async throws {
|
|
let hermes = Hermes()
|
|
let resource = Resource(url: URL(string: "https://httpbin.org/delete")!, method: .delete, modelType: PostResponse.self)
|
|
|
|
let deleteResponse = try await hermes.load(resource)
|
|
|
|
XCTAssertEqual(deleteResponse.url, "https://httpbin.org/delete")
|
|
XCTAssertEqual(deleteResponse.args.count, 0)
|
|
XCTAssertNil(deleteResponse.json)
|
|
XCTAssertGreaterThan(deleteResponse.headers.count, 0)
|
|
}
|
|
|
|
func testNotFound() async throws {
|
|
let hermes = Hermes()
|
|
let resource = Resource(url: URL(string: "https://httpbin.org/status/404")!, method: .get(), modelType: GetResponse.self)
|
|
|
|
do {
|
|
let _ = try await hermes.load(resource)
|
|
} catch NetworkError.notFound {
|
|
XCTAssert(true)
|
|
}
|
|
}
|
|
|
|
func testServerError() async throws {
|
|
let hermes = Hermes()
|
|
let resource = Resource(url: URL(string: "https://httpbin.org/status/500")!, method: .get(), modelType: GetResponse.self)
|
|
|
|
do {
|
|
let _ = try await hermes.load(resource)
|
|
} catch NetworkError.serverError(_) {
|
|
XCTAssert(true)
|
|
}
|
|
}
|
|
|
|
func testDecodeError() async throws {
|
|
let hermes = Hermes()
|
|
let resource = Resource(url: URL(string: "https://httpbin.org/get")!, method: .get(), modelType: DummyResponse.self)
|
|
|
|
do {
|
|
let _ = try await hermes.load(resource)
|
|
} catch NetworkError.decodingError {
|
|
XCTAssert(true)
|
|
}
|
|
}
|
|
}
|
|
|
|
struct GetResponse: Codable {
|
|
let args: [String: String]
|
|
let headers: [String: String]
|
|
let origin: String
|
|
let url: String
|
|
}
|
|
|
|
struct PostResponse: Codable {
|
|
let args: [String: String]
|
|
let data: String
|
|
let files: [String: String]
|
|
let form: [String: String]
|
|
let headers: [String: String]
|
|
let json: [String: String]?
|
|
let origin: String
|
|
let url: String
|
|
}
|
|
|
|
struct DummyResponse: Codable {
|
|
let dumb: Int
|
|
let dumber: Double
|
|
}
|