Skip to content

Commit

Permalink
Add a query parameters test (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushkar N Kulkarni authored and djones6 committed Mar 13, 2019
1 parent d837d45 commit 8c0c632
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Tests/KituraNetTests/ClientE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class ClientE2ETests: KituraNetTest {
("testPipeliningSpanningPackets", testPipeliningSpanningPackets),
("testSimpleHTTPClient", testSimpleHTTPClient),
("testUrlURL", testUrlURL),
("testRedirection", testRedirection)
("testRedirection", testRedirection),
("testQueryParameters", testQueryParameters)
]
}

Expand Down Expand Up @@ -424,6 +425,41 @@ class ClientE2ETests: KituraNetTest {
}
}

func testQueryParameters() {
class TestDelegate : ServerDelegate {
func toDictionary(_ queryItems: [URLQueryItem]?) -> [String : String] {
guard let queryItems = queryItems else { return [:] }
var queryParameters: [String : String] = [:]
for queryItem in queryItems {
queryParameters[queryItem.name] = queryItem.value ?? ""
}
return queryParameters
}

func handle(request: ServerRequest, response: ServerResponse) {
do {
let urlComponents = URLComponents(url: request.urlURL, resolvingAgainstBaseURL: false) ?? URLComponents()
let queryParameters = toDictionary(urlComponents.queryItems)
XCTAssertEqual(queryParameters.count, 3, "Expected 3 query parameters, received \(queryParameters.count)")
XCTAssertEqual(queryParameters["key1"], "value1", "Value of key1 should have been value1, received \(queryParameters["key1"] ?? "nil")")
XCTAssertEqual(queryParameters["key2"], "value2", "Value of key2 should have been value2, received \(queryParameters["key2"] ?? "nil")")
XCTAssertEqual(queryParameters["key3"], "value3 value4", "Value of key3 should have been \"value3 value4\", received \(queryParameters["key3"] ?? "nil")")
response.statusCode = .OK
try response.end()
} catch {
XCTFail("Error while writing a response")
}
}
}
let testDelegate = TestDelegate()
performServerTest(testDelegate) { expectation in
self.performRequest("get", path: "/zxcv/p?key1=value1&key2=value2&key3=value3%20value4", callback: { response in
XCTAssertEqual(response?.statusCode, .OK)
expectation.fulfill()
})
}
}

class TestServerDelegate: ServerDelegate {

func handle(request: ServerRequest, response: ServerResponse) {
Expand Down

0 comments on commit 8c0c632

Please sign in to comment.