Implemented the User Agent middleware (#6)

This PR contains the work done to implement the `UserAgentMiddleware` middleware that includes user agent information into a header of the requests sent by the `Client` type, as defined in the [Discogs documentation](https://www.discogs.com/developers/#page:home,header:home-general-information). For this purpose, the `CamelCaseValidationRule`, `SemanticVersionValidationRule` and `URLValidationRule` types were implemented and integrated into the existing `ValidateInputUseCase` type.

Reviewed-on: #6
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #6.
This commit is contained in:
2025-10-13 00:54:17 +00:00
committed by Javier Cicchelli
parent 24d703b967
commit 791ebf4f78
1988 changed files with 2882 additions and 2010 deletions
@@ -15,7 +15,6 @@
import struct Foundation.URL
import struct Foundation.URLComponents
import struct Foundation.URLQueryItem
import struct HTTPTypes.HTTPField
import struct HTTPTypes.HTTPFields
import struct HTTPTypes.HTTPRequest
import struct HTTPTypes.HTTPResponse
@@ -28,6 +27,7 @@ import Testing
struct AuthMiddlewareTests {
// MARK: Initializers tests
#if swift(>=6.2)
@Test(arguments: Input.authMethods)
func `initialize`(
@@ -273,11 +273,11 @@ private extension AuthMiddlewareTests {
}
}
/// Asserts the error throwing (if justified) during the initialization of the middleware.
/// Asserts the error throwing (if justified) during the initialization of a middleware.
/// - Parameters:
/// - authMethod: A representation of an authentication method.
/// - authTransport: A representation of an authentication transport.
/// - error: An expected error of type ``InputValidationError`` during the initialization of the middleware.
/// - error: An expected error of type ``InputValidationError`` during the initialization of a middleware.
func assertInitThrows(
authMethod: AuthMethod,
authTransport: AuthTransport,
@@ -331,8 +331,8 @@ private extension AuthMiddlewareTests {
try await middleware.intercept(
request,
body: nil,
baseURL: .baseURL,
operationID: .operationId
baseURL: .Sample.baseURL,
operationID: .Sample.operationId
) { request, _, _ in
// THEN
switch (authMethod, authTransport) {
@@ -430,31 +430,6 @@ private extension AuthMiddlewareTests {
}
private extension HTTPRequest {
// MARK: Initializers
/// Initializes a HTTP request conveniently.
/// - Parameters:
/// - method: A request method.
/// - path: A value of the :path pseudo header field.
/// - headerFields: A dictionary of request header fields.
init(
method: HTTPRequest.Method = .get,
path: String?,
headerFields: HTTPFields = [:]
) {
self.init(
method: method,
scheme: nil,
authority: nil,
path: path,
headerFields: headerFields
)
}
}
// MARK: - Constants
private extension Input {
@@ -464,7 +439,7 @@ private extension Input {
.user(token: "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStT"),
.none
]
/// A list of authentication methods to tests for the initialization throw test cases.
/// A list of authentication methods to use in the initialization throw test cases.
static let authMethodsThrows: [AuthMethod] = authMethods + [
.consumer(key: .empty, secret: "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP"),
.consumer(key: "aAbBcCdDeEfFgGhHiI", secret: "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP"),
@@ -487,13 +462,3 @@ private extension Output {
/// A list of expected boolean flags coming from the should authenticate test cases.
static let authMethodsShouldAuthenticate: [Bool] = [true, true, false]
}
private extension String {
/// An operation ID sample.
static let operationId = "SomeOperationId"
}
private extension URL {
/// A base URL sample.
static let baseURL = URL(string: "https://sample.domain.com")!
}