-
Notifications
You must be signed in to change notification settings - Fork 2
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 #60 from 417-72KI/stub-request-wo-ohhttpstubs
Enable test without `OHHTTPStubs`
- Loading branch information
Showing
9 changed files
with
131 additions
and
15 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
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,38 @@ | ||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
final class StubURLProtocol: URLProtocol { | ||
typealias RequestHandler = (URLRequest) throws -> (Data?, HTTPURLResponse) | ||
|
||
static var requestHandler: RequestHandler? | ||
|
||
override class func canInit(with request: URLRequest) -> Bool { | ||
true | ||
} | ||
|
||
override class func canonicalRequest(for request: URLRequest) -> URLRequest { | ||
request | ||
} | ||
|
||
override func startLoading() { | ||
guard let requestHandler = Self.requestHandler else { return } | ||
do { | ||
let (data, response) = try requestHandler(request) | ||
client?.urlProtocol(self, | ||
didReceive: response, | ||
cacheStoragePolicy: .notAllowed) | ||
if let data = data { | ||
client?.urlProtocol(self, didLoad: data) | ||
} | ||
client?.urlProtocolDidFinishLoading(self) | ||
} catch { | ||
client?.urlProtocol(self, didFailWithError: error) | ||
} | ||
} | ||
|
||
override func stopLoading() { | ||
// no-op | ||
} | ||
} |
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
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
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