From 01742cc4c644b2bc12f2ea2410821ee72e11162f Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sun, 4 Dec 2022 02:29:46 +0100 Subject: [PATCH] Implemented the DeleteItemEndpoint endpoint. --- .../Endpoints/DeleteItemEndpoint.swift | 34 +++++++++ .../Endpoints/GetDataEndpoint.swift | 6 +- .../Endpoints/GetItemsEndpoint.swift | 8 -- .../Extensions/String+Formats.swift | 13 ++++ .../MakeAuthorizationHeaderUseCase.swift | 8 +- .../DeleteItemEndpoint+InitTests.swift | 74 +++++++++++++++++++ 6 files changed, 126 insertions(+), 17 deletions(-) create mode 100644 Libraries/Sources/APIService/Endpoints/DeleteItemEndpoint.swift create mode 100644 Libraries/Sources/APIService/Extensions/String+Formats.swift create mode 100644 Libraries/Tests/APIServiceTests/Cases/Endpoints/DeleteItemEndpoint+InitTests.swift diff --git a/Libraries/Sources/APIService/Endpoints/DeleteItemEndpoint.swift b/Libraries/Sources/APIService/Endpoints/DeleteItemEndpoint.swift new file mode 100644 index 0000000..7ada098 --- /dev/null +++ b/Libraries/Sources/APIService/Endpoints/DeleteItemEndpoint.swift @@ -0,0 +1,34 @@ +// +// DeleteItemEndpoint.swift +// APIService +// +// Created by Javier Cicchelli on 04/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +struct DeleteItemEndpoint: Endpoint { + let path: String + let method: RequestMethod + let credentials: BasicCredentials + let headers: [String : String] + let body: [String : String]? +} + +// MARK: - Initialisers + +extension DeleteItemEndpoint { + init( + itemId: String, + username: String, + password: String + ) { + self.path = .init(format: .Formats.itemsWithId, itemId) + self.method = .delete + self.credentials = .init( + username: username, + password: password + ) + self.headers = [:] + self.body = nil + } +} diff --git a/Libraries/Sources/APIService/Endpoints/GetDataEndpoint.swift b/Libraries/Sources/APIService/Endpoints/GetDataEndpoint.swift index e3ab99a..c21bdf2 100644 --- a/Libraries/Sources/APIService/Endpoints/GetDataEndpoint.swift +++ b/Libraries/Sources/APIService/Endpoints/GetDataEndpoint.swift @@ -35,8 +35,6 @@ extension GetDataEndpoint { // MARK: - String+Formats -private extension String { - enum Formats { - static let itemsDataWithId = "/items/%@/data" - } +private extension String.Formats { + static let itemsDataWithId = "/items/%@/data" } diff --git a/Libraries/Sources/APIService/Endpoints/GetItemsEndpoint.swift b/Libraries/Sources/APIService/Endpoints/GetItemsEndpoint.swift index e5564fc..c60b263 100644 --- a/Libraries/Sources/APIService/Endpoints/GetItemsEndpoint.swift +++ b/Libraries/Sources/APIService/Endpoints/GetItemsEndpoint.swift @@ -32,11 +32,3 @@ extension GetItemsEndpoint { self.body = nil } } - -// MARK: - String+Formats - -private extension String { - enum Formats { - static let itemsWithId = "/items/%@" - } -} diff --git a/Libraries/Sources/APIService/Extensions/String+Formats.swift b/Libraries/Sources/APIService/Extensions/String+Formats.swift new file mode 100644 index 0000000..91ee174 --- /dev/null +++ b/Libraries/Sources/APIService/Extensions/String+Formats.swift @@ -0,0 +1,13 @@ +// +// String+Formats.swift +// APIService +// +// Created by Javier Cicchelli on 04/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +extension String { + enum Formats { + static let itemsWithId = "/items/%@" + } +} diff --git a/Libraries/Sources/APIService/Use Cases/MakeAuthorizationHeaderUseCase.swift b/Libraries/Sources/APIService/Use Cases/MakeAuthorizationHeaderUseCase.swift index 20b3080..49d7a54 100644 --- a/Libraries/Sources/APIService/Use Cases/MakeAuthorizationHeaderUseCase.swift +++ b/Libraries/Sources/APIService/Use Cases/MakeAuthorizationHeaderUseCase.swift @@ -38,9 +38,7 @@ enum MakeAuthorizationHeaderError: Error { // MARK: - String+Formats -private extension String { - enum Formats { - static let usernameAndPassword = "%@:%@" - static let authorizationValue = "Basic %@" - } +private extension String.Formats { + static let usernameAndPassword = "%@:%@" + static let authorizationValue = "Basic %@" } diff --git a/Libraries/Tests/APIServiceTests/Cases/Endpoints/DeleteItemEndpoint+InitTests.swift b/Libraries/Tests/APIServiceTests/Cases/Endpoints/DeleteItemEndpoint+InitTests.swift new file mode 100644 index 0000000..77c888c --- /dev/null +++ b/Libraries/Tests/APIServiceTests/Cases/Endpoints/DeleteItemEndpoint+InitTests.swift @@ -0,0 +1,74 @@ +// +// DeleteItemEndpoint+InitTests.swift +// APIServiceTests +// +// Created by Javier Cicchelli on 04/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +import Foundation +import XCTest + +@testable import APIService + +final class DeleteItemEndpoint_InitTests: XCTestCase { + + // MARK: Properties + + let itemId = UUID().uuidString + + var endpoint: DeleteItemEndpoint! + var username: String! + var password: String! + + // MARK: Test cases + + func test_withItemId_andProperUsernameAndPassword() throws { + // GIVEN + username = "username" + password = "password" + + // WHEN + endpoint = .init( + itemId: itemId, + username: username, + password: password + ) + + // THEN + XCTAssertEqual(endpoint.scheme, .Schemes.http) + XCTAssertEqual(endpoint.host, .Hosts.default) + XCTAssertEqual(endpoint.path, "/items/\(itemId)") + XCTAssertEqual(endpoint.method, .delete) + XCTAssertEqual(endpoint.credentials.username, username) + XCTAssertEqual(endpoint.credentials.password, password) + XCTAssertEqual(endpoint.headers, [:]) + XCTAssertEqual(endpoint.authorizationHeader, [.Header.Keys.authorization: "Basic dXNlcm5hbWU6cGFzc3dvcmQ="]) + XCTAssertNil(endpoint.body) + } + + func test_withItemId_andEmptyUsernameOrPassword() async throws { + // GIVEN + username = "" + password = "password" + + // WHEN + endpoint = .init( + itemId: itemId, + username: username, + password: password + ) + + // THEN + XCTAssertEqual(endpoint.scheme, .Schemes.http) + XCTAssertEqual(endpoint.host, .Hosts.default) + XCTAssertEqual(endpoint.path, "/items/\(itemId)") + XCTAssertEqual(endpoint.method, .delete) + XCTAssertEqual(endpoint.credentials.username, username) + XCTAssertEqual(endpoint.credentials.password, password) + XCTAssertEqual(endpoint.headers, [:]) + XCTAssertEqual(endpoint.authorizationHeader, [:]) + XCTAssertNil(endpoint.body) + } + +}