Implemented the GetDataEndpoint endpoint.

This commit is contained in:
Javier Cicchelli 2022-12-04 02:13:40 +01:00
parent 6e03a97622
commit 90d13b2651
3 changed files with 119 additions and 3 deletions

View File

@ -0,0 +1,42 @@
//
// GetDataEndpoint.swift
// APIService
//
// Created by Javier Cicchelli on 04/12/2022.
// Copyright © 2022 Röck+Cöde. All rights reserved.
//
struct GetDataEndpoint: Endpoint {
let path: String
let method: RequestMethod
let credentials: BasicCredentials
let headers: [String : String]
let body: [String : String]?
}
// MARK: - Initialisers
extension GetDataEndpoint {
init(
itemId: String,
username: String,
password: String
) {
self.path = .init(format: .Formats.itemsDataWithId, itemId)
self.method = .get
self.credentials = .init(
username: username,
password: password
)
self.headers = [:]
self.body = nil
}
}
// MARK: - String+Formats
private extension String {
enum Formats {
static let itemsDataWithId = "/items/%@/data"
}
}

View File

@ -0,0 +1,74 @@
//
// GetDataEndpoint+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 GetDataEndpointInitTests: XCTestCase {
// MARK: Properties
let itemId = UUID().uuidString
var endpoint: GetDataEndpoint!
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)/data")
XCTAssertEqual(endpoint.method, .get)
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)/data")
XCTAssertEqual(endpoint.method, .get)
XCTAssertEqual(endpoint.credentials.username, username)
XCTAssertEqual(endpoint.credentials.password, password)
XCTAssertEqual(endpoint.headers, [:])
XCTAssertEqual(endpoint.authorizationHeader, [:])
XCTAssertNil(endpoint.body)
}
}

View File

@ -6,8 +6,8 @@
// Copyright © 2022 Röck+Cöde. All rights reserved.
//
import XCTest
import Foundation
import XCTest
@testable import APIService
@ -23,7 +23,7 @@ final class GetItemsEndpointInitTests: XCTestCase {
// MARK: Test cases
func test_withProperUsernameAndPassword() throws {
func test_withItemId_andProperUsernameAndPassword() throws {
// GIVEN
username = "username"
password = "password"
@ -47,7 +47,7 @@ final class GetItemsEndpointInitTests: XCTestCase {
XCTAssertNil(endpoint.body)
}
func test_withEmptyUsernameOrPassword() async throws {
func test_withItemId_andEmptyUsernameOrPassword() async throws {
// GIVEN
username = ""
password = "password"