         let stub = URLProtocol.property(forKey: stubPropertyKey, in: request) as! RequestStub
         stub.delayLatch.await()
 
-        let client = self.client!
-
-        if let error = stub.responseError
-            {
-            client.urlProtocol(self, didFailWithError: error)
-            return
-            }
-        else
-            {
-            Thread.sleep(forTimeInterval: 1.0 / pow(.random(in: 1...1000), 2))
-
-            let response = HTTPURLResponse(
-                url: request.url!,
-                statusCode: stub.responseCode,
-                httpVersion: "1.1",
-                headerFields: stub.responseHeaders)!
-            client.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
-            if let data = stub.responseBody
-                { client.urlProtocol(self, didLoad: data) }
-            }
-
-        client.urlProtocolDidFinishLoading(self)
+        Thread.sleep(forTimeInterval: 1.0 / pow(.random(in: 1...1000), 2))
+
+        stub.responseData.send(to: self.client!, for: self, url: request.url!)
         }
 
     override func stopLoading()

        }
    }

class RequestStub
    {
    var matcher: RequestMatcher

    init(matcher: RequestMatcher, status: Int = 200)
        {
        self.matcher = matcher
        self.responseCode = status
        }

    var responseError: Error?
    var responseCode: Int
    var responseHeaders = [String:String]()
    var responseBody: Data?

    let delayLatch = Latch(name: "delayed request")

    var description: String
        { "\(matcher) → \(responseCode)" }
    }

@discardableResult
func stubRequest(_ resource: () -> Resource, _ method: String) -> LSStubRequestDSL
    {
    let stub = RequestStub(matcher: RequestMatcher(method: method, url: resource().url.absoluteString))
    NetworkStub.add(stub)
    return LSStubRequestDSL(stub: stub)
    }

            matcher: RequestMatcher(
                method: method.rawValue.uppercased(),
                url: resource().url.absoluteString),
            responseData: HTTPResponse(statusCode: status)))
        }

    static func add(_ stub: RequestStub)
