Skip to content

Commit

Permalink
Supported to the URL Scheme as static parameter and function (#18)
Browse files Browse the repository at this point in the history
* Supported to the `HttpRequest` method as static parameter  (#16)

* Wip

* Update README.md

* Increased test coverage
  • Loading branch information
devyhan authored Dec 19, 2022
1 parent f195f10 commit 2f5434d
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
*.xcuserstate
7 changes: 0 additions & 7 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

This file was deleted.

Binary file not shown.

This file was deleted.

25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,31 @@ Router {
}
// https://www.overrideurl.com/comments?postId=1
```

#### URL Scheme declaration
- Using `Scheme(_ scheme:)` to `Scheme` declaration.
```swift
Request {
...
URL {
Scheme(.https)
...
}
...
}
```
- Using `static let scheme:` to `Scheme` declaration.
```swift
Request {
...
URL {
Scheme.https
...
}
...
}
```

---
### How to configure and use ***APIRouter*** in a real project?
- Just create APIRouter.swift in your project! Happy hacking! 😁
Expand Down
12 changes: 12 additions & 0 deletions Sources/APIRouter/URLBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ public extension URL {

public struct Scheme: HttpUrlProtocol {
private let scheme: URLScheme

static let http = Self(.http)

static let https = Self(.https)

static let mqtt = Self(.mqtt)

static let mqtts = Self(.mqtts)

static func custom(_ value: String) -> Self {
Self(.custom(value))
}

public init(_ scheme: URLScheme) {
self.scheme = scheme
Expand Down
189 changes: 120 additions & 69 deletions Tests/APIRouterTests/URLBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,58 @@ final class URLBuilderTests: XCTestCase {
Scheme(.custom("custom"))
}
}

guard let httpUrlString = httpUrlRequest.urlRequest?.url?.absoluteString else { return }
guard let httpsUrlString = httpsUrlRequest.urlRequest?.url?.absoluteString else { return }
guard let mqttUrlString = mqttUrlRequest.urlRequest?.url?.absoluteString else { return }
guard let mqttsUrlString = mqttsUrlRequest.urlRequest?.url?.absoluteString else { return }
guard let customUrlString = customUrlRequest.urlRequest?.url?.absoluteString else { return }

XCTAssertEqual(httpUrlString, "http:")
XCTAssertEqual(httpsUrlString, "https:")
XCTAssertEqual(mqttUrlString, "mqtt:")
XCTAssertEqual(mqttsUrlString, "mqtts:")
XCTAssertEqual(customUrlString, "custom:")

if let httpUrlString = httpUrlRequest.urlRequest?.url?.absoluteString,
let httpsUrlString = httpsUrlRequest.urlRequest?.url?.absoluteString,
let mqttUrlString = mqttUrlRequest.urlRequest?.url?.absoluteString,
let mqttsUrlString = mqttsUrlRequest.urlRequest?.url?.absoluteString,
let customUrlString = customUrlRequest.urlRequest?.url?.absoluteString {
XCTAssertEqual(httpUrlString, "http:")
XCTAssertEqual(httpsUrlString, "https:")
XCTAssertEqual(mqttUrlString, "mqtt:")
XCTAssertEqual(mqttsUrlString, "mqtts:")
XCTAssertEqual(customUrlString, "custom:")
}
}

func testGeneratedUrlSchemeWithStaticParameterAndFunctionOfUrlBuilder() {
let httpUrlRequest = Request {
URL {
Scheme.http
}
}
let httpsUrlRequest = Request {
URL {
Scheme.https
}
}
let mqttUrlRequest = Request {
URL {
Scheme.mqtt
}
}
let mqttsUrlRequest = Request {
URL {
Scheme.mqtts
}
}
let customUrlRequest = Request {
URL {
Scheme.custom("custom")
}
}

if let httpUrlString = httpUrlRequest.urlRequest?.url?.absoluteString,
let httpsUrlString = httpsUrlRequest.urlRequest?.url?.absoluteString,
let mqttUrlString = mqttUrlRequest.urlRequest?.url?.absoluteString,
let mqttsUrlString = mqttsUrlRequest.urlRequest?.url?.absoluteString,
let customUrlString = customUrlRequest.urlRequest?.url?.absoluteString {
XCTAssertEqual(httpUrlString, "http:")
XCTAssertEqual(httpsUrlString, "https:")
XCTAssertEqual(mqttUrlString, "mqtt:")
XCTAssertEqual(mqttsUrlString, "mqtts:")
XCTAssertEqual(customUrlString, "custom:")
}
}

func testGeneratedUrlHostWithURLBuilder() {
Expand All @@ -49,10 +89,10 @@ final class URLBuilderTests: XCTestCase {
Host("host.com")
}
}

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

XCTAssertEqual(urlString, "//host.com")
if let urlString = request.urlRequest?.url?.absoluteString {
XCTAssertEqual(urlString, "//host.com")
}
}

func testGeneratedUrlPathWithURLBuilder() {
Expand All @@ -61,10 +101,10 @@ final class URLBuilderTests: XCTestCase {
Path("test/path")
}
}

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

XCTAssertEqual(pathString, "/test/path")
if let pathString = request.urlRequest?.url?.absoluteString {
XCTAssertEqual(pathString, "/test/path")
}
}

func testGeneratedUrlQueryWithURLBuilder() {
Expand All @@ -73,10 +113,10 @@ final class URLBuilderTests: XCTestCase {
Query("test", value: "query")
}
}

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

XCTAssertEqual(queryString, "?test=query")
if let queryString = request.urlRequest?.url?.absoluteString {
XCTAssertEqual(queryString, "?test=query")
}
}

func testGeneratedMultyUrlQueryWithURLBuilder() {
Expand All @@ -90,13 +130,13 @@ final class URLBuilderTests: XCTestCase {
)
}
}

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

XCTAssertEqual(queryString.first, "?")
XCTAssertEqual(queryString.contains("first=firstQuery"), true)
XCTAssertEqual(queryString.contains("second=secondQuery"), true)
XCTAssertEqual(queryString.split(separator: "&").count, 2)
if let queryString = request.urlRequest?.url?.absoluteString {
XCTAssertEqual(queryString.first, "?")
XCTAssertEqual(queryString.contains("first=firstQuery"), true)
XCTAssertEqual(queryString.contains("second=secondQuery"), true)
XCTAssertEqual(queryString.split(separator: "&").count, 2)
}
}

func testGeneratedURLWithURLBuilder() {
Expand All @@ -108,64 +148,75 @@ final class URLBuilderTests: XCTestCase {
Query("test", value: "query")
}
}

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

XCTAssertEqual(url, "https://www.urltest.com/test/path?test=query")
if let url = request.urlRequest?.url?.absoluteString {
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")
var request: Request {
Request {
URL {
switch self {
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")
if let httpUrl = SchemeOptions.http.request.urlRequest?.url?.absoluteString {
XCTAssertEqual(httpUrl, "http://www.urltest.com")
}
if let httpsUrl = SchemeOptions.https.request.urlRequest?.url?.absoluteString {
XCTAssertEqual(httpsUrl, "https://www.urltest.com")
}
}

func testIfConditionalStatementWorkingForBuildEitherInUrlBuilder() {
let conditional = true

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

var request: Request {
Request {
URL {
if self == .http {
Scheme(.http)
} else {
Scheme(.https)
}
Host("www.urltest.com")
}
}
}
}

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

XCTAssertEqual(url, "http://www.urltest.com")
if let httpUrl = SchemeOptions.http.request.urlRequest?.url?.absoluteString {
XCTAssertEqual(httpUrl, "http://www.urltest.com")
}
if let httpsUrl = SchemeOptions.https.request.urlRequest?.url?.absoluteString {
XCTAssertEqual(httpsUrl, "https://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",
// "query2": "value2",
// "query3": "value3",
// "query4": "value4",
]

let request = Request {
Expand All @@ -179,8 +230,8 @@ final class URLBuilderTests: XCTestCase {
}
}

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

XCTAssertEqual(url, "https://www.urltest.com?query1=value1")
if let url = request.urlRequest?.url?.absoluteString {
XCTAssertEqual(url, "https://www.urltest.com?query1=value1")
}
}
}

0 comments on commit 2f5434d

Please sign in to comment.