Skip to content

Commit

Permalink
Merge pull request #28 from dimebt/feature/add-support-for-url-query-…
Browse files Browse the repository at this point in the history
…parameters-with-same-name

ADD Support for url query parameters with same name
  • Loading branch information
Filip Sardzoski authored May 8, 2023
2 parents 34e4a17 + 18fab79 commit 1e05266
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
4 changes: 2 additions & 2 deletions GirdersSwift.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
20 changes: 17 additions & 3 deletions Sources/GirdersSwift/http/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,26 @@ 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)
switch parameters {
case let params as [String: AnyObject]:
self.queryString = params.urlEncodedQueryStringWithEncoding(encoding: String.Encoding.utf8)
case let params as [URLQueryItem]:
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
}
Expand Down
47 changes: 46 additions & 1 deletion Tests/GirdersSwiftTests/swift/http/TestRequestGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,43 @@ class TestMutableRequest: XCTestCase {
XCTAssertNotNil(request.parameters)
}

func testupdateHTTPHeaderFields() {
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")]

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!"

let queryParameters = [URLQueryItem(name: param1, value: value1)]
let request = mutableRequestWithQuery(queryParameters)
XCTAssertEqual(request.queryString!,
("\(param1.urlEncodedStringWithEncoding())=\(value1.urlEncodedStringWithEncoding())"))
}

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

XCTAssertEqual(request.headerFields["Accept"], "application/json")
Expand All @@ -120,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
}

}

0 comments on commit 1e05266

Please sign in to comment.