Files
Hermes/Tests/HermesTests/HermesTests.swift
Victor Bodinaud e1b86ee6c8 Add unit testing
2024-01-14 20:58:48 +01:00

62 lines
2.1 KiB
Swift

import XCTest
@testable import Hermes
struct SomeCodableStruct: Codable, Equatable {
let url: String
var data: String? = nil
var json: SomeCodableJsonStruct? = nil
}
struct SomeCodableJsonStruct: Codable, Equatable {
var test: String
}
final class HermesTests: XCTestCase {
func testResourceInitialization() {
let url = URL(string: "https://httpbin.org/get")!
let resource = Resource<SomeCodableStruct>(url: url, method: .get([]), modelType: SomeCodableStruct.self)
XCTAssertEqual(resource.url, url)
XCTAssertEqual(resource.method.name, "GET")
XCTAssert(resource.modelType == SomeCodableStruct.self)
}
func testGetRequest() async throws {
let url = URL(string: "https://httpbin.org/get")!
let resource = Resource(url: url, method: .get([]), modelType: SomeCodableStruct.self)
let response = try await Hermes().load(resource)
XCTAssertNotNil(response)
XCTAssertEqual(response.url, "https://httpbin.org/get")
XCTAssertNil(response.data)
XCTAssertNil(response.json)
}
func testPostRequest() async throws {
let postDatas = ["test": "test"]
let url = URL(string: "https://httpbin.org/post")!
let resource = try Resource(url: url, method: .post(JSONEncoder().encode(postDatas)), modelType: SomeCodableStruct.self)
let response = try await Hermes().load(resource)
XCTAssertNotNil(response)
XCTAssertEqual(response.url, "https://httpbin.org/post")
XCTAssertEqual(response.data, "{\"test\":\"test\"}")
XCTAssert(response.json == SomeCodableJsonStruct(test: "test"))
}
func testDeleteRequest() async throws {
let url = URL(string: "https://httpbin.org/delete")!
let resource = Resource(url: url, method: .delete, modelType: SomeCodableStruct.self)
let response = try await Hermes().load(resource)
XCTAssertNotNil(response)
XCTAssertEqual(response.url, "https://httpbin.org/delete")
XCTAssertEqual(response.data, "")
XCTAssertNil(response.json)
}
}