Implemented the MakeURLRequestUseCase use case.
This commit is contained in:
parent
128d11a3cf
commit
4f25f19833
@ -0,0 +1,50 @@
|
|||||||
|
//
|
||||||
|
// MakeURLRequestUseCase.swift
|
||||||
|
// APICore
|
||||||
|
//
|
||||||
|
// Created by Javier Cicchelli on 10/04/2023.
|
||||||
|
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
/// This use case generate a url request out of a given endpoint.
|
||||||
|
public struct MakeURLRequestUseCase {
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
|
||||||
|
/// Generate a `URLRequest` instance out of a given endpoint that conforms to the `Endpoint` protocol.
|
||||||
|
/// - Parameter endpoint: An endpoint which is used to generate a `URLRequest` instance from.
|
||||||
|
/// - Returns: A `URLRequest` instance filled with data provided by the given endpoint.
|
||||||
|
func callAsFunction(endpoint: some Endpoint) throws -> URLRequest {
|
||||||
|
var urlComponents = URLComponents()
|
||||||
|
|
||||||
|
urlComponents.scheme = endpoint.scheme
|
||||||
|
urlComponents.host = endpoint.host
|
||||||
|
urlComponents.path = endpoint.path
|
||||||
|
|
||||||
|
if let port = endpoint.port {
|
||||||
|
urlComponents.port = port
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let url = urlComponents.url else {
|
||||||
|
throw MakeURLRequestError.urlNotCreated
|
||||||
|
}
|
||||||
|
|
||||||
|
var urlRequest = URLRequest(url: url)
|
||||||
|
|
||||||
|
urlRequest.httpMethod = endpoint.method.rawValue
|
||||||
|
urlRequest.httpBody = endpoint.body
|
||||||
|
urlRequest.allHTTPHeaderFields = endpoint.headers
|
||||||
|
|
||||||
|
return urlRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Errors
|
||||||
|
|
||||||
|
enum MakeURLRequestError: Error {
|
||||||
|
case urlNotCreated
|
||||||
|
}
|
||||||
|
|
@ -1,11 +0,0 @@
|
|||||||
import XCTest
|
|
||||||
@testable import Libraries
|
|
||||||
|
|
||||||
final class LibrariesTests: XCTestCase {
|
|
||||||
func testExample() throws {
|
|
||||||
// This is an example of a functional test case.
|
|
||||||
// Use XCTAssert and related functions to verify your tests produce the correct
|
|
||||||
// results.
|
|
||||||
XCTAssertEqual(Libraries().text, "Hello, World!")
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,119 @@
|
|||||||
|
//
|
||||||
|
// MakeURLRequestUseCaseTests.swift
|
||||||
|
// APICoreTests
|
||||||
|
//
|
||||||
|
// Created by Javier Cicchelli on 10/04/2023.
|
||||||
|
// Copyright © 2023 Röck+Cöde. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
@testable import APICore
|
||||||
|
|
||||||
|
final class MakeURLRequestUseCaseTests: XCTestCase {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
private let makeURLRequest = MakeURLRequestUseCase()
|
||||||
|
|
||||||
|
// MARK: Test cases
|
||||||
|
|
||||||
|
func test_withEndpoint_initialisedByDefault() throws {
|
||||||
|
// GIVEN
|
||||||
|
let endpoint = TestEndpoint()
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
let result = try makeURLRequest(endpoint: endpoint)
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
XCTAssertNotNil(result)
|
||||||
|
XCTAssertEqual(result.url?.absoluteString, "http://www.something.com/path/to/endpoint")
|
||||||
|
XCTAssertEqual(result.httpMethod, HTTPRequestMethod.get.rawValue)
|
||||||
|
XCTAssertEqual(result.allHTTPHeaderFields, [:])
|
||||||
|
XCTAssertNil(result.httpBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_withEndpoint_initialisedWithPort() throws {
|
||||||
|
// GIVEN
|
||||||
|
let endpoint = TestEndpoint(port: 8080)
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
let result = try makeURLRequest(endpoint: endpoint)
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
XCTAssertNotNil(result)
|
||||||
|
XCTAssertEqual(result.url?.absoluteString, "http://www.something.com:8080/path/to/endpoint")
|
||||||
|
XCTAssertEqual(result.httpMethod, HTTPRequestMethod.get.rawValue)
|
||||||
|
XCTAssertEqual(result.allHTTPHeaderFields, [:])
|
||||||
|
XCTAssertNil(result.httpBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_withEndpoint_initialisedWithHeaders() throws {
|
||||||
|
// GIVEN
|
||||||
|
let endpoint = TestEndpoint(headers: [
|
||||||
|
"aHeader": "aValueForHead",
|
||||||
|
"someOtherHeader": "someValueForOtherHeader"
|
||||||
|
])
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
let result = try makeURLRequest(endpoint: endpoint)
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
XCTAssertNotNil(result)
|
||||||
|
XCTAssertEqual(result.url?.absoluteString, "http://www.something.com/path/to/endpoint")
|
||||||
|
XCTAssertEqual(result.httpMethod, HTTPRequestMethod.get.rawValue)
|
||||||
|
XCTAssertEqual(result.allHTTPHeaderFields, [
|
||||||
|
"aHeader": "aValueForHead",
|
||||||
|
"someOtherHeader": "someValueForOtherHeader"
|
||||||
|
])
|
||||||
|
XCTAssertNil(result.httpBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
func test_withEndpoint_initialisedWithBody() throws {
|
||||||
|
// GIVEN
|
||||||
|
let data = "This is some data for a body of a request".data(using: .utf8)
|
||||||
|
let endpoint = TestEndpoint(body: data)
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
let result = try makeURLRequest(endpoint: endpoint)
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
XCTAssertNotNil(result)
|
||||||
|
XCTAssertEqual(result.url?.absoluteString, "http://www.something.com/path/to/endpoint")
|
||||||
|
XCTAssertEqual(result.httpMethod, HTTPRequestMethod.get.rawValue)
|
||||||
|
XCTAssertEqual(result.allHTTPHeaderFields, [:])
|
||||||
|
XCTAssertEqual(result.httpBody, data)
|
||||||
|
XCTAssertNotNil(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - TestEndpoint
|
||||||
|
|
||||||
|
private struct TestEndpoint: Endpoint {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
let scheme: String = "http"
|
||||||
|
let host: String = "www.something.com"
|
||||||
|
let path: String = "/path/to/endpoint"
|
||||||
|
let method: HTTPRequestMethod = .get
|
||||||
|
|
||||||
|
var port: Int?
|
||||||
|
var headers: [String : String]
|
||||||
|
var body: Data?
|
||||||
|
|
||||||
|
// MARK: Initialisers
|
||||||
|
|
||||||
|
init(
|
||||||
|
port: Int? = nil,
|
||||||
|
headers: [String : String] = [:],
|
||||||
|
body: Data? = nil
|
||||||
|
) {
|
||||||
|
self.port = port
|
||||||
|
self.body = body
|
||||||
|
self.headers = headers
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -11,17 +11,13 @@ import XCTest
|
|||||||
@testable import Locations
|
@testable import Locations
|
||||||
|
|
||||||
final class GetLocationsEndpointTests: XCTestCase {
|
final class GetLocationsEndpointTests: XCTestCase {
|
||||||
|
|
||||||
// MARK: Properties
|
|
||||||
|
|
||||||
private var endpoint: GetLocationsEndpoint!
|
|
||||||
|
|
||||||
// MARK: Tests
|
// MARK: Tests
|
||||||
|
|
||||||
func test_init() {
|
func test_init() {
|
||||||
// GIVEN
|
// GIVEN
|
||||||
// WHEN
|
// WHEN
|
||||||
endpoint = GetLocationsEndpoint()
|
let endpoint = GetLocationsEndpoint()
|
||||||
|
|
||||||
// THEN
|
// THEN
|
||||||
XCTAssertNotNil(endpoint)
|
XCTAssertNotNil(endpoint)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user