Skip to content

Commit

Permalink
Fixed the public scope of the buildEither(component:) components of…
Browse files Browse the repository at this point in the history
… `URLBuilder` (#13)

* Fixed #11

* Added BodyBuilderTests.swift for increase test coverage

* wip

* Added HeaderBuilderTest.swift for increase test coverage
  • Loading branch information
devyhan authored Dec 15, 2022
1 parent 3645145 commit 93fd522
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 3 deletions.
Binary file not shown.
3 changes: 2 additions & 1 deletion .swiftpm/xcode/xcshareddata/xcschemes/APIRouter.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
4 changes: 2 additions & 2 deletions Sources/APIRouter/URLBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public struct URLBuilder {
CombinedURL(components)
}

static func buildEither(first component: HttpUrlProtocol) -> HttpUrlProtocol {
public static func buildEither(first component: HttpUrlProtocol) -> HttpUrlProtocol {
CombinedURL([component])
}

static func buildEither(second component: HttpUrlProtocol) -> HttpUrlProtocol {
public static func buildEither(second component: HttpUrlProtocol) -> HttpUrlProtocol {
CombinedURL([component])
}
}
Expand Down
82 changes: 82 additions & 0 deletions Tests/APIRouterTests/BodyBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Foundation
import XCTest
@testable import APIRouter

final class BodyBuilderTests: XCTestCase {
func testSwitchConditionalStatementWorkingForBuildEitherInBodyBuilder() {
enum BodyOptions {
case one
case two

var request: Request {
Request {
Body {
switch self {
case .one:
Param("VALUE", forKey: "OPTIONONE")
case .two:
Param("VALUE", forKey: "OPTIONTWO")
}
}
}
}
}

if let optionOneBody = BodyOptions.one.request.urlRequest?.httpBody,
let optionOneBodyString = String(data: optionOneBody, encoding: .utf8),
let optionTwoBody = BodyOptions.two.request.urlRequest?.httpBody,
let optionTwoBodyString = String(data: optionTwoBody, encoding: .utf8) {
XCTAssertEqual(optionOneBodyString, "{\"OPTIONONE\":\"VALUE\"}")
XCTAssertEqual(optionTwoBodyString, "{\"OPTIONTWO\":\"VALUE\"}")
}
}

func testIfConditionalStatementWorkingForBuildEitherInUrlBuilder() {
enum BodyOptions {
case one
case two

var request: Request {
Request {
Body {
if self == .one {
Param("VALUE", forKey: "OPTIONONE")
} else {
Param("VALUE", forKey: "OPTIONTWO")
}
}
}
}
}

if let optionOneBody = BodyOptions.one.request.urlRequest?.httpBody,
let optionOneBodyString = String(data: optionOneBody, encoding: .utf8),
let optionTwoBody = BodyOptions.two.request.urlRequest?.httpBody,
let optionTwoBodyString = String(data: optionTwoBody, encoding: .utf8) {
XCTAssertEqual(optionOneBodyString, "{\"OPTIONONE\":\"VALUE\"}")
XCTAssertEqual(optionTwoBodyString, "{\"OPTIONTWO\":\"VALUE\"}")
}
}

func testForLoopStatementWorkingForBuildEitherInBodyBuilder() {
let params = [
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
]

let request = Request {
Body {
for param in params {
Param(param.value, forKey: param.key)
}
}
}

if let httpBody = request.urlRequest?.httpBody,
let bodyString = String(data: httpBody, encoding: .utf8) {
XCTAssertEqual(bodyString.sorted(), ["\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", "\"", ",", ",", ",", "1", "1", "2", "2", "3", "3", "4", "4", ":", ":", ":", ":", "a", "a", "a", "a", "e", "e", "e", "e", "e", "e", "e", "e", "k", "k", "k", "k", "l", "l", "l", "l", "u", "u", "u", "u", "v", "v", "v", "v", "y", "y", "y", "y", "{", "}"])
}
}
}
77 changes: 77 additions & 0 deletions Tests/APIRouterTests/HeaderBuilder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import Foundation
import XCTest
@testable import APIRouter

final class HeaderBuilderTests: XCTestCase {
func testSwitchConditionStatemntWorkingForBuildEitherInHeaderBuilder() {
enum HeaderOptions {
case one
case two

var request: Request {
Request {
Header {
switch self {
case .one:
Field("VALUE", forKey: "OPTIONONE")
case .two:
Field("VALUE", forKey: "OPTIONTWO")
}
}
}
}
}

if let optionOneHeaderFields = HeaderOptions.one.request.urlRequest?.allHTTPHeaderFields,
let optionTwoHeaderFields = HeaderOptions.two.request.urlRequest?.allHTTPHeaderFields {
XCTAssertEqual(optionOneHeaderFields, ["OPTIONONE":"VALUE"])
XCTAssertEqual(optionTwoHeaderFields, ["OPTIONTWO":"VALUE"])
}
}

func testIfConditionalStatementWorkingForBuildEitherInUrlBuilder() {
enum HeaderOptions {
case one
case two

var request: Request {
Request {
Header {
if self == .one {
Field("VALUE", forKey: "OPTIONONE")
} else {
Field("VALUE", forKey: "OPTIONTWO")
}
}
}
}
}

if let optionOneHeaderFields = HeaderOptions.one.request.urlRequest?.allHTTPHeaderFields,
let optionTwoHeaderFields = HeaderOptions.two.request.urlRequest?.allHTTPHeaderFields {
XCTAssertEqual(optionOneHeaderFields, ["OPTIONONE":"VALUE"])
XCTAssertEqual(optionTwoHeaderFields, ["OPTIONTWO":"VALUE"])
}
}

func testForLoopStatementWorkingForBuildEitherInHeaderBuilder() {
let fields = [
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
]

let request = Request {
Header {
for field in fields {
Field(field.value, forKey: field.key)
}
}
}

if let sortedAllHTTPHeaderFields = request.urlRequest?.allHTTPHeaderFields {
XCTAssertEqual(sortedAllHTTPHeaderFields, ["key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4"])
}
}
}
70 changes: 70 additions & 0 deletions Tests/APIRouterTests/URLBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,74 @@ final class URLBuilderTests: XCTestCase {

XCTAssertEqual(url, "https://www.urltest.com/test/path?test=query")
}

func testSwitchConditionalStatementWorkingForBuildEitherInUrlBuilder() {
enum SchemeOptions {
case https
case http
}

let options: SchemeOptions = .http

let request = Request {
URL {
switch options {
case .https:
Scheme(.https)
case .http:
Scheme(.http)
}
Host("www.urltest.com")
}
}

guard let url = request.urlRequest?.url?.absoluteString else { return }

XCTAssertEqual(url, "http://www.urltest.com")
}

func testIfConditionalStatementWorkingForBuildEitherInUrlBuilder() {
let conditional = true

let request = Request {
URL {
if conditional == true {
Scheme(.http)
} else {
Scheme(.https)
}
Host("www.urltest.com")
}
}

guard let url = request.urlRequest?.url?.absoluteString else { return }

XCTAssertEqual(url, "http://www.urltest.com")
}

/// #1: https://github.com/devyhan/APIRouter/issues/1
#warning("#1 change after issue resolution.")
func testForLoopStatementWorkingForBuildEitherInUrlBuilder() {
let queries = [
"query1": "value1"//,
// "query2": "value2",
// "query3": "value3",
// "query4": "value4",
]

let request = Request {
URL {
Scheme(.https)
Host("www.urltest.com")

for query in queries {
Query(query.key, value: query.value)
}
}
}

guard let url = request.urlRequest?.url?.absoluteString else { return }

XCTAssertEqual(url, "https://www.urltest.com?query1=value1")
}
}

0 comments on commit 93fd522

Please sign in to comment.