-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPRequestTests.swift
More file actions
172 lines (150 loc) · 7.47 KB
/
HTTPRequestTests.swift
File metadata and controls
172 lines (150 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// HTTPRequestTests.swift
// QuickHatchHTTP
//
// Created by Daniel Koster on 10/13/25.
//
import QuickHatchHTTP
@testable import QuickHatchHTTPMocks
import Foundation
import Testing
import Combine
final class HTTPRequestTests {
private var cancellables = Set<AnyCancellable>()
@Test
func asURLRequest_expectHeadersCorrectlyParsed() throws {
let sut = QHHTTPRequest<DataModel>(headers: ["Content-Type": "json"],
url: "quickhatch.com",
method: .get)
let result = try sut.asURLRequest()
let headers = try #require(result.allHTTPHeaderFields)
let url = try #require(result.url?.absoluteString)
let method = try #require(result.httpMethod)
#expect(url == "quickhatch.com")
#expect(headers == ["Content-Type": "json"])
#expect(method == "GET")
}
@Test
func response_whenNoErrorSpecified_expectCorrectResponse() async throws {
let requestFactoryMock = NetworkRequestFactoryMock()
let sut = QHHTTPRequest<DataModel>(url: "quickhatch.com", method: .get, requestFactory: requestFactoryMock)
requestFactoryMock.asyncDataResponseResult = URLSessionMocks.anyResponse()
do {
_ = try await sut.response()
#expect(requestFactoryMock.invokedAsyncDataCount == 1)
}
catch _ {
#expect(Bool(false))
}
}
@Test func response_whenErrorThrown_expectCatchToWork() async throws {
let requestFactoryMock = NetworkRequestFactoryMock()
let sut = QHHTTPRequest<DataModel>(url: "quickhatch.com", method: .get, requestFactory: requestFactoryMock)
requestFactoryMock.asyncDataResponseResult = URLSessionMocks.anyResponse()
requestFactoryMock.asyncDataErrorThrown = RequestError.requestWithError(statusCode: HTTPStatusCode.badRequest)
do {
_ = try await sut.response()
}
catch let error as RequestError {
#expect(error == .requestWithError(statusCode: .badRequest))
#expect(requestFactoryMock.invokedAsyncDataCount == 1)
}
}
@Test
func responseDecoded_whenNoErrorSpecified_expectCorrectResponse() async throws {
let requestFactoryMock = NetworkRequestFactoryMock()
let sut = QHHTTPRequest<DataModel>(url: "quickhatch.com", method: .get, requestFactory: requestFactoryMock)
requestFactoryMock.asyncDataResponseResult = URLSessionMocks.anyResponse(withData: URLSessionMocks.anyDataModelSample)
do {
let response = try await sut.responseDecoded()
let dataExpected = DataModel(name: "dan", nick: "sp", age: 12)
#expect(requestFactoryMock.invokedAsyncDataCount == 1)
#expect(dataExpected.name == response.data.name)
}
catch _ {
#expect(Bool(false))
}
}
@Test func responseDecoded_whenErrorThrown_expectCatchToWork() async throws {
let requestFactoryMock = NetworkRequestFactoryMock()
let sut = QHHTTPRequest<DataModel>(url: "quickhatch.com", method: .get, requestFactory: requestFactoryMock)
requestFactoryMock.asyncDataResponseResult = URLSessionMocks.anyResponse()
requestFactoryMock.asyncDataErrorThrown = RequestError.requestWithError(statusCode: HTTPStatusCode.badRequest)
do {
_ = try await sut.responseDecoded()
}
catch let error as RequestError {
#expect(error == .requestWithError(statusCode: .badRequest))
#expect(requestFactoryMock.invokedAsyncDataCount == 1)
}
}
@Test func responseDecodedPublisher_whenErrorThrown_expectCatchToWork() async throws {
let requestFactoryMock = NetworkRequestFactoryMock()
let sut = QHHTTPRequest<DataModel>(url: "quickhatch.com", method: .get, requestFactory: requestFactoryMock)
requestFactoryMock.subject.send(completion: Subscribers.Completion.failure(RequestError.requestWithError(statusCode: HTTPStatusCode.badRequest)))
await confirmation("") { confirmation in
sut.responseDecodedPublisher.sink(receiveCompletion: { completion in
switch completion {
case .failure(let error as RequestError):
#expect(error == .requestWithError(statusCode: HTTPStatusCode.badRequest))
confirmation.confirm()
case .failure(_): break
case .finished: break
}
},
receiveValue: { dataModel in }).store(in: &cancellables)
}
}
@Test func responseDecodedPublisher_whenNoErrorThrown_expectCorrectResponse() async throws {
let requestFactoryMock = NetworkRequestFactoryMock()
let sut = QHHTTPRequest<DataModel>(url: "quickhatch.com", method: .get, requestFactory: requestFactoryMock)
let response = DataModel(name: "dan", nick: "sp", age: 12)
await confirmation("") { confirmation in
sut.responseDecodedPublisher.sink(receiveCompletion: { completion in
switch completion {
case .failure(_): break
case .finished: break
}
},
receiveValue: { dataModel in
#expect(dataModel.name == response.name)
confirmation.confirm()
}).store(in: &cancellables)
requestFactoryMock.subject.send(URLSessionMocks.anyResponse(withData: URLSessionMocks.anyDataModelSample))
}
}
@Test func responsePublisher_whenErrorThrown_expectCatchToWork() async throws {
let requestFactoryMock = NetworkRequestFactoryMock()
let sut = QHHTTPRequest<DataModel>(url: "quickhatch.com", method: .get, requestFactory: requestFactoryMock)
requestFactoryMock.subject.send(completion: Subscribers.Completion.failure(RequestError.requestWithError(statusCode: HTTPStatusCode.badRequest)))
await confirmation("") { confirmation in
sut.responsePublisher.sink(receiveCompletion: { completion in
switch completion {
case .failure(let error as RequestError):
#expect(error == .requestWithError(statusCode: HTTPStatusCode.badRequest))
confirmation.confirm()
case .failure(_): break
case .finished: break
}
},
receiveValue: { _ in }).store(in: &cancellables)
}
}
@Test func responsePublisher_whenNoErrorThrown_expectCorrectResponse() async throws {
let requestFactoryMock = NetworkRequestFactoryMock()
let sut = QHHTTPRequest<DataModel>(url: "quickhatch.com", method: .get, requestFactory: requestFactoryMock)
await confirmation("") { confirmation in
sut.responsePublisher.sink(receiveCompletion: { completion in
switch completion {
case .failure(_): break
case .finished: break
}
},
receiveValue: { response in
#expect(response.body != nil)
confirmation.confirm()
}).store(in: &cancellables)
requestFactoryMock.subject.send(URLSessionMocks.anyResponse(withData: URLSessionMocks.anyDataModelSample))
}
}
}