Simulating response timeouts with ASP.NET Web API
<p>Suppose I have a client that continually requests streams from a service, and I want automate testing it. So, as part of the test, I create a service that returns a stream. The following code snippet constructs the response and returns it:</p>

<pre><code>HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(fstream);
response.Content.Headers.ContentType = mediaType;
return response;
</code></pre>

<p>This works for the success case where the client calls the API and gets a response in a timely manner. But I also want to simulate some timeout failures.</p>

<p>If I want to simulate timeouts before any part of the response is returned, i can simply add a <em>Thread.Sleep()</em> before return <em>response</em>.</p>

<p>My question is: <strong>how can I simulate the timeout case where the service has already started return <em>response</em>?</strong> I would like to simulate the service timing out after the response headers have been sent, but before the entirety of <em>fstream</em> is sent.</p>