Created
March 3, 2024 06:33
-
-
Save MounikaMadishetti/c9e935ef2dd8711ac03e83bb9563c4c1 to your computer and use it in GitHub Desktop.
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
// MARK: - Main Target | |
protocol HTTPSession { | |
func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> HTTPSessionTask | |
} | |
protocol HTTPSessionTask { | |
func resume() | |
} | |
final class HTTPClient { | |
let session: HTTPSession | |
init(session: HTTPSession) { | |
self.session = session | |
} | |
func get(from url: URL) { | |
session.dataTask(with: URLRequest(url: url)).resume() | |
} | |
} | |
// MARK: - Test Target | |
final class HTTPClientTests { | |
func test_initHTTPClient_sessionNotNil() { | |
let sut = makeSUT() | |
XCTAssertNotNil(sut.session) | |
} | |
func makeSUT() -> HTTPClient { | |
return HTTPClient(session: URLSessionSpy()) | |
} | |
} | |
final class URLSessionSpy: HTTPSession { | |
func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> HTTPSessionTask { | |
completionHandler(nil, nil, nil) | |
return SampleURLSessionDataTask() | |
} | |
private class SampleURLSessionDataTask: HTTPSessionTask { | |
func resume() { | |
print("resume called") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment