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