Skip to content

Commit d46abe2

Browse files
committed
Conditionalize MockURLProtocol declaration
1 parent fad8a42 commit d46abe2

File tree

1 file changed

+69
-66
lines changed

1 file changed

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

3-
final class MockURLProtocol: URLProtocol, @unchecked Sendable {
4-
static let requestHandlerStorage = RequestHandlerStorage()
4+
final class MockURLProtocol: URLProtocol, @unchecked Sendable {
5+
static let requestHandlerStorage = RequestHandlerStorage()
56

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+
}
1113
}
12-
}
1314

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

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

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

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+
}
3537
}
3638
}
37-
}
3839

39-
override func stopLoading() {}
40-
}
40+
override func stopLoading() {}
41+
}
4142

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

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

51-
func clearHandler() async {
52-
requestHandler = nil
53-
}
53+
func clearHandler() async {
54+
requestHandler = nil
55+
}
5456

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)
6266
}
63-
return try await handler(request)
6467
}
65-
}
6668

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

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

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

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
8589
}
86-
return data
8790
}
88-
}
91+
#endif // !os(Linux)

0 commit comments

Comments
 (0)