|
1 |
| -#if !os(Linux) |
2 |
| - import Foundation |
| 1 | +import Foundation |
3 | 2 |
|
4 |
| - final class MockURLProtocol: URLProtocol, @unchecked Sendable { |
5 |
| - static let requestHandlerStorage = RequestHandlerStorage() |
| 3 | +#if canImport(FoundationNetworking) |
| 4 | + import FoundationNetworking |
| 5 | +#endif |
6 | 6 |
|
7 |
| - static func setHandler( |
8 |
| - _ handler: @Sendable @escaping (URLRequest) async throws -> (HTTPURLResponse, Data) |
9 |
| - ) async { |
10 |
| - await requestHandlerStorage.setHandler { request in |
11 |
| - try await handler(request) |
12 |
| - } |
13 |
| - } |
| 7 | +final class MockURLProtocol: URLProtocol, @unchecked Sendable { |
| 8 | + static let requestHandlerStorage = RequestHandlerStorage() |
14 | 9 |
|
15 |
| - func executeHandler(for request: URLRequest) async throws -> (HTTPURLResponse, Data) { |
16 |
| - return try await Self.requestHandlerStorage.executeHandler(for: request) |
| 10 | + static func setHandler( |
| 11 | + _ handler: @Sendable @escaping (URLRequest) async throws -> (HTTPURLResponse, Data) |
| 12 | + ) async { |
| 13 | + await requestHandlerStorage.setHandler { request in |
| 14 | + try await handler(request) |
17 | 15 | }
|
| 16 | + } |
18 | 17 |
|
19 |
| - override class func canInit(with request: URLRequest) -> Bool { |
20 |
| - return true |
21 |
| - } |
| 18 | + func executeHandler(for request: URLRequest) async throws -> (HTTPURLResponse, Data) { |
| 19 | + return try await Self.requestHandlerStorage.executeHandler(for: request) |
| 20 | + } |
22 | 21 |
|
23 |
| - override class func canonicalRequest(for request: URLRequest) -> URLRequest { |
24 |
| - return request |
25 |
| - } |
| 22 | + override class func canInit(with request: URLRequest) -> Bool { |
| 23 | + return true |
| 24 | + } |
26 | 25 |
|
27 |
| - override func startLoading() { |
28 |
| - Task { |
29 |
| - do { |
30 |
| - let (response, data) = try await self.executeHandler(for: request) |
31 |
| - client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) |
32 |
| - client?.urlProtocol(self, didLoad: data) |
33 |
| - client?.urlProtocolDidFinishLoading(self) |
34 |
| - } catch { |
35 |
| - client?.urlProtocol(self, didFailWithError: error) |
36 |
| - } |
| 26 | + override class func canonicalRequest(for request: URLRequest) -> URLRequest { |
| 27 | + return request |
| 28 | + } |
| 29 | + |
| 30 | + override func startLoading() { |
| 31 | + Task { |
| 32 | + do { |
| 33 | + let (response, data) = try await self.executeHandler(for: request) |
| 34 | + client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) |
| 35 | + client?.urlProtocol(self, didLoad: data) |
| 36 | + client?.urlProtocolDidFinishLoading(self) |
| 37 | + } catch { |
| 38 | + client?.urlProtocol(self, didFailWithError: error) |
37 | 39 | }
|
38 | 40 | }
|
39 |
| - |
40 |
| - override func stopLoading() {} |
41 | 41 | }
|
42 | 42 |
|
43 |
| - actor RequestHandlerStorage { |
44 |
| - private var requestHandler: |
45 |
| - (@Sendable (URLRequest) async throws -> (HTTPURLResponse, Data))? |
| 43 | + override func stopLoading() {} |
| 44 | +} |
46 | 45 |
|
47 |
| - func setHandler( |
48 |
| - _ handler: @Sendable @escaping (URLRequest) async throws -> (HTTPURLResponse, Data) |
49 |
| - ) async { |
50 |
| - requestHandler = handler |
51 |
| - } |
| 46 | +actor RequestHandlerStorage { |
| 47 | + private var requestHandler: (@Sendable (URLRequest) async throws -> (HTTPURLResponse, Data))? |
52 | 48 |
|
53 |
| - func clearHandler() async { |
54 |
| - requestHandler = nil |
55 |
| - } |
| 49 | + func setHandler( |
| 50 | + _ handler: @Sendable @escaping (URLRequest) async throws -> (HTTPURLResponse, Data) |
| 51 | + ) async { |
| 52 | + requestHandler = handler |
| 53 | + } |
56 | 54 |
|
57 |
| - func executeHandler(for request: URLRequest) async throws -> (HTTPURLResponse, Data) { |
58 |
| - guard let handler = requestHandler else { |
59 |
| - throw NSError( |
60 |
| - domain: "MockURLProtocolError", code: 0, |
61 |
| - userInfo: [ |
62 |
| - NSLocalizedDescriptionKey: "No request handler set" |
63 |
| - ]) |
64 |
| - } |
65 |
| - return try await handler(request) |
| 55 | + func clearHandler() async { |
| 56 | + requestHandler = nil |
| 57 | + } |
| 58 | + |
| 59 | + func executeHandler(for request: URLRequest) async throws -> (HTTPURLResponse, Data) { |
| 60 | + guard let handler = requestHandler else { |
| 61 | + throw NSError( |
| 62 | + domain: "MockURLProtocolError", code: 0, |
| 63 | + userInfo: [ |
| 64 | + NSLocalizedDescriptionKey: "No request handler set" |
| 65 | + ]) |
66 | 66 | }
|
| 67 | + return try await handler(request) |
67 | 68 | }
|
| 69 | +} |
68 | 70 |
|
69 |
| - extension URLRequest { |
70 |
| - func readBody() -> Data? { |
71 |
| - if let httpBodyData = self.httpBody { |
72 |
| - return httpBodyData |
73 |
| - } |
| 71 | +extension URLRequest { |
| 72 | + func readBody() -> Data? { |
| 73 | + if let httpBodyData = self.httpBody { |
| 74 | + return httpBodyData |
| 75 | + } |
74 | 76 |
|
75 |
| - guard let bodyStream = self.httpBodyStream else { return nil } |
76 |
| - bodyStream.open() |
77 |
| - defer { bodyStream.close() } |
| 77 | + guard let bodyStream = self.httpBodyStream else { return nil } |
| 78 | + bodyStream.open() |
| 79 | + defer { bodyStream.close() } |
78 | 80 |
|
79 |
| - let bufferSize: Int = 4096 |
80 |
| - let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize) |
81 |
| - defer { buffer.deallocate() } |
| 81 | + let bufferSize: Int = 4096 |
| 82 | + let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize) |
| 83 | + defer { buffer.deallocate() } |
82 | 84 |
|
83 |
| - var data = Data() |
84 |
| - while bodyStream.hasBytesAvailable { |
85 |
| - let bytesRead = bodyStream.read(buffer, maxLength: bufferSize) |
86 |
| - data.append(buffer, count: bytesRead) |
87 |
| - } |
88 |
| - return data |
| 85 | + var data = Data() |
| 86 | + while bodyStream.hasBytesAvailable { |
| 87 | + let bytesRead = bodyStream.read(buffer, maxLength: bufferSize) |
| 88 | + data.append(buffer, count: bytesRead) |
89 | 89 | }
|
| 90 | + return data |
90 | 91 | }
|
91 |
| -#endif // !os(Linux) |
| 92 | +} |
0 commit comments