Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from pedrocid/master
Browse files Browse the repository at this point in the history
Dynamic query parameter value
  • Loading branch information
albertodebortoli authored Aug 12, 2022
2 parents 10eec07 + 6a4b1e3 commit 6986eee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Framework/Sources/MockHTTPRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ extension MockHTTPRoute: Equatable {
if case MockHTTPRoute.custom(let lhsMethod, let lhsUrlPath, let lhsQuery, let lhsRequestHeaders, _, _, _) = lhs,
case MockHTTPRoute.custom(let rhsMethod, let rhsUrlPath, let rhsQuery, let rhsRequestHeaders, _, _, _) = rhs {
return lhsMethod == rhsMethod && lhsUrlPath.pathMatches(rhsUrlPath)
&& lhsQuery == rhsQuery && headers(lhsRequestHeaders, contains: rhsRequestHeaders)
&& queryParamsMatch(lhs: lhsQuery, rhs: rhsQuery) && headers(lhsRequestHeaders, contains: rhsRequestHeaders)
}
if case MockHTTPRoute.template(let lhsMethod, let lhsUrlPath, _, _, _) = lhs,
case MockHTTPRoute.template(let rhsMethod, let rhsUrlPath, _, _, _) = rhs {
Expand Down Expand Up @@ -198,6 +198,19 @@ extension MockHTTPRoute: Equatable {
return false
}

private static func queryParamsMatch(lhs: [String:String], rhs: [String:String]) -> Bool {

if lhs.count != rhs.count { return false }

for element in lhs {
let matches = rhs[element.key]?.pathMatches(element.value) ?? false
if !matches {
return false
}
}
return true
}

public func matches(method: MockHTTPMethod, path: String, params: [String:String], headers: [String:String]) -> Bool {
guard !method.rawValue.isEmpty else { return false }
guard !path.isEmpty else { return false }
Expand Down
39 changes: 39 additions & 0 deletions Tests/Sources/CustomRouteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,45 @@ class CustomRouteTests: ShockTestCase {
self.waitForExpectations(timeout: timeout, handler: nil)
}

func testCustomRouteWithDynamicQueryParameters() {
let query = "item1=value1&item2=value2"
let route: MockHTTPRoute = .custom(
method: .get,
urlPath: "/custom-with-query",
query: ["item1": ":placeHolder1", "item2": ":placeHolder2"],
requestHeaders: [:],
responseHeaders: [:],
code: 200,
filename: "testCustomRoute.txt"
)
let query2 = "item3=value3&item4=value4"
let route2: MockHTTPRoute = .custom(
method: .get,
urlPath: "/custom-with-query",
query: ["item3": ":placeHolder3", "item4": ":placeHolder4"],
requestHeaders: [:],
responseHeaders: [:],
code: 200,
filename: "testCustomRoute2.txt"
)
let routes: MockHTTPRoute = .collection(routes: [route, route2])
server.setup(route: routes)

let expectation = self.expectation(description: "Expect 200 response with response body")
HTTPClient.get(url: "\(server.hostURL)/custom-with-query?\(query)") { code, body, headers, error in
expectation.fulfill()
XCTAssertEqual(code, 200)
XCTAssertEqual(body, "testCustomRoute test fixture\n")
}
let expectation2 = self.expectation(description: "Expect 200 response with response body")
HTTPClient.get(url: "\(server.hostURL)/custom-with-query?\(query2)") { code, body, headers, error in
expectation2.fulfill()
XCTAssertEqual(code, 200)
XCTAssertEqual(body, "testCustomRoute2 test fixture\n")
}
self.waitForExpectations(timeout: timeout, handler: nil)
}

func testCustomRouteWithoutQueryParameters() {
let route: MockHTTPRoute = .custom(
method: .get,
Expand Down

0 comments on commit 6986eee

Please sign in to comment.