Created
March 5, 2017 22:14
-
-
Save ragdroid/d04525c55c23b51570d87821d31e48c4 to your computer and use it in GitHub Desktop.
MockWebServer Dispatcher to Map the server requests to their json files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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