Added support for the "unauthorized" response status in the APIClient client.
This commit is contained in:
parent
29e19d8fdf
commit
7670d5b7a0
@ -11,6 +11,7 @@ enum ResponseStatus: Int {
|
|||||||
case created = 201
|
case created = 201
|
||||||
case noContent = 204
|
case noContent = 204
|
||||||
case badRequest = 400
|
case badRequest = 400
|
||||||
|
case unauthorized = 401
|
||||||
case forbidden = 403
|
case forbidden = 403
|
||||||
case notFound = 404
|
case notFound = 404
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,8 @@ extension APIClient: Client {
|
|||||||
throw APIClientError.notSupported
|
throw APIClientError.notSupported
|
||||||
case .badRequest:
|
case .badRequest:
|
||||||
throw APIClientError.itemAlreadyExist
|
throw APIClientError.itemAlreadyExist
|
||||||
case .forbidden:
|
case .unauthorized,
|
||||||
|
.forbidden:
|
||||||
throw APIClientError.authenticationFailed
|
throw APIClientError.authenticationFailed
|
||||||
case .notFound:
|
case .notFound:
|
||||||
throw APIClientError.itemDoesNotExist
|
throw APIClientError.itemDoesNotExist
|
||||||
@ -79,7 +80,8 @@ extension APIClient: Client {
|
|||||||
throw APIClientError.notSupported
|
throw APIClientError.notSupported
|
||||||
case .badRequest:
|
case .badRequest:
|
||||||
throw APIClientError.itemIsNotFile
|
throw APIClientError.itemIsNotFile
|
||||||
case .forbidden:
|
case .unauthorized,
|
||||||
|
.forbidden:
|
||||||
throw APIClientError.authenticationFailed
|
throw APIClientError.authenticationFailed
|
||||||
case .notFound:
|
case .notFound:
|
||||||
throw APIClientError.itemDoesNotExist
|
throw APIClientError.itemDoesNotExist
|
||||||
|
@ -157,6 +157,31 @@ final class APIClientRequestTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func test_withSomeEndpoint_andSomeModel_whenResponseStatusUnauthorized() async throws {
|
||||||
|
// GIVEN
|
||||||
|
let endpoint = GetMeEndpoint(
|
||||||
|
username: "username",
|
||||||
|
password: "password"
|
||||||
|
)
|
||||||
|
|
||||||
|
url = try makeURLRequest(endpoint: endpoint).url
|
||||||
|
|
||||||
|
MockURLProtocol.mockData[url] = MockURLResponse(
|
||||||
|
status: .unauthorized,
|
||||||
|
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_whenResponseStatusForbidden() async throws {
|
func test_withSomeEndpoint_andSomeModel_whenResponseStatusForbidden() async throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let endpoint = GetMeEndpoint(
|
let endpoint = GetMeEndpoint(
|
||||||
@ -333,6 +358,32 @@ final class APIClientRequestTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func test_withSomeEndpoint_whenResponseStatusUnauthorized() async throws {
|
||||||
|
// GIVEN
|
||||||
|
let endpoint = GetDataEndpoint(
|
||||||
|
itemId: UUID().uuidString,
|
||||||
|
username: "username",
|
||||||
|
password: "password"
|
||||||
|
)
|
||||||
|
|
||||||
|
url = try makeURLRequest(endpoint: endpoint).url
|
||||||
|
|
||||||
|
MockURLProtocol.mockData[url] = MockURLResponse(
|
||||||
|
status: .unauthorized,
|
||||||
|
headers: [:],
|
||||||
|
data: nil
|
||||||
|
)
|
||||||
|
|
||||||
|
// WHEN & THEN
|
||||||
|
do {
|
||||||
|
try await client.request(endpoint: endpoint)
|
||||||
|
} catch APIClientError.authenticationFailed {
|
||||||
|
XCTAssertTrue(true)
|
||||||
|
} catch {
|
||||||
|
XCTAssertTrue(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func test_withSomeEndpoint_whenResponseStatusForbidden() async throws {
|
func test_withSomeEndpoint_whenResponseStatusForbidden() async throws {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
let endpoint = GetDataEndpoint(
|
let endpoint = GetDataEndpoint(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user