-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from freshOS/tests
Adds get, post, put patch & delete unit tests
- Loading branch information
Showing
8 changed files
with
1,151 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
// | ||
// DeleteRequestTests.swift | ||
// | ||
// | ||
// Created by Sacha DSO on 12/04/2022. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
import Combine | ||
|
||
@testable | ||
import Networking | ||
|
||
class DeletehRequestTests: XCTestCase { | ||
|
||
private let network = NetworkingClient(baseURL: "https://mocked.com") | ||
private var cancellables = Set<AnyCancellable>() | ||
|
||
override func setUpWithError() throws { | ||
network.sessionConfiguration.protocolClasses = [MockingURLProtocol.self] | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
MockingURLProtocol.mockedResponse = "" | ||
MockingURLProtocol.currentRequest = nil | ||
} | ||
|
||
func testPOSTVoidWorks() { | ||
MockingURLProtocol.mockedResponse = | ||
""" | ||
{ "response": "OK" } | ||
""" | ||
let expectationWorks = expectation(description: "Call works") | ||
let expectationFinished = expectation(description: "Finished") | ||
network.delete("/users").sink { completion in | ||
switch completion { | ||
case .failure(_): | ||
XCTFail() | ||
case .finished: | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE") | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users") | ||
expectationFinished.fulfill() | ||
} | ||
} receiveValue: { () in | ||
expectationWorks.fulfill() | ||
} | ||
.store(in: &cancellables) | ||
waitForExpectations(timeout: 0.1) | ||
} | ||
|
||
func testPOSTDataWorks() { | ||
MockingURLProtocol.mockedResponse = | ||
""" | ||
{ "response": "OK" } | ||
""" | ||
let expectationWorks = expectation(description: "ReceiveValue called") | ||
let expectationFinished = expectation(description: "Finished called") | ||
network.delete("/users").sink { completion in | ||
switch completion { | ||
case .failure: | ||
XCTFail() | ||
case .finished: | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE") | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users") | ||
expectationFinished.fulfill() | ||
} | ||
} receiveValue: { (data: Data) in | ||
XCTAssertEqual(data, MockingURLProtocol.mockedResponse.data(using: String.Encoding.utf8)) | ||
expectationWorks.fulfill() | ||
} | ||
.store(in: &cancellables) | ||
waitForExpectations(timeout: 0.1) | ||
} | ||
|
||
func testPOSTJSONWorks() { | ||
MockingURLProtocol.mockedResponse = | ||
""" | ||
{"response":"OK"} | ||
""" | ||
let expectationWorks = expectation(description: "ReceiveValue called") | ||
let expectationFinished = expectation(description: "Finished called") | ||
network.delete("/users").sink { completion in | ||
switch completion { | ||
case .failure: | ||
XCTFail() | ||
case .finished: | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE") | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users") | ||
expectationFinished.fulfill() | ||
} | ||
} receiveValue: { (json: Any) in | ||
let data = try? JSONSerialization.data(withJSONObject: json, options: []) | ||
let expectedResponseData = | ||
""" | ||
{"response":"OK"} | ||
""".data(using: String.Encoding.utf8) | ||
|
||
XCTAssertEqual(data, expectedResponseData) | ||
expectationWorks.fulfill() | ||
} | ||
.store(in: &cancellables) | ||
waitForExpectations(timeout: 0.1) | ||
} | ||
|
||
func testPOSTNetworkingJSONDecodableWorks() { | ||
MockingURLProtocol.mockedResponse = | ||
""" | ||
{ | ||
"firstname":"John", | ||
"lastname":"Doe", | ||
} | ||
""" | ||
let expectationWorks = expectation(description: "ReceiveValue called") | ||
let expectationFinished = expectation(description: "Finished called") | ||
network.delete("/users/1") | ||
.sink { completion in | ||
switch completion { | ||
case .failure: | ||
XCTFail() | ||
case .finished: | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE") | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users/1") | ||
expectationFinished.fulfill() | ||
} | ||
} receiveValue: { (userJSON: UserJSON) in | ||
XCTAssertEqual(userJSON.firstname, "John") | ||
XCTAssertEqual(userJSON.lastname, "Doe") | ||
expectationWorks.fulfill() | ||
} | ||
.store(in: &cancellables) | ||
waitForExpectations(timeout: 0.1) | ||
} | ||
|
||
func testPOSTArrayOfNetworkingJSONDecodableWorks() { | ||
MockingURLProtocol.mockedResponse = | ||
""" | ||
[ | ||
{ | ||
"firstname":"John", | ||
"lastname":"Doe" | ||
}, | ||
{ | ||
"firstname":"Jimmy", | ||
"lastname":"Punchline" | ||
} | ||
] | ||
""" | ||
let expectationWorks = expectation(description: "ReceiveValue called") | ||
let expectationFinished = expectation(description: "Finished called") | ||
network.delete("/users") | ||
.sink { completion in | ||
switch completion { | ||
case .failure: | ||
XCTFail() | ||
case .finished: | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE") | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users") | ||
expectationFinished.fulfill() | ||
} | ||
} receiveValue: { (userJSON: [UserJSON]) in | ||
XCTAssertEqual(userJSON[0].firstname, "John") | ||
XCTAssertEqual(userJSON[0].lastname, "Doe") | ||
XCTAssertEqual(userJSON[1].firstname, "Jimmy") | ||
XCTAssertEqual(userJSON[1].lastname, "Punchline") | ||
expectationWorks.fulfill() | ||
} | ||
.store(in: &cancellables) | ||
waitForExpectations(timeout: 0.1) | ||
} | ||
|
||
func testPOSTArrayOfNetworkingJSONDecodableWithKeypathWorks() { | ||
MockingURLProtocol.mockedResponse = | ||
""" | ||
{ | ||
"users" : | ||
[ | ||
{ | ||
"firstname":"John", | ||
"lastname":"Doe" | ||
}, | ||
{ | ||
"firstname":"Jimmy", | ||
"lastname":"Punchline" | ||
} | ||
] | ||
} | ||
""" | ||
let expectationWorks = expectation(description: "ReceiveValue called") | ||
let expectationFinished = expectation(description: "Finished called") | ||
network.delete("/users", keypath: "users") | ||
.sink { completion in | ||
switch completion { | ||
case .failure: | ||
XCTFail() | ||
case .finished: | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.httpMethod, "DELETE") | ||
XCTAssertEqual(MockingURLProtocol.currentRequest?.url?.absoluteString, "https://mocked.com/users") | ||
expectationFinished.fulfill() | ||
} | ||
} receiveValue: { (userJSON: [UserJSON]) in | ||
XCTAssertEqual(userJSON[0].firstname, "John") | ||
XCTAssertEqual(userJSON[0].lastname, "Doe") | ||
XCTAssertEqual(userJSON[1].firstname, "Jimmy") | ||
XCTAssertEqual(userJSON[1].lastname, "Punchline") | ||
expectationWorks.fulfill() | ||
} | ||
.store(in: &cancellables) | ||
waitForExpectations(timeout: 0.1) | ||
} | ||
|
||
} |
Oops, something went wrong.