Skip to content

Instantly share code, notes, and snippets.

@ragdroid
Created March 5, 2017 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ragdroid/d04525c55c23b51570d87821d31e48c4 to your computer and use it in GitHub Desktop.
Save ragdroid/d04525c55c23b51570d87821d31e48c4 to your computer and use it in GitHub Desktop.
MockWebServer Dispatcher to Map the server requests to their json files
public class LocalResponseDispatcher extends QueueDispatcher {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
MockResponse mockResponse = new MockResponse();
String scenario = getScenario(request);
if (scenario != null) {
try {
mockResponse.setBody(readFile(scenario));
mockResponse.setResponseCode(200);
} catch (IOException e) {
e.printStackTrace();
}
}
return mockResponse;
}
private String getScenario(RecordedRequest request) {
String scenario = "";
String path = request.getPath();
String requestedMethod = request.getMethod().toLowerCase(Locale.US);
scenario += requestedMethod + path.replace("/", "_") + ".json";
return scenario;
}
private String readFile(String jsonFileName) throws IOException {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment