From 6e03a97622a2dd1f34a65e8879cd74fedfc916cc Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sun, 4 Dec 2022 02:00:02 +0100 Subject: [PATCH] Implemented the GetItemsEndpoint endpoint. --- .../Endpoints/GetItemsEndpoint.swift | 42 +++++++++++ .../GetItemsEndpoint+InitTests.swift | 74 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 Libraries/Sources/APIService/Endpoints/GetItemsEndpoint.swift create mode 100644 Libraries/Tests/APIServiceTests/Cases/Endpoints/GetItemsEndpoint+InitTests.swift 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) + } + +}