diff --git a/Libraries/Sources/APIService/Enumerations/ResponseStatus.swift b/Libraries/Sources/APIService/Enumerations/ResponseStatus.swift index 8d09e02..0895820 100644 --- a/Libraries/Sources/APIService/Enumerations/ResponseStatus.swift +++ b/Libraries/Sources/APIService/Enumerations/ResponseStatus.swift @@ -11,6 +11,7 @@ enum ResponseStatus: Int { case created = 201 case noContent = 204 case badRequest = 400 + case unauthorized = 401 case forbidden = 403 case notFound = 404 } diff --git a/Libraries/Sources/APIService/Structs/APIClient.swift b/Libraries/Sources/APIService/Structs/APIClient.swift index 2397fd3..ca7c67f 100644 --- a/Libraries/Sources/APIService/Structs/APIClient.swift +++ b/Libraries/Sources/APIService/Structs/APIClient.swift @@ -50,7 +50,8 @@ extension APIClient: Client { throw APIClientError.notSupported case .badRequest: throw APIClientError.itemAlreadyExist - case .forbidden: + case .unauthorized, + .forbidden: throw APIClientError.authenticationFailed case .notFound: throw APIClientError.itemDoesNotExist @@ -79,7 +80,8 @@ extension APIClient: Client { throw APIClientError.notSupported case .badRequest: throw APIClientError.itemIsNotFile - case .forbidden: + case .unauthorized, + .forbidden: throw APIClientError.authenticationFailed case .notFound: throw APIClientError.itemDoesNotExist diff --git a/Libraries/Tests/APIServiceTests/Cases/Structs/APIClient+RequestTests.swift b/Libraries/Tests/APIServiceTests/Cases/Structs/APIClient+RequestTests.swift index 2c2e977..40851f0 100644 --- a/Libraries/Tests/APIServiceTests/Cases/Structs/APIClient+RequestTests.swift +++ b/Libraries/Tests/APIServiceTests/Cases/Structs/APIClient+RequestTests.swift @@ -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 { // GIVEN 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 { // GIVEN let endpoint = GetDataEndpoint(