-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QueryParameters protocol to provide interface for URL query.
- Loading branch information
Showing
5 changed files
with
81 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
|
||
/// `QueryParameters` provides interface to generate HTTP URL query strings. | ||
public protocol QueryParameters { | ||
/// Generate URL query strings. | ||
func encode() -> String? | ||
} |
20 changes: 20 additions & 0 deletions
20
Sources/APIKit/QueryParameters/URLEncodedQueryParameters.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Foundation | ||
|
||
/// `URLEncodedQueryParameters` serializes form object for HTTP URL query. | ||
public struct URLEncodedQueryParameters: QueryParameters { | ||
/// The parameters to be url encoded. | ||
public let parameters: Any | ||
|
||
/// Returns `URLEncodedQueryParameters` that is initialized with parameters. | ||
public init(parameters: Any) { | ||
self.parameters = parameters | ||
} | ||
|
||
/// Generate url encoded `String`. | ||
public func encode() -> String? { | ||
guard let parameters = parameters as? [String: Any], !parameters.isEmpty else { | ||
return nil | ||
} | ||
return URLEncodedSerialization.string(from: parameters) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Tests/APIKitTests/QueryParameters/URLEncodedQueryParametersTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import XCTest | ||
import APIKit | ||
|
||
class URLEncodedQueryParametersTests: XCTestCase { | ||
func testURLEncodedSuccess() { | ||
let object: [String: Any] = ["foo": "string", "bar": 1, "q": "こんにちは"] | ||
let parameters = URLEncodedQueryParameters(parameters: object) | ||
guard let query = parameters.encode() else { | ||
XCTFail() | ||
return | ||
} | ||
|
||
let items = query.components(separatedBy: "&") | ||
XCTAssertEqual(items.count, 3) | ||
XCTAssertTrue(items.contains("foo=string")) | ||
XCTAssertTrue(items.contains("bar=1")) | ||
XCTAssertTrue(items.contains("q=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF")) | ||
} | ||
} |