Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed Emad Abdalla Hegab committed Jul 18, 2018
1 parent 5b3941d commit 0e87d77
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ script:

after_success:
- if [ $COVERAGE == "YES" ]; then
export CODECOV_TOKEN="1521a0fc-8401-4c98-b0aa-b307c02acdcb"
bash <(curl -s https://codecov.io/bash);
fi
11 changes: 3 additions & 8 deletions MHNetwork/Network/NetworkDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public class NetworkDispatcher: Dispatcher {
urlRequest.httpBody = jsonData
createHeadersInRequest(urlRequest, request, onComplete: { (urlRequest) in
onComplete(urlRequest)
}) { (error) in
onError(error)
}
})

}
case .url(let params):
Expand All @@ -72,16 +70,13 @@ public class NetworkDispatcher: Dispatcher {
urlRequest.url = components.url
createHeadersInRequest(urlRequest, request, onComplete: { (urlRequest) in
onComplete(urlRequest)
}) { (error) in
onError(error)
}
})
}
}

fileprivate func createHeadersInRequest(_ urlRequest: URLRequest,
_ request: Request,
onComplete: @escaping (URLRequest) -> Void,
onError: @escaping (NetworkErrors) -> Void) {
onComplete: @escaping (URLRequest) -> Void) {
// Add headers from enviornment and request
var urlRequest = urlRequest
self.environment.headers?.forEach { urlRequest.addValue($0.value as! String, forHTTPHeaderField: $0.key) }
Expand Down
24 changes: 20 additions & 4 deletions MHNetworkTests/NetworkDispatcherTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let timeout: TimeInterval = 10

private enum MockQuoteRequest: Request {


case getRandomQuote

var path: String {
Expand Down Expand Up @@ -71,9 +72,9 @@ private class MockBadRequest: Request {

}

private class MockQuoteTask<T: Codable>: MHNetwork.Operation {
var shouldFail = false
private class MockQuoteTask<T: Codable>: Operations {
var body: Bool = false

var request: Request {
return MockQuoteRequest.getRandomQuote
}
Expand Down Expand Up @@ -109,7 +110,8 @@ private class MockQuoteTask<T: Codable>: MHNetwork.Operation {
}
}

private class MockBadTask<T: Codable>: MHNetwork.Operation {

private class MockBadTask<T: Codable>: Operations {

var body: Bool = false
var request: Request {
Expand Down Expand Up @@ -183,7 +185,19 @@ class NetworkDispatcherTests: XCTestCase {
}

func testBadURL() {

env = Environment(host: "BADURL")
env.headers = ["Authorization" : "1234"]
let session = URLSession(configuration: URLSessionConfiguration.default)
networkDispatcher = NetworkDispatcher(environment: env, session: session)
let expectation = self.expectation(description: "network failed to connect")
mockTask.exeute(in: networkDispatcher, completed: { _ in
XCTFail("Should not succeed")
}) { (error) in
expectation.fulfill()
XCTAssertNotNil(error)
}

waitForExpectations(timeout: timeout, handler: nil)
}

func testBadRequest() {
Expand Down Expand Up @@ -227,4 +241,6 @@ class NetworkDispatcherTests: XCTestCase {

waitForExpectations(timeout: timeout, handler: nil)
}

}

0 comments on commit 0e87d77

Please sign in to comment.