@@ -22,11 +22,6 @@ class MockURLProtocol: URLProtocol, @unchecked Sendable {
22
22
AsyncLineSequence < URL . AsyncBytes > ?
23
23
) ) ?
24
24
25
- nonisolated ( unsafe) static var dataRequestHandler : ( ( URLRequest ) throws -> (
26
- URLResponse ,
27
- Data ?
28
- ) ) ?
29
-
30
25
override class func canInit( with request: URLRequest ) -> Bool {
31
26
#if os(watchOS)
32
27
print ( " MockURLProtocol cannot be used on watchOS. " )
@@ -43,40 +38,31 @@ class MockURLProtocol: URLProtocol, @unchecked Sendable {
43
38
fatalError ( " `client` is nil. " )
44
39
}
45
40
46
- if let requestHandler = MockURLProtocol . requestHandler {
47
- Task {
48
- let ( response, stream) = try requestHandler ( self . request)
49
- client. urlProtocol ( self , didReceive: response, cacheStoragePolicy: . notAllowed)
50
- if let stream = stream {
51
- do {
52
- for try await line in stream {
53
- guard let data = line. data ( using: . utf8) else {
54
- fatalError ( " Failed to convert \" \( line) \" to UTF8 data. " )
55
- }
56
- client. urlProtocol ( self , didLoad: data)
57
- // Add a newline character since AsyncLineSequence strips them when reading line by
58
- // line;
59
- // without the following, the whole file is delivered as a single line.
60
- client. urlProtocol ( self , didLoad: " \n " . data ( using: . utf8) !)
41
+ guard let requestHandler = MockURLProtocol . requestHandler else {
42
+ fatalError ( " No request handler set. " )
43
+ }
44
+
45
+ Task {
46
+ let ( response, stream) = try requestHandler ( self . request)
47
+ client. urlProtocol ( self , didReceive: response, cacheStoragePolicy: . notAllowed)
48
+ if let stream = stream {
49
+ do {
50
+ for try await line in stream {
51
+ guard let data = line. data ( using: . utf8) else {
52
+ fatalError ( " Failed to convert \" \( line) \" to UTF8 data. " )
61
53
}
62
- } catch {
63
- client. urlProtocol ( self , didFailWithError: error)
64
- XCTFail ( " Unexpected failure reading lines from stream: \( error. localizedDescription) " )
54
+ client. urlProtocol ( self , didLoad: data)
55
+ // Add a newline character since AsyncLineSequence strips them when reading line by
56
+ // line;
57
+ // without the following, the whole file is delivered as a single line.
58
+ client. urlProtocol ( self , didLoad: " \n " . data ( using: . utf8) !)
65
59
}
60
+ } catch {
61
+ client. urlProtocol ( self , didFailWithError: error)
62
+ XCTFail ( " Unexpected failure reading lines from stream: \( error. localizedDescription) " )
66
63
}
67
- client. urlProtocolDidFinishLoading ( self )
68
- }
69
- } else if let dataRequestHandler = MockURLProtocol . dataRequestHandler {
70
- Task {
71
- let ( response, data) = try dataRequestHandler ( self . request)
72
- client. urlProtocol ( self , didReceive: response, cacheStoragePolicy: . notAllowed)
73
- if let data = data {
74
- client. urlProtocol ( self , didLoad: data)
75
- }
76
- client. urlProtocolDidFinishLoading ( self )
77
64
}
78
- } else {
79
- fatalError ( " No request handler set. " )
65
+ client. urlProtocolDidFinishLoading ( self )
80
66
}
81
67
}
82
68
0 commit comments