Implemented the User Agent middleware #6
@@ -18,8 +18,8 @@ enum InputValidationError: Error {
|
|||||||
case inputIsEmpty
|
case inputIsEmpty
|
||||||
/// An input is nil.
|
/// An input is nil.
|
||||||
case inputIsNil
|
case inputIsNil
|
||||||
/// An input does not comply with the user agent name requirements.
|
/// An input is not camel-case.
|
||||||
case inputNotAgentName
|
case inputNotCamelCase
|
||||||
/// An input does not comply with the consumer key requirements.
|
/// An input does not comply with the consumer key requirements.
|
||||||
case inputNotConsumerKey
|
case inputNotConsumerKey
|
||||||
/// An input does not comply with the consumer secret requirements.
|
/// An input does not comply with the consumer secret requirements.
|
||||||
|
|||||||
+10
-8
@@ -12,7 +12,8 @@
|
|||||||
//
|
//
|
||||||
// ===----------------------------------------------------------------------===
|
// ===----------------------------------------------------------------------===
|
||||||
|
|
||||||
struct AgentNameValidationRule: InputValidationRule {
|
/// A validation rule type that checks whether an input is camel-case or not.
|
||||||
|
struct CamelCaseValidationRule: InputValidationRule {
|
||||||
|
|
||||||
// MARK: Functions
|
// MARK: Functions
|
||||||
|
|
||||||
@@ -30,18 +31,18 @@ struct AgentNameValidationRule: InputValidationRule {
|
|||||||
|
|
||||||
// MARK: - Definitions
|
// MARK: - Definitions
|
||||||
|
|
||||||
extension InputValidationRule where Self == AgentNameValidationRule {
|
extension InputValidationRule where Self == CamelCaseValidationRule {
|
||||||
|
|
||||||
// MARK: Constants
|
// MARK: Constants
|
||||||
|
|
||||||
/// A validation rule that checks whether an input is camel-cased or not.
|
/// A validation rule that checks whether an input is camel-cased or not.
|
||||||
static var agentName: Self { .init() }
|
static var camelCase: Self { .init() }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Helpers
|
// MARK: - Helpers
|
||||||
|
|
||||||
private extension AgentNameValidationRule {
|
private extension CamelCaseValidationRule {
|
||||||
|
|
||||||
// MARK: Functions
|
// MARK: Functions
|
||||||
|
|
||||||
@@ -58,9 +59,9 @@ private extension AgentNameValidationRule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
guard input.fullyMatch(
|
guard input.fullyMatch(
|
||||||
pattern: .init(format: .Pattern.agentName)
|
pattern: .init(format: .Pattern.camelCase)
|
||||||
) else {
|
) else {
|
||||||
throw InputValidationError.inputNotAgentName
|
throw InputValidationError.inputNotCamelCase
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@@ -71,6 +72,7 @@ private extension AgentNameValidationRule {
|
|||||||
// MARK: - Constants
|
// MARK: - Constants
|
||||||
|
|
||||||
private extension String.Pattern {
|
private extension String.Pattern {
|
||||||
/// A regular expression pattern to match the user agent name input against.
|
/// A regular expression pattern that represents camel-cased inputs.
|
||||||
static let agentName = "^([A-Z]([a-z]|[0-9])+)+$"
|
static let camelCase = "([A-Z]([a-z]|[0-9])+)+"
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -107,6 +107,6 @@ private extension SecureValidationRule {
|
|||||||
// MARK: - Constants
|
// MARK: - Constants
|
||||||
|
|
||||||
private extension String.Pattern {
|
private extension String.Pattern {
|
||||||
/// A regular expression pattern to match the security inputs against.
|
/// A regular expression pattern that represents security inputs.
|
||||||
static let securityInput = "^([a-z]|[A-Z]){%d}$"
|
static let securityInput = "^([a-z]|[A-Z]){%d}$"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ struct ValidateInputUseCaseTests {
|
|||||||
@Test(arguments: zip(
|
@Test(arguments: zip(
|
||||||
Input.inputsAgentName,
|
Input.inputsAgentName,
|
||||||
Output.inputsAgentName
|
Output.inputsAgentName
|
||||||
)) func `validate agent name`(
|
)) func `validate camel case`(
|
||||||
input: String,
|
input: String,
|
||||||
expects error: InputValidationError?
|
expects error: InputValidationError?
|
||||||
) async throws {
|
) async throws {
|
||||||
@@ -106,15 +106,15 @@ struct ValidateInputUseCaseTests {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@Test("validate agent name", arguments: zip(
|
@Test("validate camel case", arguments: zip(
|
||||||
Input.inputsAgentName,
|
Input.inputsCamelCase,
|
||||||
Output.inputsAgentName
|
Output.inputsCamelCase
|
||||||
)) func validateAgentName(
|
)) func validateCamelCase(
|
||||||
input: String,
|
input: String,
|
||||||
expects error: InputValidationError?
|
expects error: InputValidationError?
|
||||||
) async throws {
|
) async throws {
|
||||||
try assertValidate(
|
try assertValidate(
|
||||||
rule: .agentName,
|
rule: .camelCase,
|
||||||
input: input,
|
input: input,
|
||||||
expects: error
|
expects: error
|
||||||
)
|
)
|
||||||
@@ -231,8 +231,8 @@ private extension ValidateInputUseCaseTests {
|
|||||||
// MARK: - Constants
|
// MARK: - Constants
|
||||||
|
|
||||||
private extension Input {
|
private extension Input {
|
||||||
/// A list of inputs to validate against a user agent name validation rule.
|
/// A list of inputs to validate against a camel-case validation rule.
|
||||||
static let inputsAgentName: [String] = ["SampleApp", "Sample4pp", "SampleApp1", "SampleApp🚀", "Sample App", "Sample-App", "Sample_App"]
|
static let inputsCamelCase: [String] = ["SampleApp", "Sample4pp", "SampleApp1", "SampleApp🚀", "Sample App", "Sample-App", "Sample_App"]
|
||||||
/// A list of inputs to validate against the not empty validation rule.
|
/// A list of inputs to validate against the not empty validation rule.
|
||||||
static let inputsNotEmpty: [String] = ["Something", .empty]
|
static let inputsNotEmpty: [String] = ["Something", .empty]
|
||||||
/// A list of inputs to validate against the not nil validation rule.
|
/// A list of inputs to validate against the not nil validation rule.
|
||||||
@@ -246,8 +246,8 @@ private extension Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private extension Output {
|
private extension Output {
|
||||||
/// A list of expected input validation errors to be thrown after validating inputs against the user agent name validation rule.
|
/// A list of expected input validation errors to be thrown after validating inputs against the camel-case validation rule.
|
||||||
static let inputsAgentName: [InputValidationError?] = [nil, nil, nil, .inputNotAgentName, .inputNotAgentName, .inputNotAgentName, .inputNotAgentName]
|
static let inputsCamelCase: [InputValidationError?] = [nil, nil, nil, .inputNotCamelCase, .inputNotCamelCase, .inputNotCamelCase, .inputNotCamelCase]
|
||||||
/// A list of expected input validation errors to be thrown after validating inputs against the not empty validation rule.
|
/// A list of expected input validation errors to be thrown after validating inputs against the not empty validation rule.
|
||||||
static let inputsNotEmpty: [InputValidationError?] = [nil, .inputIsEmpty]
|
static let inputsNotEmpty: [InputValidationError?] = [nil, .inputIsEmpty]
|
||||||
/// A list of expected input validation errors to be thrown after validating inputs against the not nil validation rule.
|
/// A list of expected input validation errors to be thrown after validating inputs against the not nil validation rule.
|
||||||
|
|||||||
Reference in New Issue
Block a user