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

Commit 6986eee

Browse files
Merge pull request #55 from pedrocid/master
Dynamic query parameter value
2 parents 10eec07 + 6a4b1e3 commit 6986eee

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

Framework/Sources/MockHTTPRoute.swift

+14-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ extension MockHTTPRoute: Equatable {
157157
if case MockHTTPRoute.custom(let lhsMethod, let lhsUrlPath, let lhsQuery, let lhsRequestHeaders, _, _, _) = lhs,
158158
case MockHTTPRoute.custom(let rhsMethod, let rhsUrlPath, let rhsQuery, let rhsRequestHeaders, _, _, _) = rhs {
159159
return lhsMethod == rhsMethod && lhsUrlPath.pathMatches(rhsUrlPath)
160-
&& lhsQuery == rhsQuery && headers(lhsRequestHeaders, contains: rhsRequestHeaders)
160+
&& queryParamsMatch(lhs: lhsQuery, rhs: rhsQuery) && headers(lhsRequestHeaders, contains: rhsRequestHeaders)
161161
}
162162
if case MockHTTPRoute.template(let lhsMethod, let lhsUrlPath, _, _, _) = lhs,
163163
case MockHTTPRoute.template(let rhsMethod, let rhsUrlPath, _, _, _) = rhs {
@@ -198,6 +198,19 @@ extension MockHTTPRoute: Equatable {
198198
return false
199199
}
200200

201+
private static func queryParamsMatch(lhs: [String:String], rhs: [String:String]) -> Bool {
202+
203+
if lhs.count != rhs.count { return false }
204+
205+
for element in lhs {
206+
let matches = rhs[element.key]?.pathMatches(element.value) ?? false
207+
if !matches {
208+
return false
209+
}
210+
}
211+
return true
212+
}
213+
201214
public func matches(method: MockHTTPMethod, path: String, params: [String:String], headers: [String:String]) -> Bool {
202215
guard !method.rawValue.isEmpty else { return false }
203216
guard !path.isEmpty else { return false }

Tests/Sources/CustomRouteTests.swift

+39
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,45 @@ class CustomRouteTests: ShockTestCase {
136136
self.waitForExpectations(timeout: timeout, handler: nil)
137137
}
138138

139+
func testCustomRouteWithDynamicQueryParameters() {
140+
let query = "item1=value1&item2=value2"
141+
let route: MockHTTPRoute = .custom(
142+
method: .get,
143+
urlPath: "/custom-with-query",
144+
query: ["item1": ":placeHolder1", "item2": ":placeHolder2"],
145+
requestHeaders: [:],
146+
responseHeaders: [:],
147+
code: 200,
148+
filename: "testCustomRoute.txt"
149+
)
150+
let query2 = "item3=value3&item4=value4"
151+
let route2: MockHTTPRoute = .custom(
152+
method: .get,
153+
urlPath: "/custom-with-query",
154+
query: ["item3": ":placeHolder3", "item4": ":placeHolder4"],
155+
requestHeaders: [:],
156+
responseHeaders: [:],
157+
code: 200,
158+
filename: "testCustomRoute2.txt"
159+
)
160+
let routes: MockHTTPRoute = .collection(routes: [route, route2])
161+
server.setup(route: routes)
162+
163+
let expectation = self.expectation(description: "Expect 200 response with response body")
164+
HTTPClient.get(url: "\(server.hostURL)/custom-with-query?\(query)") { code, body, headers, error in
165+
expectation.fulfill()
166+
XCTAssertEqual(code, 200)
167+
XCTAssertEqual(body, "testCustomRoute test fixture\n")
168+
}
169+
let expectation2 = self.expectation(description: "Expect 200 response with response body")
170+
HTTPClient.get(url: "\(server.hostURL)/custom-with-query?\(query2)") { code, body, headers, error in
171+
expectation2.fulfill()
172+
XCTAssertEqual(code, 200)
173+
XCTAssertEqual(body, "testCustomRoute2 test fixture\n")
174+
}
175+
self.waitForExpectations(timeout: timeout, handler: nil)
176+
}
177+
139178
func testCustomRouteWithoutQueryParameters() {
140179
let route: MockHTTPRoute = .custom(
141180
method: .get,

0 commit comments

Comments
 (0)