Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Allows custom headers for provider verification (#95) #96

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
],

dependencies: [
.package(url: "https://github.com/surpher/PactSwiftMockServer.git", .exact("0.4.3"))
.package(url: "https://github.com/surpher/PactSwiftMockServer.git", .exact("0.4.4")),
],

targets: [
Expand Down
1 change: 1 addition & 0 deletions PactSwift.xcfilelist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
$(SRCROOT)/Sources/Extensions/Bundle+PactSwift.swift
$(SRCROOT)/Sources/Extensions/Date+PactSwift.swift
$(SRCROOT)/Sources/Extensions/UUID+PactSwift.swift
$(SRCROOT)/Sources/Extensions/CharacterSet+PactSwift.swift
$(SRCROOT)/Sources/Extensions/String+PactSwift.swift
$(SRCROOT)/Sources/Extensions/Dictionary+PactSwift.swift
$(SRCROOT)/Sources/Extensions/Sequence+PactSwift.swift
Expand Down
8 changes: 7 additions & 1 deletion PactSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
AD54435F27D32DCA00D4C464 /* DateTimeExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD54435D27D32DCA00D4C464 /* DateTimeExpression.swift */; };
AD54436127D3316600D4C464 /* DateTimeExpressionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD54436027D3316600D4C464 /* DateTimeExpressionTests.swift */; };
AD54436227D3316600D4C464 /* DateTimeExpressionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD54436027D3316600D4C464 /* DateTimeExpressionTests.swift */; };
AD5A6E7428E827E7001725D9 /* CharacterSet+PactSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD5A6E7328E827E7001725D9 /* CharacterSet+PactSwift.swift */; };
AD5A6E7528E827E7001725D9 /* CharacterSet+PactSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD5A6E7328E827E7001725D9 /* CharacterSet+PactSwift.swift */; };
AD5E9F0226D375BE0002580D /* ProviderVerifier+Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD5E9F0126D375BE0002580D /* ProviderVerifier+Error.swift */; };
AD5E9F0326D375BE0002580D /* ProviderVerifier+Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD5E9F0126D375BE0002580D /* ProviderVerifier+Error.swift */; };
AD5E9F0526D4684B0002580D /* WIPPacts.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD5E9F0426D4684B0002580D /* WIPPacts.swift */; };
Expand Down Expand Up @@ -283,6 +285,7 @@
AD4FC5DF242CC2CB0039342D /* Target-iOS-Tests-Shared.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Target-iOS-Tests-Shared.xcconfig"; sourceTree = "<group>"; };
AD54435D27D32DCA00D4C464 /* DateTimeExpression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTimeExpression.swift; sourceTree = "<group>"; };
AD54436027D3316600D4C464 /* DateTimeExpressionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTimeExpressionTests.swift; sourceTree = "<group>"; };
AD5A6E7328E827E7001725D9 /* CharacterSet+PactSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CharacterSet+PactSwift.swift"; sourceTree = "<group>"; };
AD5E9F0126D375BE0002580D /* ProviderVerifier+Error.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ProviderVerifier+Error.swift"; sourceTree = "<group>"; };
AD5E9F0426D4684B0002580D /* WIPPacts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WIPPacts.swift; sourceTree = "<group>"; };
AD641A322434331300785CE1 /* Bundle+PactSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+PactSwift.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -594,6 +597,7 @@
isa = PBXGroup;
children = (
AD641A322434331300785CE1 /* Bundle+PactSwift.swift */,
AD5A6E7328E827E7001725D9 /* CharacterSet+PactSwift.swift */,
ADA17E40251377A4004F1A82 /* Date+PactSwift.swift */,
ADB7C11C2432D41E00A16CDE /* Dictionary+PactSwift.swift */,
ADC372B3269877AF000DA90B /* Sequence+PactSwift.swift */,
Expand Down Expand Up @@ -1101,6 +1105,7 @@
ADE9B3C7250A3C4700274672 /* Matcher.swift in Sources */,
ADD031782512425800C6099B /* RandomInt.swift in Sources */,
AD72E54027B89CB900C7453F /* DateTime.swift in Sources */,
AD5A6E7428E827E7001725D9 /* CharacterSet+PactSwift.swift in Sources */,
AD9D7D8D26C8AD3400FE4137 /* ProviderStateGenerator.swift in Sources */,
ADD3C27624416DAE002E73B9 /* IncludesLike.swift in Sources */,
ADD0316F25122E4B00C6099B /* RandomUUID.swift in Sources */,
Expand Down Expand Up @@ -1227,6 +1232,7 @@
ADE9B3C8250A3C4700274672 /* Matcher.swift in Sources */,
ADD031792512425800C6099B /* RandomInt.swift in Sources */,
AD72E54127B89CB900C7453F /* DateTime.swift in Sources */,
AD5A6E7528E827E7001725D9 /* CharacterSet+PactSwift.swift in Sources */,
AD9D7D8E26C8AD3400FE4137 /* ProviderStateGenerator.swift in Sources */,
AD8FC7E82463BB9F00361854 /* PactHTTPMethod.swift in Sources */,
AD8FC7F72463BBB800361854 /* MatchingRuleExpressible.swift in Sources */,
Expand Down Expand Up @@ -1463,7 +1469,7 @@
repositoryURL = "https://github.com/surpher/PactSwiftMockServer";
requirement = {
kind = exactVersion;
version = 0.4.2;
version = 0.4.4;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
2 changes: 1 addition & 1 deletion Scripts/build_test_linux
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if [[ "$CI" == true ]]; then
echo "ℹ️ Changing location to ${LIBPACT_FFI_DIR}"
cd $LIBPACT_FFI_DIR
git fetch --all --tags
git checkout tags/libpact_ffi-v0.3.11
git checkout tags/libpact_ffi-v0.3.15
fi

export LD_LIBRARY_PATH="/usr/local/lib"
Expand Down
26 changes: 26 additions & 0 deletions Sources/Extensions/CharacterSet+PactSwift.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by Marko Justinek on 1/10/2022.
// Copyright © 2022 Marko Justinek. All rights reserved.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//

import Foundation

extension CharacterSet {

static var headerASCIIAllowed: CharacterSet {
CharacterSet(charactersIn: Unicode.Scalar(0x20)...Unicode.Scalar(0x7F))
}

}
32 changes: 32 additions & 0 deletions Sources/Model/ProviderVerifier+Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public extension ProviderVerifier {
/// URL to post state change requests to
let stateChangeURL: URL?

/// Custom header being passed to verifier
let customHeader: [String: String]?

/// Sets the log level
let logLevel: LogLevel

Expand All @@ -95,20 +98,23 @@ public extension ProviderVerifier {
/// - pactsSource: The locations of pacts
/// - filter: Only validates the interactions that match the filter
/// - stateChangeURL: URL to post state change requests to
/// - customHeader: A dictionary for header values where KEY and VALUE contain ASCII characters (32-127) only
/// - logLevel: Logging level
///
public init(
provider: Provider,
pactsSource: PactsSource,
filter: FilterPacts? = nil,
stateChangeURL: URL? = nil,
customHeader: [String: String]? = nil,
logLevel: LogLevel = .warn
) {
self.providerURL = provider.url
self.port = provider.port
self.pactsSource = pactsSource
self.filterPacts = filter
self.stateChangeURL = stateChangeURL
self.customHeader = customHeader
self.logLevel = logLevel
}
}
Expand Down Expand Up @@ -240,6 +246,17 @@ extension ProviderVerifier.Options {
newLineDelimitedArgs.append("--state-change-url\n\(stateChangeURL.absoluteString)")
}

// Custom headers
if let customHeader = customHeader, customHeader.isEmpty == false {
customHeader.forEach { key, value in
guard validate(key + value) else {
Logger.log(message: "Skipping a header key-value pair \"\(key)=\(value)\" containing nonallowed characters.")
return
}
newLineDelimitedArgs.append("--header\n\(key)=\(value)")
}
}

// Set logging level
newLineDelimitedArgs.append("--loglevel\n\(self.logLevel.rawValue)")

Expand All @@ -249,6 +266,21 @@ extension ProviderVerifier.Options {

}

// MARK: - Private extensions

private extension ProviderVerifier.Options {

/// Validates input against ASCII (32-127) characters
///
/// - parameter input: A string value to validate
///
/// Returns `true` if string contains ASCII characters (32-127) only.
func validate(_ input: String) -> Bool {
input.unicodeScalars.allSatisfy { CharacterSet.headerASCIIAllowed.contains($0) }
}

}

private extension Date {

/// Date represented as string in short ISO8601 format (eg: "2021-08-24")
Expand Down
23 changes: 23 additions & 0 deletions Tests/Model/ProviderVerifier+OptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,29 @@ final class ProviderVerifierOptionsTests: XCTestCase {
XCTAssertTrue(testSubject.args.contains("--provider-version\nv1.2.3"))
}

func testArgsWithValidCustomHeader() {
let asciiCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !\"#$%&'()*+,-./:;<=>?@"
let testSubject = ProviderVerifier.Options(
provider: .init(port: 8080),
pactsSource: .directories(["/tmp/pacts"]),
customHeader: ["Foo": asciiCharacters, "Bar": "BAZ"]
)

XCTAssertTrue(["--header\nFoo=\(asciiCharacters)", "--header\nBar=BAZ"].allSatisfy { testSubject.args.contains($0) })
}

func testArgsWithInvalidCustomHeader() {
let invalidCharacters = "çüé abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !\"#$%&'()*+,-./:;<=>?@"
let testSubject = ProviderVerifier.Options(
provider: .init(port: 8080),
pactsSource: .directories(["/tmp/pacts"]),
customHeader: ["Foo": invalidCharacters, "Bar": "BAZ"]
)

XCTAssertTrue(testSubject.args.contains("--header\nBar=BAZ"))
XCTAssertFalse(testSubject.args.contains("--header\nFoo=\(invalidCharacters)"))
}

}

private extension ProviderVerifierOptionsTests {
Expand Down