From 8c0c63217556eab5711796d717c3652b19adb797 Mon Sep 17 00:00:00 2001 From: Pushkar N Kulkarni Date: Wed, 13 Mar 2019 16:30:10 +0530 Subject: [PATCH] Add a query parameters test (#292) --- Tests/KituraNetTests/ClientE2ETests.swift | 38 ++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/Tests/KituraNetTests/ClientE2ETests.swift b/Tests/KituraNetTests/ClientE2ETests.swift index e6b9f15..2ea387c 100644 --- a/Tests/KituraNetTests/ClientE2ETests.swift +++ b/Tests/KituraNetTests/ClientE2ETests.swift @@ -38,7 +38,8 @@ class ClientE2ETests: KituraNetTest { ("testPipeliningSpanningPackets", testPipeliningSpanningPackets), ("testSimpleHTTPClient", testSimpleHTTPClient), ("testUrlURL", testUrlURL), - ("testRedirection", testRedirection) + ("testRedirection", testRedirection), + ("testQueryParameters", testQueryParameters) ] } @@ -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) {