From 4b12bfb5893acd03e2ef3fdc805eb1ca4aa250ce Mon Sep 17 00:00:00 2001 From: Pushkar N Kulkarni Date: Wed, 20 Feb 2019 09:44:33 +0000 Subject: [PATCH] Add % encoding in ClientRequest.init, remove % encoding in HTTPServerRequest.init --- Sources/KituraNet/ClientRequest.swift | 2 +- Sources/KituraNet/HTTP/HTTPServerRequest.swift | 2 +- Tests/KituraNetTests/ClientRequestTests.swift | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Sources/KituraNet/ClientRequest.swift b/Sources/KituraNet/ClientRequest.swift index 2afae2d2..2de209b1 100644 --- a/Sources/KituraNet/ClientRequest.swift +++ b/Sources/KituraNet/ClientRequest.swift @@ -238,7 +238,7 @@ public class ClientRequest { if thePath.first != "/" { thePath = "/" + thePath } - path = thePath + path = thePath.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) ?? thePath self.path = path case .username(let userName): self.userName = userName diff --git a/Sources/KituraNet/HTTP/HTTPServerRequest.swift b/Sources/KituraNet/HTTP/HTTPServerRequest.swift index c396d69f..9b971de9 100644 --- a/Sources/KituraNet/HTTP/HTTPServerRequest.swift +++ b/Sources/KituraNet/HTTP/HTTPServerRequest.swift @@ -148,7 +148,7 @@ public class HTTPServerRequest: ServerRequest { self.method = requestHead.method.string() self.httpVersionMajor = requestHead.version.major self.httpVersionMinor = requestHead.version.minor - self._urlString = requestHead.uri + self._urlString = requestHead.uri.removingPercentEncoding ?? requestHead.uri self.enableSSL = enableSSL } diff --git a/Tests/KituraNetTests/ClientRequestTests.swift b/Tests/KituraNetTests/ClientRequestTests.swift index 72ad66b0..348bca3d 100644 --- a/Tests/KituraNetTests/ClientRequestTests.swift +++ b/Tests/KituraNetTests/ClientRequestTests.swift @@ -98,6 +98,20 @@ class ClientRequestTests: KituraNetTest { let options = ClientRequest.parse("https://username:password@66o.tech:8080/path?key=value") let testRequest = ClientRequest(options: options, callback: testCallback) XCTAssertEqual(testRequest.url, "https://username:password@66o.tech:8080/path?key=value") + + let options1: [ClientRequest.Options] = [ .schema("https"), + .hostname("66o.tech"), + .path("/view/matching?key=\"viewTest\"") + ] + let testRequest1 = ClientRequest(options: options1, callback: testCallback) + XCTAssertEqual(testRequest1.url, "https://66o.tech/view/matching?key=%22viewTest%22") + + let options2: [ClientRequest.Options] = [ .schema("https"), + .hostname("66o.tech"), + .path("/view/match?key?=value?") + ] + let testRequest2 = ClientRequest(options: options2, callback: testCallback) + XCTAssertEqual(testRequest2.url, "https://66o.tech/view/match?key?=value?") } func testClientRequestBasicAuthentcation() {