Implemented the DeleteItemEndpoint endpoint.

This commit is contained in:
Javier Cicchelli 2022-12-04 02:29:46 +01:00
parent 90d13b2651
commit 01742cc4c6
6 changed files with 126 additions and 17 deletions

View File

@ -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
}
}

View File

@ -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"
}

View File

@ -32,11 +32,3 @@ extension GetItemsEndpoint {
self.body = nil
}
}
// MARK: - String+Formats
private extension String {
enum Formats {
static let itemsWithId = "/items/%@"
}
}

View File

@ -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/%@"
}
}

View File

@ -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 %@"
}

View File

@ -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)
}
}