 
     func withHeader(_ key: String, _ value: String?) -> Self
         {
-        stub.responseHeaders[key] = value
+        var response = stub.responseData as! HTTPResponse
+        response.headers[key] = value
+        stub.responseData = response
         return self
         }
 

        }
    }

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


    func withBody(_ data: Data?) -> Self
        {
        var response = stub.responseData as! HTTPResponse
        response.body = data
        stub.responseData = response
        return self
        }

