Search code examples
c#.netflurl

How to simulate longer response time (not timeout) using HttpTest (flurl)


I want to configure HttpTest to simulate slow response tests; like wait X seconds before responding with configured RespondWith* response. Any suggestions?


Solution

  • This isn't directly supported. Unit tests should be fast (SimulateTimeout throws a FlurlHttpTimeoutException immediately, rather than actually pausing), but I guess I could see this being useful in some kind of integration test.

    There is one way you could do this. There's a low-level overload of RespondWith that takes a builder func. You could add a pause here, although it doesn't have async support so I'd suggest Thread.Sleep over Task.Delay. (Not ideal, but honestly it wasn't designed with this use case in mind.)

    Something like this in your setup should do it:

    httpTest.RespondWith(() => {
        // pause for 1 second
        Thread.Sleep(1000);
        return new StringContent("ok");
    });