Skip to content

Commit fddae47

Browse files
committed
tests & docs
1 parent 60e5780 commit fddae47

File tree

4 files changed

+128
-5
lines changed

4 files changed

+128
-5
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,39 @@ You can transform a HttpRequest into a URLReequest via the `.urlRequest` propert
151151
You can print the cURL representation of a request by using the `.curlString` property on a URLRequest object.
152152

153153

154+
## Response validation
155+
156+
You can validate a response by using a HttpResponseValidator object.
157+
158+
```swift
159+
// mock response
160+
let response = HttpRawResponse(statusCode: .ok,
161+
headers: [
162+
.key(.contentType): "application/json",
163+
],
164+
data: .init())
165+
166+
// check if the status code is between 200 and 299
167+
let validator1 = HttpStatusCodeValidator() // -> (.ok), (.notFound), etc.
168+
try validator1.validate(response)
169+
170+
171+
// check if a header key exists and the value is equal to "application/json"
172+
let validator2 = HttpHeaderValidator(.key(.contentType)) { value in
173+
value == "application/json"
174+
}
175+
176+
try validator2.validate(response)
177+
178+
179+
// validate using multiple validators
180+
let validation = HttpResponseValidation([validator1, validator2])
181+
try validation.validate(response)
182+
```
183+
184+
It is possible to check for multiple validation criterias using a HttpResponseValidation.
185+
186+
154187
## Pipelines
155188

156189
A pipeline allows you to transform a raw request and a raw response using a set of custom actions.
@@ -160,3 +193,5 @@ You can create your own HttpRequestTransformer object to add extra headers to yo
160193
You can create your own HttpResponseTransformer object to validate the response and decode a custom value from the response data.
161194

162195
The codable (encodable, decodable, codable) pipelines are a good example of this approach.
196+
197+

Tests/SwiftHttpTests/HttpRequestTests.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ final class HttpRequestTests: XCTestCase {
2121
method: .post,
2222
headers: [
2323
.key(.authorization): "Bearer \(token)",
24-
.custom("my-header"): "my-header-value",
2524
],
2625
body: body)
2726

2827
let expectation = """
2928
curl "https://localhost/login/" \\
3029
\t-X POST \\
31-
\t-H 'my-header: my-header-value' \\
3230
\t-H 'Authorization: Bearer valid-token' \\
3331
\t-d '{"foo":"bar"}'
3432
"""

Tests/SwiftHttpTests/HttpUrlTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ final class HttpUrlTests: XCTestCase {
2323
XCTAssertEqual(query1Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/?foo=bar")
2424

2525
let query2Url = baseUrl.path("todos").query([
26-
"foo": "baz",
27-
"bar": "1",
26+
"foo": "1",
2827
])
29-
XCTAssertEqual(query2Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/?foo=baz&bar=1")
28+
XCTAssertEqual(query2Url.url.absoluteString, "https://jsonplaceholder.typicode.com/todos/?foo=1")
3029
}
3130
}
3231

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// HttpValidationTests.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 HttpValidationTests: XCTestCase {
12+
13+
func testStatusCodeValidator() async throws {
14+
let response = HttpRawResponse(statusCode: .ok, headers: [:], data: .init())
15+
let validator1 = HttpStatusCodeValidator() // 200...299
16+
try validator1.validate(response)
17+
18+
let validator2 = HttpStatusCodeValidator(.ok) // 200
19+
try validator2.validate(response)
20+
}
21+
22+
func testBadStatusCodeValidator() async throws {
23+
let response = HttpRawResponse(statusCode: .notFound, headers: [:], data: .init())
24+
let validator = HttpStatusCodeValidator() // 200...299
25+
do {
26+
try validator.validate(response)
27+
XCTFail("Bad validation result")
28+
}
29+
catch {
30+
31+
}
32+
}
33+
34+
func testBadStatusCodeValidator2() async throws {
35+
let response = HttpRawResponse(statusCode: .notFound, headers: [:], data: .init())
36+
let validator = HttpStatusCodeValidator(.ok)
37+
do {
38+
try validator.validate(response)
39+
XCTFail("Bad validation result")
40+
}
41+
catch {
42+
43+
}
44+
}
45+
46+
func testHeaderValidator() async throws {
47+
let response = HttpRawResponse(statusCode: .ok,
48+
headers: [
49+
.key(.contentType): "application/json",
50+
],
51+
data: .init())
52+
let validator = HttpHeaderValidator(.key(.contentType)) { value in
53+
value == "application/json"
54+
}
55+
try validator.validate(response)
56+
}
57+
58+
func testInvalidHeaderValidator() async throws {
59+
let response = HttpRawResponse(statusCode: .ok,
60+
headers: [
61+
.key(.contentType): "application/json",
62+
],
63+
data: .init())
64+
let validator = HttpHeaderValidator(.key(.contentType)) { value in
65+
value == "application/xml"
66+
}
67+
do {
68+
try validator.validate(response)
69+
XCTFail("Bad validation result")
70+
}
71+
catch {
72+
73+
}
74+
}
75+
76+
func testMultipleValidation() async throws {
77+
let response = HttpRawResponse(statusCode: .ok,
78+
headers: [
79+
.key(.contentType): "application/json",
80+
],
81+
data: .init())
82+
83+
let validator1 = HttpStatusCodeValidator(.ok)
84+
let validator2 = HttpHeaderValidator(.key(.contentType)) { value in
85+
value == "application/json"
86+
}
87+
let validation = HttpResponseValidation([validator1, validator2])
88+
try validation.validate(response)
89+
}
90+
}
91+

0 commit comments

Comments
 (0)