I'm trying to test HTTP timeout scenarios using a MockWebServer which answers my test requests sent with Retrofit/OkHttp. (This question was asked before some years ago, but at the time concerned a bug in the MockWebServer. Also, the API has since changed a fair bit, so I think reposting this question is warranted.)
There seem to be several related methods, but I'd appreciate a solution to this issue with a clear example: I'm unsure about the difference between..
.delayBody
and.throttleBody
Also, both of these methods seem to only delay/throttle the body - is it not possible to set a timeout for the response header? I.e. something along the lines of "wait X seconds after the next incoming request before you send out any response".
By default MockWebServer uses a queue to specify a series of responses. Use a Dispatcher to handle requests using another policy. One natural policy is to dispatch on the request path. You can, for example, filter the request instead of using server.
enqueue(MockResponse response) Scripts response to be returned to a request made in sequence. Dispatcher. getDispatcher() Returns the dispatcher used to respond to HTTP requests.
Try setting the response’s socket policy to NO_RESPONSE.
The setSocketPolicy()
method is deprecated.
MockResponse mockResponse = new MockResponse().setBodyDelay(10, TimeUnit.SECONDS);
This will delay the MockWebServer response by 10 seconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With