This file contains hidden or 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
| class HttpClient { | |
| typealias completeClosure = ( _ data: Data?, _ error: Error?)->Void | |
| private let session: URLSession | |
| init(session: URLSessionProtocol) { | |
| self.session = session | |
| } | |
| func get( url: URL, callback: @escaping completeClosure ) { | |
| let request = NSMutableURLRequest(url: url) | |
| request.httpMethod = "GET" | |
| let task = session.dataTask(with: request) { (data, response, error) in |
This file contains hidden or 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
| let task = URLSession.shared.dataTask() |
This file contains hidden or 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
| let task = session.dataTask() |
This file contains hidden or 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
| class HttpClientTests: XCTestCase { | |
| var httpClient: HttpClient! | |
| let session = MockURLSession() | |
| override func setUp() { | |
| super.setUp() | |
| httpClient = HttpClient(session: session) | |
| } | |
| override func tearDown() { | |
| super.tearDown() | |
| } |
This file contains hidden or 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
| func test_get_request_withURL() { | |
| guard let url = URL(string: "https://mockurl") else { | |
| fatalError("URL can't be empty") | |
| } | |
| httpClient.get(url: url) { (success, response) in | |
| // Return data | |
| } | |
| // Assert | |
| } |
This file contains hidden or 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
| let task = session.dataTask(with: request) { (data, response, error) in | |
| callback(data, error) | |
| } | |
| task.resume() |
This file contains hidden or 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
| class MockURLSession { | |
| private (set) var lastURL: URL? | |
| func dataTask(with request: NSURLRequest, completionHandler: @escaping DataTaskResult) -> URLSessionDataTask { | |
| lastURL = request.url | |
| completionHandler(nextData, successHttpURLResponse(request: request), nextError) | |
| return // dataTask, will be impletmented later | |
| } | |
| } |
This file contains hidden or 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
| func test_get_request_withURL() { | |
| guard let url = URL(string: "https://mockurl") else { | |
| fatalError("URL can't be empty") | |
| } | |
| httpClient.get(url: url) { (success, response) in | |
| // Return data | |
| } | |
| XCTAssert(session.lastURL == url) | |
| } |
This file contains hidden or 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
| class MockURLSessionDataTask { | |
| func resume() { } | |
| } |
This file contains hidden or 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
| class HttpClientTests: XCTestCase { | |
| var httpClient: HttpClient! | |
| let session = MockURLSession() | |
| override func setUp() { | |
| super.setUp() | |
| httpClient = HttpClient(session: session) // Doesn't compile | |
| } | |
| override func tearDown() { | |
| super.tearDown() | |
| } |