✨ Add global configuration, refacto & updated testing
This commit is contained in:
@@ -1,14 +1,24 @@
|
||||
import XCTest
|
||||
@testable import Hermes
|
||||
import XCTest
|
||||
|
||||
final class HermesTests: XCTestCase {
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
configureHermes()
|
||||
}
|
||||
|
||||
private func configureHermes() {
|
||||
let defaultHeaders = ["Custom-Header": "HeaderValue"]
|
||||
let decodingStrategy = JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase
|
||||
let config = HermesConfiguration(defaultHeaders: defaultHeaders, decodingStrategy: decodingStrategy)
|
||||
Hermes.shared.configure(config)
|
||||
}
|
||||
|
||||
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)
|
||||
let getResponse = try await Hermes.shared.load(resource)
|
||||
|
||||
XCTAssertEqual(getResponse.url, "https://httpbin.org/get?foo=bar")
|
||||
XCTAssertEqual(getResponse.args.count, 1)
|
||||
@@ -16,52 +26,48 @@ final class HermesTests: XCTestCase {
|
||||
}
|
||||
|
||||
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)
|
||||
let postResponse = try await Hermes.shared.load(resource)
|
||||
|
||||
XCTAssertEqual(postResponse.url, "https://httpbin.org/post")
|
||||
XCTAssertEqual(postResponse.args.count, 0)
|
||||
XCTAssertNotNil(postResponse.json)
|
||||
XCTAssertEqual(postResponse.json!.count, 1)
|
||||
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)
|
||||
let putResponse = try await Hermes.shared.load(resource)
|
||||
|
||||
XCTAssertEqual(putResponse.url, "https://httpbin.org/put")
|
||||
XCTAssertEqual(putResponse.args.count, 0)
|
||||
XCTAssertNotNil(putResponse.json)
|
||||
XCTAssertEqual(putResponse.json!.count, 1)
|
||||
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)
|
||||
let patchResponse = try await Hermes.shared.load(resource)
|
||||
|
||||
XCTAssertEqual(patchResponse.url, "https://httpbin.org/patch")
|
||||
XCTAssertEqual(patchResponse.args.count, 0)
|
||||
XCTAssertNotNil(patchResponse.json)
|
||||
XCTAssertEqual(patchResponse.json!.count, 1)
|
||||
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)
|
||||
let deleteResponse = try await Hermes.shared.load(resource)
|
||||
|
||||
XCTAssertEqual(deleteResponse.url, "https://httpbin.org/delete")
|
||||
XCTAssertEqual(deleteResponse.args.count, 0)
|
||||
@@ -70,35 +76,41 @@ final class HermesTests: XCTestCase {
|
||||
}
|
||||
|
||||
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)
|
||||
let _ = try await Hermes.shared.load(resource)
|
||||
XCTFail("Expected to throw NetworkError.notFound")
|
||||
} catch NetworkError.notFound {
|
||||
XCTAssert(true)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
let _ = try await Hermes.shared.load(resource)
|
||||
XCTFail("Expected to throw NetworkError.serverError")
|
||||
} catch NetworkError.serverError(_) {
|
||||
XCTAssert(true)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
let _ = try await Hermes.shared.load(resource)
|
||||
XCTFail("Expected to throw NetworkError.decodingError")
|
||||
} catch NetworkError.decodingError {
|
||||
XCTAssert(true)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user