Add unit testing

This commit is contained in:
Victor Bodinaud
2024-03-21 17:23:06 +01:00
parent 9f8a411438
commit a5ff5b34a3
2 changed files with 138 additions and 10 deletions

View File

@@ -11,6 +11,7 @@ public enum NetworkError: Error {
case serverError(String)
case decodingError
case invalidResponse
case notFound
}
extension NetworkError: LocalizedError {
@@ -19,18 +20,19 @@ extension NetworkError: LocalizedError {
case .badRequest:
return NSLocalizedString("Unable to perform request", comment: "badRequestError")
case .serverError(let errorMessage):
print(errorMessage)
return NSLocalizedString(errorMessage, comment: "serverError")
case .decodingError:
return NSLocalizedString("Unable to decode successfully", comment: "decodingError")
case .invalidResponse:
return NSLocalizedString("Invalid response", comment: "invalidResponse")
case .notFound:
return NSLocalizedString("Not Found", comment: "notFound")
}
}
}
public enum HTTPMethod {
case get([URLQueryItem])
case get([URLQueryItem]? = nil)
case post(Data?)
case put(Data?)
case patch(Data?)
@@ -110,14 +112,25 @@ public struct Hermes {
let (data, response) = try await session.data(for: request)
guard let _ = response as? HTTPURLResponse else {
guard let response = response as? HTTPURLResponse else {
throw NetworkError.invalidResponse
}
if response.statusCode != 200 {
switch response.statusCode {
case 404:
throw NetworkError.notFound
case 500:
throw NetworkError.serverError(response.description)
default:
throw NetworkError.invalidResponse
}
}
guard let result = try? JSONDecoder().decode(resource.modelType, from: data) else {
throw NetworkError.decodingError
}
return result
}
}