下面列出了怎么用com.squareup.okhttp.mockwebserver.Dispatcher的API类实例代码及写法,或者点击链接到github查看源代码。
@Before public void setUp() throws Exception {
initMocks(this);
setDefaultConfiguration();
when(context.getApplicationContext()).thenReturn(RuntimeEnvironment.application);
final String successJson =
IOUtils.toString(getClass().getResourceAsStream("/test.json"), "UTF-8");
server.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().startsWith("/success")) {
return new MockResponse().setResponseCode(200).setBody(successJson);
} else {
return new MockResponse().setResponseCode(400);
}
}
});
}
protected void setCustomAnswer(boolean enableBranches, boolean enableContributors) {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/repos.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches") && enableBranches) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/branches.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors") && enableContributors) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/contributors.json"));
}
return new MockResponse().setResponseCode(404);
}
});
}
public ApiInterface getApiInterface(MockWebServer mockWebServer) throws IOException {
mockWebServer.start();
TestUtils testUtils = new TestUtils();
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/repos.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/branches.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/contributors.json"));
}
return new MockResponse().setResponseCode(404);
}
};
mockWebServer.setDispatcher(dispatcher);
HttpUrl baseUrl = mockWebServer.url("/");
return ApiModule.getApiInterface(baseUrl.toString());
}
protected void setErrorAnswerWebServer() {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setResponseCode(500);
}
});
}
@Before
public void setUp() throws Exception {
super.setUp();
server = new MockWebServer();
server.start();
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/repos.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/branches.json"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/contributors.json"));
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
HttpUrl baseUrl = server.url("/");
apiInterface = ApiModule.getApiInterface(baseUrl.toString());
}
private static Dispatcher getMockwebserverDispatcherInstance() {
return new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request)
throws InterruptedException {
try {
if (request.getPath().equals("/api/apps/test")) {
return new MockResponse().setResponseCode(200).setBody(
TestUtils.getContentFromFile("testOkHttp.json", this));
} else if (request.getPath().equals("/api/apps/error")) {
return new MockResponse().setStatus("HTTP/1.1 500 KO")
.setBody(TestUtils.getContentFromFile("testErrorHttp.json", this));
} else if (request.getPath().equals("/api/apps/errorBusiness")) {
return new MockResponse().setResponseCode(200).setBody(
TestUtils.getContentFromFile("testErrorBusiness.json", this));
} else if (request.getPath().equals("/api/apps/bad")) {
return new MockResponse().setResponseCode(ERROR_RESPONSE_CODE).setBody(
TestUtils.getContentFromFile("testBadHttp.json", this));
} else if (request.getPath().equals("/api/builds/1/token")) {
return new MockResponse().setResponseCode(200).setBody(
TestUtils.getContentFromFile("testOkBuildToken.json", this));
} else if (request.getPath().equals("/download/1/manifest/2")) {
return new MockResponse().setResponseCode(200).setBody(
TestUtils.getContentFromFile("testOkBuild.json", this));
}else{
return null;
}
} catch (Exception e) {
e.printStackTrace();
return new MockResponse().setResponseCode(ERROR_RESPONSE_CODE).setBody("");
}
}
};
}
@Test
public void testNativeCSRResponseLogImpresionsClicksProperly() {
final HashMap<String, Boolean> logs = new HashMap<>();
logs.put("impression", false);
logs.put("click", false);
logs.put("request_url", false);
logs.put("response_url", false);
HttpUrl impression = server.url("/impression");
HttpUrl click = server.url("/click");
final HttpUrl request_url = server.url("/request_url");
final HttpUrl response_url = server.url("/response_url");
final MockResponse impbusResponse = new MockResponse().setResponseCode(200).setBody(TestResponsesUT.csrNativeSuccesfulWithMockTrackers(impression.toString(), click.toString(), request_url.toString(), response_url.toString()));
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
String path = request.getPath();
if ("/".equals(path)) {
return impbusResponse;
} else if ("/impression".equals(path)) {
logs.put("impresssion", true);
return new MockResponse().setResponseCode(200);
} else if ("/click".equals(path)) {
logs.put("click", true);
return new MockResponse().setResponseCode(200);
} else if ("/request_url".equals(path)) {
logs.put("request_url", true);
return new MockResponse().setResponseCode(200);
} else if (path != null && path.startsWith("/response_url")) {
logs.put("response_url", true);
return new MockResponse().setResponseCode(200);
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
HttpUrl impbus = server.url("/");
UTConstants.REQUEST_BASE_URL_UT = impbus.toString();
NativeAdRequestListener listener = new NativeAdRequestListener() {
@Override
public void onAdLoaded(NativeAdResponse response) {
nativeAdResponse = response;
((MockFBNativeBannerAdResponse) response).logImpression();
((MockFBNativeBannerAdResponse) response).clickAd();
}
@Override
public void onAdFailed(ResultCode errorcode, ANAdResponseInfo adResponseInfo) {
}
};
adRequest.setListener(listener);
adRequest.loadAd();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertNotNull(nativeAdResponse);
assertTrue(nativeAdResponse instanceof MockFBNativeBannerAdResponse);
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
waitForTasks();
Robolectric.flushBackgroundThreadScheduler();
Robolectric.flushForegroundThreadScheduler();
assertTrue(logs.get("impresssion"));
assertTrue(logs.get("click"));
assertTrue(logs.get("response_url"));
}