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

Commit

Permalink
Return false if no query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-cid committed Jul 29, 2022
1 parent 2eaff02 commit 6a4b1e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Framework/Sources/MockHTTPRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ extension MockHTTPRoute: Equatable {
}

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 {
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 6a4b1e3

Please sign in to comment.