Added support for the "forbidden" response status in the APIClient client.
This commit is contained in:
parent
74a22d5844
commit
29e19d8fdf
@ -11,5 +11,6 @@ enum ResponseStatus: Int {
|
||||
case created = 201
|
||||
case noContent = 204
|
||||
case badRequest = 400
|
||||
case forbidden = 403
|
||||
case notFound = 404
|
||||
}
|
||||
|
@ -50,6 +50,8 @@ extension APIClient: Client {
|
||||
throw APIClientError.notSupported
|
||||
case .badRequest:
|
||||
throw APIClientError.itemAlreadyExist
|
||||
case .forbidden:
|
||||
throw APIClientError.authenticationFailed
|
||||
case .notFound:
|
||||
throw APIClientError.itemDoesNotExist
|
||||
}
|
||||
@ -77,6 +79,8 @@ extension APIClient: Client {
|
||||
throw APIClientError.notSupported
|
||||
case .badRequest:
|
||||
throw APIClientError.itemIsNotFile
|
||||
case .forbidden:
|
||||
throw APIClientError.authenticationFailed
|
||||
case .notFound:
|
||||
throw APIClientError.itemDoesNotExist
|
||||
}
|
||||
@ -106,6 +110,7 @@ private extension APIClient {
|
||||
|
||||
public enum APIClientError: Error {
|
||||
case responseNotReturned
|
||||
case authenticationFailed
|
||||
case itemIsNotFile
|
||||
case itemAlreadyExist
|
||||
case itemDoesNotExist
|
||||
|
@ -157,6 +157,31 @@ final class APIClientRequestTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_withSomeEndpoint_andSomeModel_whenResponseStatusForbidden() async throws {
|
||||
// GIVEN
|
||||
let endpoint = GetMeEndpoint(
|
||||
username: "username",
|
||||
password: "password"
|
||||
)
|
||||
|
||||
url = try makeURLRequest(endpoint: endpoint).url
|
||||
|
||||
MockURLProtocol.mockData[url] = MockURLResponse(
|
||||
status: .forbidden,
|
||||
headers: [:],
|
||||
data: nil
|
||||
)
|
||||
|
||||
// WHEN & THEN
|
||||
do {
|
||||
_ = try await client.request(endpoint: endpoint, model: Me.self)
|
||||
} catch APIClientError.authenticationFailed {
|
||||
XCTAssertTrue(true)
|
||||
} catch {
|
||||
XCTAssertTrue(false)
|
||||
}
|
||||
}
|
||||
|
||||
func test_withSomeEndpoint_andSomeModel_whenResponseStatusNotFound() async throws {
|
||||
// GIVEN
|
||||
let endpoint = GetMeEndpoint(
|
||||
@ -308,6 +333,32 @@ final class APIClientRequestTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_withSomeEndpoint_whenResponseStatusForbidden() async throws {
|
||||
// GIVEN
|
||||
let endpoint = GetDataEndpoint(
|
||||
itemId: UUID().uuidString,
|
||||
username: "username",
|
||||
password: "password"
|
||||
)
|
||||
|
||||
url = try makeURLRequest(endpoint: endpoint).url
|
||||
|
||||
MockURLProtocol.mockData[url] = MockURLResponse(
|
||||
status: .forbidden,
|
||||
headers: [:],
|
||||
data: nil
|
||||
)
|
||||
|
||||
// WHEN & THEN
|
||||
do {
|
||||
try await client.request(endpoint: endpoint)
|
||||
} catch APIClientError.authenticationFailed {
|
||||
XCTAssertTrue(true)
|
||||
} catch {
|
||||
XCTAssertTrue(false)
|
||||
}
|
||||
}
|
||||
|
||||
func test_withSomeEndpoint_whenResponseStatusNotFound() async throws {
|
||||
// GIVEN
|
||||
let endpoint = GetDataEndpoint(
|
||||
|
Loading…
x
Reference in New Issue
Block a user