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

reaz
- 23
- 3
-
clarification - I am writing unit tests using flurl - HttpTest. I see examples of how to setup mock responses; and time out. I want to simulate a slow response, like I still like to get 200 after waiting 2 seconds or so. – reaz Aug 28 '20 at 16:44
1 Answers
2
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");
});

Todd Menier
- 37,557
- 17
- 150
- 173
-
1thx. this shall work; and your comment about some special integration type scenario is exactly what it is going to be used for – reaz Sep 07 '20 at 13:12
-
my apology - seems like I spoke too early; I don't see any overload of `RespondWith` that takes a `Func<>`. I am using flurl.http\2.4.2. What am I missing? – reaz Sep 09 '20 at 19:56
-
Sorry missed your last comment. It's in 3.0 which is in prerelease but stable. Specific enhancement mentioned [here](https://github.com/tmenier/Flurl/issues/482#issuecomment-564363041). – Todd Menier Sep 30 '20 at 14:46