Add unit testing

This commit is contained in:
Victor Bodinaud
2024-03-21 17:23:06 +01:00
committed by Mahtan
parent 9f8a411438
commit 1e2159b248
4 changed files with 109 additions and 9 deletions

View File

@@ -2,11 +2,95 @@ import XCTest
@testable import Hermes
final class HermesTests: XCTestCase {
func testExample() throws {
// XCTest Documentation
// https://developer.apple.com/documentation/xctest
// Defining Test Cases and Test Methods
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
func test404() async throws {
let hermes = Hermes()
let resource = Resource(url: URL(string: "https://httpbin.org/404")!, method: .get(), modelType: GetResponse.self)
let getResponse = try await hermes.load(resource)
}
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)
}
}
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
}