mirror of
https://github.com/square/okhttp.git
synced 2026-01-17 08:42:25 +03:00
Merge pull request #1541 from lalbuquerque/master
Adding more content and example to Dispatcher
This commit is contained in:
@@ -116,6 +116,26 @@ assertEquals("{}", request.getUtf8Body());
|
||||
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()`.
|
||||
|
||||
```java
|
||||
final Dispatcher dispatcher = new Dispatcher() {
|
||||
|
||||
@Override
|
||||
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
|
||||
|
||||
if (request.getPath().equals("/v1/login/auth/")){
|
||||
return new MockResponse().setResponseCode(200));
|
||||
} else if (request.getPath().equals("v1/check/version/")){
|
||||
return new MockResponse().setResponseCode(200).setBody("version=9");
|
||||
} else if (request.getPath().equals("/v1/profile/info")) {
|
||||
return new MockResponse().setResponseCode(200).setBody("{\\\"info\\\":{\\\"name\":\"Lucas Albuquerque\",\"age\":\"21\",\"gender\":\"male\"}}");
|
||||
}
|
||||
return new MockResponse().setResponseCode(404);
|
||||
}
|
||||
};
|
||||
server.setDispatcher(dispatcher);
|
||||
```
|
||||
|
||||
|
||||
### Download
|
||||
|
||||
Reference in New Issue
Block a user