From 1fca1cb14867cc691ca88bb9f3fd4c5924625ac0 Mon Sep 17 00:00:00 2001 From: Dimitar Stefanovski Date: Fri, 5 May 2023 08:40:51 +0200 Subject: [PATCH 1/5] ADD Support for URL query parameters with same key name --- Sources/GirdersSwift/http/Request.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/GirdersSwift/http/Request.swift b/Sources/GirdersSwift/http/Request.swift index e923a18..7acc4e3 100644 --- a/Sources/GirdersSwift/http/Request.swift +++ b/Sources/GirdersSwift/http/Request.swift @@ -154,6 +154,15 @@ public struct MutableRequest : RequestGenerator { self.queryString = params.urlEncodedQueryStringWithEncoding(encoding: String.Encoding.utf8) } + if let params = parameters as? [URLQueryItem] { + var urlComponents = URLComponents() + let encodedQueryParams = params.map { + URLQueryItem(name: $0.name.urlEncodedStringWithEncoding(), + value: $0.value?.urlEncodedStringWithEncoding()) + } + urlComponents.queryItems = encodedQueryParams + self.queryString = urlComponents.query! + } } public mutating func updateSSLCredentials(sslCredentials: SSLCredentials) { From 73bb98d0e6462f90a16233bcc8a80b3f1e97ec7d Mon Sep 17 00:00:00 2001 From: Dimitar Stefanovski Date: Fri, 5 May 2023 08:41:15 +0200 Subject: [PATCH 2/5] MOD Bump podspec to 0.7.1 --- GirdersSwift.podspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GirdersSwift.podspec b/GirdersSwift.podspec index 437e029..9d3cfb2 100644 --- a/GirdersSwift.podspec +++ b/GirdersSwift.podspec @@ -1,12 +1,12 @@ Pod::Spec.new do |s| s.name = 'GirdersSwift' - s.version = '0.7.0' + s.version = '0.7.1' s.summary = 'Girders for iOS, written in Swift.' s.homepage = 'https://www.netcetera.com' s.author = 'Netcetera' s.description = 'A library that reduces development time for iOS Swift applications.' s.platform = :ios, '11.0' - s.source = { :git => 'https://github.com/netceteragroup/GirdersSwift.git', :tag => '0.7.0' } + s.source = { :git => 'https://github.com/netceteragroup/GirdersSwift.git', :tag => '0.7.1' } s.requires_arc = true s.swift_version = "5.0" s.module_name = 'GirdersSwift' From b7835e81317e8e366461b57ac651193904cbe1f7 Mon Sep 17 00:00:00 2001 From: Dimitar Stefanovski Date: Sun, 7 May 2023 21:47:39 +0200 Subject: [PATCH 3/5] ADD Mutable request update query parameters unit tests --- Sources/GirdersSwift/http/Request.swift | 11 ++++++----- .../swift/http/TestRequestGenerator.swift | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Sources/GirdersSwift/http/Request.swift b/Sources/GirdersSwift/http/Request.swift index 7acc4e3..ffee930 100644 --- a/Sources/GirdersSwift/http/Request.swift +++ b/Sources/GirdersSwift/http/Request.swift @@ -150,11 +150,10 @@ public struct MutableRequest : RequestGenerator { } public mutating func updateQueryParameters(parameters: Any?) { - if let params = parameters as? [String: AnyObject] { - self.queryString = - params.urlEncodedQueryStringWithEncoding(encoding: String.Encoding.utf8) - } - if let params = parameters as? [URLQueryItem] { + switch parameters { + case let params as [String: AnyObject]: + self.queryString = params.urlEncodedQueryStringWithEncoding(encoding: String.Encoding.utf8) + case let params as [URLQueryItem]: var urlComponents = URLComponents() let encodedQueryParams = params.map { URLQueryItem(name: $0.name.urlEncodedStringWithEncoding(), @@ -162,6 +161,8 @@ public struct MutableRequest : RequestGenerator { } urlComponents.queryItems = encodedQueryParams self.queryString = urlComponents.query! + default: + self.queryString = nil } } diff --git a/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift b/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift index 738c342..42d0d42 100644 --- a/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift +++ b/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift @@ -107,6 +107,24 @@ class TestMutableRequest: XCTestCase { XCTAssertNotNil(request.parameters) } + + func testUpdateQueryParameters() { + var request = mockGenerator.generateRequest(withMethod: .GET) + let queryParameters = [URLQueryItem(name: "token", value: "token1")] + + request.updateQueryParameters(parameters: queryParameters) + + XCTAssertNotNil(request.queryString) + } + + func testUpdateQueryParametersFromUnsupportedType() { + var request = mockGenerator.generateRequest(withMethod: .GET) + let queryParameters = ["param1=value1", "param2=value2"] + + request.updateQueryParameters(parameters: queryParameters) + + XCTAssertNil(request.queryString) + } func testupdateHTTPHeaderFields() { var request = mockGenerator.request(withMethod: .POST) |> mockGenerator.withBasicAuth |> mockGenerator.withJsonSupport From adb3da83b976141854fd127c0f4b8e7498e49fa1 Mon Sep 17 00:00:00 2001 From: Orce Mihailov Date: Mon, 8 May 2023 09:08:50 +0200 Subject: [PATCH 4/5] Extending the UnitTests for the support of updating query parameters with URLQueryItems. --- Sources/GirdersSwift/http/Request.swift | 18 +++++++---- .../swift/http/TestRequestGenerator.swift | 32 ++++++++++++++++--- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/Sources/GirdersSwift/http/Request.swift b/Sources/GirdersSwift/http/Request.swift index ffee930..4c194d2 100644 --- a/Sources/GirdersSwift/http/Request.swift +++ b/Sources/GirdersSwift/http/Request.swift @@ -154,18 +154,22 @@ public struct MutableRequest : RequestGenerator { case let params as [String: AnyObject]: self.queryString = params.urlEncodedQueryStringWithEncoding(encoding: String.Encoding.utf8) case let params as [URLQueryItem]: - var urlComponents = URLComponents() - let encodedQueryParams = params.map { - URLQueryItem(name: $0.name.urlEncodedStringWithEncoding(), - value: $0.value?.urlEncodedStringWithEncoding()) - } - urlComponents.queryItems = encodedQueryParams - self.queryString = urlComponents.query! + updateQueryParameters(params: params) default: self.queryString = nil } } + private mutating func updateQueryParameters(params: [URLQueryItem]) { + var urlComponents = URLComponents() + let encodedQueryParams = params.map { + URLQueryItem(name: $0.name.urlEncodedStringWithEncoding(), + value: $0.value?.urlEncodedStringWithEncoding()) + } + urlComponents.queryItems = encodedQueryParams + self.queryString = urlComponents.query! + } + public mutating func updateSSLCredentials(sslCredentials: SSLCredentials) { self.sslCredentials = sslCredentials } diff --git a/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift b/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift index 42d0d42..4a32552 100644 --- a/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift +++ b/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift @@ -108,13 +108,37 @@ class TestMutableRequest: XCTestCase { XCTAssertNotNil(request.parameters) } - func testUpdateQueryParameters() { + func mutableRequestWithQuery(_ params: Any) -> MutableRequest { var request = mockGenerator.generateRequest(withMethod: .GET) - let queryParameters = [URLQueryItem(name: "token", value: "token1")] + request.updateQueryParameters(parameters: params) + return request + } + + func testQueryParametersAreProperlySetWhenURLQueryItemConfigurationIsUsed() { + let queryParameters = [URLQueryItem(name: "token1", value: "token1"), + URLQueryItem(name: "token2", value: "token2")] + let request = mutableRequestWithQuery(queryParameters) + XCTAssertTrue(request.queryString!.contains("token1=token1")) + XCTAssertTrue(request.queryString!.contains("token2=token2")) + } + + func testQueryParametersAreSetWhenEqualKeysAreBeingUsed() { + let queryParameters = [URLQueryItem(name: "token1", value: "token1"), + URLQueryItem(name: "token1", value: "token2")] - request.updateQueryParameters(parameters: queryParameters) + let request = mutableRequestWithQuery(queryParameters) + XCTAssertTrue(request.queryString!.contains("token1=token1")) + XCTAssertTrue(request.queryString!.contains("token1=token2")) + } + + func testURLEscapeEncodingIsAppliedInQueryString() { + let param1 = "New Parameter" + let value1 = "Hello, world!" - XCTAssertNotNil(request.queryString) + let queryParameters = [URLQueryItem(name: param1, value: value1)] + let request = mutableRequestWithQuery(queryParameters) + XCTAssertEqual(request.queryString!, + ("\(param1.urlEncodedStringWithEncoding())=\(value1.urlEncodedStringWithEncoding())")) } func testUpdateQueryParametersFromUnsupportedType() { From 18fab79860ef4bd6ef5e86e9d8585fee5b8fcdbc Mon Sep 17 00:00:00 2001 From: Dimitar Stefanovski Date: Mon, 8 May 2023 09:44:16 +0200 Subject: [PATCH 5/5] MOD Restructure TestMutableRequest --- .../swift/http/TestRequestGenerator.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift b/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift index 4a32552..17e6754 100644 --- a/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift +++ b/Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift @@ -107,12 +107,6 @@ class TestMutableRequest: XCTestCase { XCTAssertNotNil(request.parameters) } - - func mutableRequestWithQuery(_ params: Any) -> MutableRequest { - var request = mockGenerator.generateRequest(withMethod: .GET) - request.updateQueryParameters(parameters: params) - return request - } func testQueryParametersAreProperlySetWhenURLQueryItemConfigurationIsUsed() { let queryParameters = [URLQueryItem(name: "token1", value: "token1"), @@ -150,7 +144,7 @@ class TestMutableRequest: XCTestCase { XCTAssertNil(request.queryString) } - func testupdateHTTPHeaderFields() { + func testUpdateHTTPHeaderFields() { var request = mockGenerator.request(withMethod: .POST) |> mockGenerator.withBasicAuth |> mockGenerator.withJsonSupport XCTAssertEqual(request.headerFields["Accept"], "application/json") @@ -162,4 +156,13 @@ class TestMutableRequest: XCTestCase { XCTAssertNotNil(request.headerFields["Test1"]) } + + // MARK: - Private + + private func mutableRequestWithQuery(_ params: Any) -> MutableRequest { + var request = mockGenerator.generateRequest(withMethod: .GET) + request.updateQueryParameters(parameters: params) + return request + } + }