Skip to content

Commit 5978c71

Browse files
committed
Try a different conditional compilation for MockURLProtocol
1 parent 953beb7 commit 5978c71

File tree

1 file changed

+70
-69
lines changed

1 file changed

+70
-69
lines changed
Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,92 @@
1-
#if !os(Linux)
2-
import Foundation
1+
import Foundation
32

4-
final class MockURLProtocol: URLProtocol, @unchecked Sendable {
5-
static let requestHandlerStorage = RequestHandlerStorage()
3+
#if canImport(FoundationNetworking)
4+
import FoundationNetworking
5+
#endif
66

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()
149

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)
1715
}
16+
}
1817

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+
}
2221

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+
}
2625

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)
3739
}
3840
}
39-
40-
override func stopLoading() {}
4141
}
4242

43-
actor RequestHandlerStorage {
44-
private var requestHandler:
45-
(@Sendable (URLRequest) async throws -> (HTTPURLResponse, Data))?
43+
override func stopLoading() {}
44+
}
4645

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))?
5248

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+
}
5654

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+
])
6666
}
67+
return try await handler(request)
6768
}
69+
}
6870

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+
}
7476

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() }
7880

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() }
8284

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)
8989
}
90+
return data
9091
}
91-
#endif // !os(Linux)
92+
}

0 commit comments

Comments
 (0)