Skip to content

Commit 00164a7

Browse files
committed
More tests
1 parent 22f5977 commit 00164a7

File tree

2 files changed

+112
-4
lines changed

2 files changed

+112
-4
lines changed

RxHttpClient/HttpClientType+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public extension HttpClientType {
6262
- returns: Created observable that emits stream events
6363
*/
6464
func request(url: URL, method: HttpMethod = .get, jsonBody: Any, options: JSONSerialization.WritingOptions = [], httpHeaders: [String: String] = [:],
65-
dataCacheProvider: DataCacheProviderType? = nil) throws -> Observable<StreamTaskEvents> {
65+
dataCacheProvider: DataCacheProviderType? = nil) -> Observable<StreamTaskEvents> {
6666
guard let req = URLRequest(url: url, method: method, jsonBody: jsonBody, options: options, headers: httpHeaders) else {
6767
return Observable.error(HttpClientError.invalidJsonObject)
6868
}

RxHttpClientTests/HttpClientSendTests.swift

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class HttpClientSendTests: XCTestCase {
2222
super.tearDown()
2323
}
2424

25-
func testSendBody() {
25+
func testSendBody_1() {
2626
let session = FakeSession()
2727
let client = HttpClient(session: session)
2828
let sendData = "testData".data(using: String.Encoding.utf8)!
@@ -58,7 +58,7 @@ class HttpClientSendTests: XCTestCase {
5858
waitForExpectations(timeout: 1, handler: nil)
5959
}
6060

61-
func testSendJsonBody() {
61+
func testSendJsonBody_1() {
6262
let session = FakeSession()
6363
let client = HttpClient(session: session)
6464
let sendJson = ["Key1":"Value1", "Key2":"Value2"]
@@ -96,7 +96,87 @@ class HttpClientSendTests: XCTestCase {
9696
waitForExpectations(timeout: 1, handler: nil)
9797
}
9898

99-
func testRequestWithIncorrectJsonObject() {
99+
func testSendJsonBody_2() {
100+
let session = FakeSession()
101+
let client = HttpClient(session: session)
102+
let sendJson = ["Key1":"Value1", "Key2":"Value2"]
103+
104+
let sendJsonData = try! JSONSerialization.data(withJSONObject: sendJson, options: [])
105+
106+
let actions: (FakeDataTask) -> () = { task in
107+
DispatchQueue.global(qos: DispatchQoS.QoSClass.utility).async { _ in
108+
guard task.originalRequest!.url! == URL(baseUrl: "https://test.com/post", parameters: ["post":"Request"])!,
109+
task.originalRequest!.httpMethod == HttpMethod.patch.rawValue,
110+
task.originalRequest!.allHTTPHeaderFields?["Header1"] == "HeaderVal1",
111+
task.originalRequest!.httpBody?.elementsEqual(sendJsonData) ?? false else {
112+
client.sessionObserver.sessionEventsSubject.onNext(.didCompleteWithError(session: session,
113+
dataTask: task,
114+
error: NSError(domain: "HttpRequestTests", code: 1, userInfo: nil)))
115+
116+
return
117+
}
118+
119+
client.sessionObserver.sessionEventsSubject.onNext(
120+
SessionDataEvents.didReceiveResponse(session: session,
121+
dataTask: task,
122+
response: URLResponse(url: task.originalRequest!.url!, mimeType: "Application/json", expectedContentLength: 26, textEncodingName: nil),
123+
completion: { _ in }))
124+
client.sessionObserver.sessionEventsSubject.onNext(.didReceiveData(session: session, dataTask: task, data: sendJsonData))
125+
client.sessionObserver.sessionEventsSubject.onNext(.didCompleteWithError(session: session,
126+
dataTask: task,
127+
error: nil))
128+
}
129+
}
130+
131+
session.task = FakeDataTask(resumeClosure: actions)
132+
133+
let url = URL(baseUrl: "https://test.com/post", parameters: ["post":"Request"])!
134+
let expectation = self.expectation(description: "Should receive OK response")
135+
136+
_ = client.requestJson(url: url, method: .patch, jsonBody: sendJson, options: [], httpHeaders: ["Header1": "HeaderVal1"], requestCacheMode: CacheMode.withoutCache)
137+
.subscribe(onNext: { _ in expectation.fulfill() }, onError: { _ in XCTFail("error returned") })
138+
139+
waitForExpectations(timeout: 1, handler: nil)
140+
}
141+
142+
func testSendJsonBody_3() {
143+
let session = FakeSession()
144+
let client = HttpClient(session: session)
145+
let sendJson = ["Key1":"Value1", "Key2":"Value2"]
146+
147+
let sendJsonData = try! JSONSerialization.data(withJSONObject: sendJson, options: [])
148+
149+
let actions: (FakeDataTask) -> () = { task in
150+
DispatchQueue.global(qos: DispatchQoS.QoSClass.utility).async { _ in
151+
guard task.originalRequest!.url! == URL(baseUrl: "https://test.com/post", parameters: ["post":"Request"])!,
152+
task.originalRequest!.httpMethod == HttpMethod.patch.rawValue,
153+
task.originalRequest!.allHTTPHeaderFields?["Header1"] == "HeaderVal1",
154+
task.originalRequest!.httpBody?.elementsEqual(sendJsonData) ?? false else {
155+
client.sessionObserver.sessionEventsSubject.onNext(.didCompleteWithError(session: session,
156+
dataTask: task,
157+
error: NSError(domain: "HttpRequestTests", code: 1, userInfo: nil)))
158+
159+
return
160+
}
161+
162+
client.sessionObserver.sessionEventsSubject.onNext(.didCompleteWithError(session: session,
163+
dataTask: task,
164+
error: nil))
165+
}
166+
}
167+
168+
session.task = FakeDataTask(resumeClosure: actions)
169+
170+
let url = URL(baseUrl: "https://test.com/post", parameters: ["post":"Request"])!
171+
let expectation = self.expectation(description: "Should receive OK response")
172+
173+
_ = client.request(url: url, method: .patch, jsonBody: sendJson, options: [], httpHeaders: ["Header1": "HeaderVal1"])
174+
.subscribe(onNext: { _ in expectation.fulfill() }, onError: { _ in XCTFail("error returned") })
175+
176+
waitForExpectations(timeout: 1, handler: nil)
177+
}
178+
179+
func testRequestWithIncorrectJsonObject_1() {
100180
let url = URL(baseUrl: "https://test.com/post", parameters: ["post":"Request"])!
101181
let expectation = self.expectation(description: "Should receive error")
102182

@@ -109,4 +189,32 @@ class HttpClientSendTests: XCTestCase {
109189

110190
waitForExpectations(timeout: 1, handler: nil)
111191
}
192+
193+
func testRequestWithIncorrectJsonObject_2() {
194+
let url = URL(baseUrl: "https://test.com/post", parameters: ["post":"Request"])!
195+
let expectation = self.expectation(description: "Should receive error")
196+
197+
let client = HttpClient()
198+
_ = client.requestJson(url: url, method: .patch, jsonBody: 2, options: [], httpHeaders: ["Header1": "HeaderVal1"], requestCacheMode: CacheMode.withoutCache)
199+
.subscribe(onNext: { _ in XCTFail("Should not return data") }, onError: { error in
200+
guard case HttpClientError.invalidJsonObject = error else { XCTFail("Incorrect error returned"); return }
201+
expectation.fulfill()
202+
})
203+
204+
waitForExpectations(timeout: 1, handler: nil)
205+
}
206+
207+
func testRequestWithIncorrectJsonObject_3() {
208+
let url = URL(baseUrl: "https://test.com/post", parameters: ["post":"Request"])!
209+
let expectation = self.expectation(description: "Should receive error")
210+
211+
let client = HttpClient()
212+
_ = client.request(url: url, method: .patch, jsonBody: 2, options: [], httpHeaders: ["Header1": "HeaderVal1"])
213+
.subscribe(onNext: { _ in XCTFail("Should not return data") }, onError: { error in
214+
guard case HttpClientError.invalidJsonObject = error else { XCTFail("Incorrect error returned"); return }
215+
expectation.fulfill()
216+
})
217+
218+
waitForExpectations(timeout: 1, handler: nil)
219+
}
112220
}

0 commit comments

Comments
 (0)