Added the "init(uriRoot:)" initializer function for the CheckURIUseCase use case in the library target and also, improved its test cases.

This commit is contained in:
2025-09-30 15:49:53 +02:00
parent 790433f1a7
commit 2c3474a1b8
2 changed files with 112 additions and 65 deletions
@@ -15,6 +15,22 @@ import struct HummingbirdCore.URI
/// A use case that checks whether a given URI against a set of conditions, to determine whether the URI could be used by the middleware or not. /// A use case that checks whether a given URI against a set of conditions, to determine whether the URI could be used by the middleware or not.
struct CheckURIUseCase { struct CheckURIUseCase {
// MARK: Properties
/// A root path that prefixes the documentation resource.
private let uriRoot: String
// MARK: Initializers
/// Initializes this use case.
///
/// > important: It is assumed that the `uriRoot` parameter is not empty and that it is prefixed by the `/` character.
///
/// - Parameter uriRoot: A root path that prefixes the documentation resource.
init(uriRoot: String) {
self.uriRoot = uriRoot
}
// MARK: Functions // MARK: Functions
/// Checks whether a provided URI against a set of conditions, so it could be used by the middleware. /// Checks whether a provided URI against a set of conditions, so it could be used by the middleware.
@@ -23,8 +39,8 @@ struct CheckURIUseCase {
func callAsFunction(_ uri: URI) -> String? { func callAsFunction(_ uri: URI) -> String? {
guard guard
let uriPath = uri.path.removingPercentEncoding, let uriPath = uri.path.removingPercentEncoding,
!uriPath.contains(.Path.previousFolder), uriPath.hasPrefix(uriRoot),
uriPath.hasPrefix(.Path.forwardSlash) !uriPath.contains(.Path.previousFolder)
else { else {
return nil return nil
} }
@@ -19,17 +19,15 @@ import struct HummingbirdCore.URI
@Suite("Check URI use case", .tags(.useCase)) @Suite("Check URI use case", .tags(.useCase))
struct CheckURIUseCaseTests { struct CheckURIUseCaseTests {
// MARK: Properties
private let useCase: CheckURIUseCase = .init()
// MARK: Use case tests // MARK: Use case tests
#if swift(>=6.2) #if swift(>=6.2)
@Test(arguments: zip( @Test(
arguments: zip(
Input.nonEncodedURIs, Input.nonEncodedURIs,
Output.nonEncodedURIs Output.nonEncodedURIs
)) )
)
func `check non encoded URIs`( func `check non encoded URIs`(
uri uriPath: String, uri uriPath: String,
expects result: String? expects result: String?
@@ -37,10 +35,12 @@ struct CheckURIUseCaseTests {
assertURI(uriPath, expects: result) assertURI(uriPath, expects: result)
} }
@Test(arguments: zip( @Test(
arguments: zip(
Input.percentEncodedURIs, Input.percentEncodedURIs,
Output.percentEncodedURIs Output.percentEncodedURIs
)) )
)
func `check percent-encoded URIs`( func `check percent-encoded URIs`(
uri uriPath: String, uri uriPath: String,
expects result: String? expects result: String?
@@ -48,10 +48,13 @@ struct CheckURIUseCaseTests {
assertURI(uriPath, expects: result) assertURI(uriPath, expects: result)
} }
#else #else
@Test("check non-encoded URIs", arguments: zip( @Test(
"check non-encoded URIs",
arguments: zip(
Input.nonEncodedURIs, Input.nonEncodedURIs,
Output.nonEncodedURIs Output.nonEncodedURIs
)) )
)
func check_nonEncodedURIs( func check_nonEncodedURIs(
uri uriPath: String, uri uriPath: String,
expects result: String? expects result: String?
@@ -59,10 +62,13 @@ struct CheckURIUseCaseTests {
assertURI(uriPath, expects: result) assertURI(uriPath, expects: result)
} }
@Test("check percent-encoded URIs", arguments: zip( @Test(
"check percent-encoded URIs",
arguments: zip(
Input.percentEncodedURIs, Input.percentEncodedURIs,
Output.percentEncodedURIs Output.percentEncodedURIs
)) )
)
func check_percentEncodedURIs( func check_percentEncodedURIs(
uri uriPath: String, uri uriPath: String,
expects result: String? expects result: String?
@@ -75,19 +81,22 @@ struct CheckURIUseCaseTests {
// MARK: - Assertions // MARK: - Assertions
private extension CheckURIUseCaseTests { extension CheckURIUseCaseTests {
// MARK: Functions // MARK: Functions
/// Asserts a URI path provided by the ``CheckURIPathUseCase`` use case based on a given path and an expected result. /// Asserts a URI path provided by the ``CheckURIPathUseCase`` use case based on a given path and an expected result.
/// - Parameters: /// - Parameters:
/// - uriPath: A URI path to use with a URI type. /// - uriPath: A URI path to use with a URI type.
/// - uriRoot: A URI path that prefixes the `DocC` documentation resources.
/// - result: An expected result coming out of the use case. /// - result: An expected result coming out of the use case.
func assertURI( fileprivate func assertURI(
_ uriPath: String, _ uriPath: String,
uriRoot: String = .Sample.uriRoot,
expects result: String? expects result: String?
) { ) {
// GIVEN // GIVEN
let useCase = CheckURIUseCase(uriRoot: uriRoot)
let uri = URI(uriPath) let uri = URI(uriPath)
// WHEN // WHEN
@@ -101,16 +110,38 @@ private extension CheckURIUseCaseTests {
// MARK: - Constants // MARK: - Constants
private extension Input { extension Input {
/// A list of non-encoded URI samples. /// A list of non-encoded URI samples.
static let nonEncodedURIs: [String] = ["/", "/some/known/path", "", "/some/../path", "some/other/path"] fileprivate static let nonEncodedURIs: [String] = [
.Sample.uriRoot + .empty,
.Sample.uriRoot + .Path.forwardSlash,
.Sample.uriRoot + "/some/known/path",
.Sample.uriRoot + "/some/../path",
"some/other/root/some/known/path",
]
/// A list of percent-encoded URI samples. /// A list of percent-encoded URI samples.
static let percentEncodedURIs: [String] = ["%2F", "/some%2Fknown%3Fpath", "%20", "/some/%2E%2E/path", "some%2Fother%3Fpath"] fileprivate static let percentEncodedURIs: [String] = [
.Sample.uriRoot + "%2F",
.Sample.uriRoot + "/some%2Fknown%3Fpath",
.Sample.uriRoot + "/some/%2E%2E/path",
"some/other%2Froot/some%2Fknown%3Fpath",
]
} }
private extension Output { extension Output {
/// A list of expected outputs for the non-encoded URI samples. /// A list of expected outputs for the non-encoded URI samples.
static let nonEncodedURIs: [String?] = ["/", "/some/known/path", "/", nil, nil] fileprivate static let nonEncodedURIs: [String?] = [
.Sample.uriRoot,
.Sample.uriRoot + .Path.forwardSlash,
.Sample.uriRoot + "/some/known/path",
nil,
nil,
]
/// A list of expected outputs for the percent-encoded URI samples. /// A list of expected outputs for the percent-encoded URI samples.
static let percentEncodedURIs: [String?] = ["/", "/some/known?path", nil, nil, nil] fileprivate static let percentEncodedURIs: [String?] = [
.Sample.uriRoot + .Path.forwardSlash,
.Sample.uriRoot + "/some/known?path",
nil,
nil,
]
} }