diff --git a/Libraries/Sources/APIService/Endpoints/GetItemsEndpoint.swift b/Libraries/Sources/APIService/Endpoints/GetItemsEndpoint.swift new file mode 100644 index 0000000..e5564fc --- /dev/null +++ b/Libraries/Sources/APIService/Endpoints/GetItemsEndpoint.swift @@ -0,0 +1,42 @@ +// +// GetItemsEndpoint.swift +// APIService +// +// Created by Javier Cicchelli on 04/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +struct GetItemsEndpoint: Endpoint { + let path: String + let method: RequestMethod + let credentials: BasicCredentials + let headers: [String : String] + let body: [String : String]? +} + +// MARK: - Initialisers + +extension GetItemsEndpoint { + init( + itemId: String, + username: String, + password: String + ) { + self.path = .init(format: .Formats.itemsWithId, itemId) + self.method = .get + self.credentials = .init( + username: username, + password: password + ) + self.headers = [.Header.Keys.contentType: .Header.Values.contentTypeJSON] + self.body = nil + } +} + +// MARK: - String+Formats + +private extension String { + enum Formats { + static let itemsWithId = "/items/%@" + } +} diff --git a/Libraries/Tests/APIServiceTests/Cases/Endpoints/GetItemsEndpoint+InitTests.swift b/Libraries/Tests/APIServiceTests/Cases/Endpoints/GetItemsEndpoint+InitTests.swift new file mode 100644 index 0000000..5d1fc3d --- /dev/null +++ b/Libraries/Tests/APIServiceTests/Cases/Endpoints/GetItemsEndpoint+InitTests.swift @@ -0,0 +1,74 @@ +// +// GetItemsEndpoint+InitTests.swift +// APIServiceTests +// +// Created by Javier Cicchelli on 04/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +import XCTest +import Foundation + +@testable import APIService + +final class GetItemsEndpointInitTests: XCTestCase { + + // MARK: Properties + + let itemId = UUID().uuidString + + var endpoint: GetItemsEndpoint! + var username: String! + var password: String! + + // MARK: Test cases + + func test_withProperUsernameAndPassword() 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, .get) + XCTAssertEqual(endpoint.credentials.username, username) + XCTAssertEqual(endpoint.credentials.password, password) + XCTAssertEqual(endpoint.headers, [.Header.Keys.contentType: .Header.Values.contentTypeJSON]) + XCTAssertEqual(endpoint.authorizationHeader, [.Header.Keys.authorization: "Basic dXNlcm5hbWU6cGFzc3dvcmQ="]) + XCTAssertNil(endpoint.body) + } + + func test_withEmptyUsernameOrPassword() 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, .get) + XCTAssertEqual(endpoint.credentials.username, username) + XCTAssertEqual(endpoint.credentials.password, password) + XCTAssertEqual(endpoint.headers, [.Header.Keys.contentType: .Header.Values.contentTypeJSON]) + XCTAssertEqual(endpoint.authorizationHeader, [:]) + XCTAssertNil(endpoint.body) + } + +}