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