Skip to content

Commit 60e5780

Browse files
committed
readme updates & tests
1 parent 7233ebc commit 60e5780

File tree

4 files changed

+193
-4
lines changed

4 files changed

+193
-4
lines changed

README.md

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,100 @@ The HttpClient provides the executors to perform data, download or upload tasks.
6363

6464
You can create decodable, encodable, codable or raw request when using a codable pipeline collection.
6565

66-
## Request pipelines
6766

67+
## Using raw requests & responses
6868

69-
## Custom decoder
69+
You can create raw HTTP requests using the HttpUrl and the HttpRawRequest type.
7070

71-
If you are using a request pipeline collection you can override the encoder and decoder objects.
71+
```swift
72+
let url = HttpUrl(scheme: "https",
73+
host: "jsonplaceholder.typicode.com",
74+
port: 80,
75+
path: ["todos"],
76+
resource: nil,
77+
query: [:],
78+
fragment: nil)
79+
80+
let req = HttpRawRequest(url: url, method: .get, headers: [:], body: nil)
81+
82+
/// execute the request using the client
83+
let client = UrlSessionHttpClient(session: .shared, log: true)
84+
let response = try await client.dataTask(req)
85+
86+
/// use the response data
87+
let todos = try JSONDecoder().decode([Todo].self, from: response.data)
88+
// response.statusCode == .ok
89+
// response.headers -> response headers as a dictionary
90+
```
91+
92+
You can use a specific HttpClient to execute a request using a data, download or upload task.
93+
94+
95+
## Constructing URLs
96+
97+
The HttpUrl type allows us to construct various URLs using a base URL.
7298

7399
```swift
74100

101+
let baseUrl = HttpUrl(host: "jsonplaceholder.typicode.com")
75102

76-
```
103+
// https://jsonplaceholder.typicode.com/todos/
104+
baseUrl.path("todos")
105+
106+
// https://jsonplaceholder.typicode.com/todos/1/
107+
baseUrl.path("todos", String(1))
108+
109+
// https://jsonplaceholder.typicode.com/todos/?foo=bar
110+
baseUrl.path("todos").query("foo", "bar")
111+
112+
// https://jsonplaceholder.typicode.com/todos/?foo=baz&bar=1
113+
baseUrl.path("todos").query([
114+
"foo": "baz",
115+
"bar": "1",
116+
])
117+
```
118+
119+
You can transform a HttpUrl object into a Foundation URL by using the `.url` property.
120+
121+
## Requests
122+
123+
You can create a raw request object using a HttpUrl and a HttpMethod, including additional headers and a body data.
124+
125+
```swift
126+
let url = HttpUrl(host: "localhost", path: ["login"])
127+
128+
let token: String = "valid-token"
129+
let body = try JSONEncoder().encode([
130+
"foo": "bar",
131+
])
132+
let req = HttpRawRequest(url: url,
133+
method: .post,
134+
headers: [
135+
.key(.authorization): "Bearer \(token)",
136+
.custom("my-header"): "my-header-value",
137+
],
138+
body: body)
139+
140+
/*
141+
curl "https://localhost/login/" \
142+
-X POST \
143+
-H 'my-header: my-header-value' \
144+
-H 'Authorization: Bearer valid-token' \
145+
-d '{"foo":"bar"}'
146+
*/
147+
print(req.urlRequest.curlString)
148+
```
149+
150+
You can transform a HttpRequest into a URLReequest via the `.urlRequest` property.
151+
You can print the cURL representation of a request by using the `.curlString` property on a URLRequest object.
152+
153+
154+
## Pipelines
155+
156+
A pipeline allows you to transform a raw request and a raw response using a set of custom actions.
157+
158+
You can create your own HttpRequestTransformer object to add extra headers to your request and encode a custom body object to a data value.
159+
160+
You can create your own HttpResponseTransformer object to validate the response and decode a custom value from the response data.
161+
162+
The codable (encodable, decodable, codable) pipelines are a good example of this approach.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// HttpRawRequestTests.swift
3+
// SwiftHttpTests
4+
//
5+
// Created by Tibor Bodecs on 2022. 03. 13..
6+
//
7+
8+
import XCTest
9+
@testable import SwiftHttp
10+
11+
final class HttpRawRequestTests: XCTestCase {
12+
13+
func testRawRequest() async throws {
14+
let client = UrlSessionHttpClient(session: .shared, log: true)
15+
16+
let url = HttpUrl(scheme: "https",
17+
host: "jsonplaceholder.typicode.com",
18+
port: 80,
19+
path: ["todos"],
20+
resource: nil,
21+
query: [:],
22+
fragment: nil)
23+
24+
let req = HttpRawRequest(url: url, method: .get, headers: [:], body: nil)
25+
26+
let response = try await client.dataTask(req)
27+
let todos = try JSONDecoder().decode([Todo].self, from: response.data)
28+
XCTAssertEqual(response.statusCode, .ok)
29+
XCTAssertEqual(todos.count, 200)
30+
}
31+
}
32+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// HttpRequestTests.swift
3+
// SwiftHttp
4+
//
5+
// Created by Tibor Bodecs on 2022. 03. 13..
6+
//
7+
8+
import XCTest
9+
@testable import SwiftHttp
10+
11+
final class HttpRequestTests: XCTestCase {
12+
13+
func testRequests() async throws {
14+
let url = HttpUrl(host: "localhost", path: ["login"])
15+
16+
let token: String = "valid-token"
17+
let body = try JSONEncoder().encode([
18+
"foo": "bar",
19+
])
20+
let req = HttpRawRequest(url: url,
21+
method: .post,
22+
headers: [
23+
.key(.authorization): "Bearer \(token)",
24+
.custom("my-header"): "my-header-value",
25+
],
26+
body: body)
27+
28+
let expectation = """
29+
curl "https://localhost/login/" \\
30+
\t-X POST \\
31+
\t-H 'my-header: my-header-value' \\
32+
\t-H 'Authorization: Bearer valid-token' \\
33+
\t-d '{"foo":"bar"}'
34+
"""
35+
36+
XCTAssertEqual(req.urlRequest.curlString, expectation)
37+
}
38+
}
39+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// HttpUrlTests.swift
3+
// SwiftHttpTests
4+
//
5+
// Created by Tibor Bodecs on 2022. 03. 13..
6+
//
7+
8+
import XCTest
9+
@testable import SwiftHttp
10+
11+
final class HttpUrlTests: XCTestCase {
12+
13+
func testBaseUrl() async throws {
14+
let baseUrl = HttpUrl(host: "jsonplaceholder.typicode.com")
15+
16+
let todosUrl = baseUrl.path("todos")
17+
XCTAssertEqual(todosUrl.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/")
18+
19+
let todoUrl = baseUrl.path("todos", String(1))
20+
XCTAssertEqual(todoUrl.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/1/")
21+
22+
let query1Url = baseUrl.path("todos").query("foo", "bar")
23+
XCTAssertEqual(query1Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/?foo=bar")
24+
25+
let query2Url = baseUrl.path("todos").query([
26+
"foo": "baz",
27+
"bar": "1",
28+
])
29+
XCTAssertEqual(query2Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/?foo=baz&bar=1")
30+
}
31+
}
32+

0 commit comments

Comments
 (0)