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

Added matcher for host #90

Merged
merged 1 commit into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions Vinyl/RequestMatcherRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Foundation
public enum RequestMatcherType {
case method
case url
case host
case path
case query
case headers
Expand Down Expand Up @@ -42,6 +43,8 @@ public struct RequestMatcherRegistry {
return MethodRequestMatcher()
case .url:
return URLRequestMatcher()
case .host:
return HostRequestMatcher()
case .path:
return PathRequestMatcher()
case .query:
Expand All @@ -65,13 +68,19 @@ private struct MethodRequestMatcher: RequestMatcher {
}
}

private struct URLRequestMatcher: RequestMatcher {
private struct URLRequestMatcher: RequestMatcher {
func match(lhs: Request, rhs: Request) -> Bool {
return lhs.url == rhs.url
}
}

private struct PathRequestMatcher: RequestMatcher {
private struct HostRequestMatcher: RequestMatcher {
func match(lhs: Request, rhs: Request) -> Bool {
return lhs.url?.host == rhs.url?.host
}
}

private struct PathRequestMatcher: RequestMatcher {
func match(lhs: Request, rhs: Request) -> Bool {
return lhs.url?.path == rhs.url?.path
}
Expand Down
20 changes: 20 additions & 0 deletions VinylTests/Arbitrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ let pathParameterGen: Gen<String> = pathParameterGenArray.map { xs in
}
}

func pathParameterGenArrayMinimumSize(_ minimumSize: Int) -> Gen<[String]> {
return Gen.sized { sz in
return parameterGen.proliferate(withSize: max(sz + 1, minimumSize))
}
}

func pathParameterGenMinimumSize(_ minimumSize: Int) -> Gen<String> {
return pathParameterGenArrayMinimumSize(minimumSize).map { xs in
return xs.reduce("?") {
switch $0 {
case "?":
return $0 + $1
default:
return $0 + "&" + $1
}
}
}
}


private func curry<A, B, C>(_ f : @escaping (A, B) -> C) -> (A) -> (B) -> C {
return { a in { b in f(a, b) } }
}
44 changes: 43 additions & 1 deletion VinylTests/RequestMatcherRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension Dictionary {
class RequestMatcherRegistryTests: XCTestCase {
func testProperties() {
property("Requests with identical paths and query parameters should match") <- forAllNoShrink(
Gen<RequestMatcherType>.fromElements(of: [RequestMatcherType.path, .query, .body])
Gen<RequestMatcherType>.fromElements(of: [.path, .query, .body])
, urlStringGen
, urlPathGen
, pathParameterGen
Expand All @@ -41,6 +41,48 @@ class RequestMatcherRegistryTests: XCTestCase {

return registry.matchableRequests(request: aRequest, with: anotherRequest)
}

property("Requests with different order of query parameters should not match by URL") <- forAllNoShrink(
Gen<RequestMatcherType>.fromElements(of: [.url, .path, .query, .body])
, urlStringGen
, urlPathGen
, pathParameterGenMinimumSize(2)
, Optional<String>.arbitrary
) { (type, url, path, params, body) in
let registry = RequestMatcherRegistry(types: [type])
let commonData = body?.data(using: .utf8)

var aRequest = URLRequest(url: URL(string: url + path + params )!)
aRequest.httpBody = commonData

let shuffledParams = "?" + params.dropFirst().components(separatedBy: "&").reversed().joined(separator: "&")

var anotherRequest = URLRequest(url: URL(string: url + path + shuffledParams)!)
anotherRequest.httpBody = commonData

return registry.matchableRequests(request: aRequest, with: anotherRequest)
}.expectFailure

property("Requests with different order of query parameters should match by Host") <- forAllNoShrink(
Gen<RequestMatcherType>.fromElements(of: [.host, .path, .query, .body])
, urlStringGen
, urlPathGen
, pathParameterGenMinimumSize(2)
, Optional<String>.arbitrary
) { (type, url, path, params, body) in
let registry = RequestMatcherRegistry(types: [type])
let commonData = body?.data(using: .utf8)

var aRequest = URLRequest(url: URL(string: url + path + params )!)
aRequest.httpBody = commonData

let shuffledParams = "?" + params.dropFirst().components(separatedBy: "&").reversed().joined(separator: "&")

var anotherRequest = URLRequest(url: URL(string: url + path + shuffledParams)!)
anotherRequest.httpBody = commonData

return registry.matchableRequests(request: aRequest, with: anotherRequest)
}

property("Requests with identical headers should match") <- forAllNoShrink(
urlStringGen
Expand Down