Skip to content

Commit c1514b1

Browse files
committed
Add unit test for faking a response with a status code
1 parent 33878e8 commit c1514b1

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

Sources/Networking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ extension Networking {
402402
completion(response: fakeRequest.response, headers: [String : AnyObject](), error: nil)
403403
} else {
404404
let error = NSError(domain: Networking.ErrorDomain, code: fakeRequest.statusCode, userInfo: [NSLocalizedDescriptionKey : NSHTTPURLResponse.localizedStringForStatusCode(fakeRequest.statusCode)])
405-
completion(response: nil, headers: [String : AnyObject](), error: error)
405+
completion(response: fakeRequest.response, headers: [String : AnyObject](), error: error)
406406
}
407407
} else {
408408
switch responseType {

Tests/GETTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class GETTests: XCTestCase {
5353
}
5454
}
5555

56+
// TODO: I'm not sure how it implement this, since I need a service that returns a faulty
57+
// status code, meaning not 2XX, and at the same time it returns a JSON response.
58+
func testGETWithInvalidPathAndJSONError() {
59+
60+
}
61+
5662
func testFakeGET() {
5763
let networking = Networking(baseURL: baseURL)
5864

@@ -75,6 +81,18 @@ class GETTests: XCTestCase {
7581
}
7682
}
7783

84+
func testFakeGETWithInvalidPathAndJSONError() {
85+
let networking = Networking(baseURL: baseURL)
86+
87+
let response = ["error_message" : "Shit went down"]
88+
networking.fakeGET("/stories", response: response, statusCode: 401)
89+
90+
networking.GET("/stories") { JSON, error in
91+
XCTAssertEqual(JSON as! [String : String], response)
92+
XCTAssertEqual(error?.code, 401)
93+
}
94+
}
95+
7896
func testFakeGETUsingFile() {
7997
let networking = Networking(baseURL: baseURL)
8098

0 commit comments

Comments
 (0)