下面列出了com.squareup.okhttp.mockwebserver.MockWebServer#setDispatcher ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
public void run() throws IOException {
MockWebServer server = new MockWebServer();
server.useHttps(sslContext.getSocketFactory(), false);
server.setDispatcher(this);
InetAddress address = HttpUtil.getHostAddress();
server.start(address,port);
System.out.println(String.format("Started server for: http://%s:%d/", address.getHostAddress(), port));
}
@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());
}
public static AppliveryTestApi getApiTestInstance(){
MockWebServer server = new MockWebServer();
server.setDispatcher(getMockwebserverDispatcherInstance());
return initializeApiclient(server);
}
public static AppliveryApiService getApiServiceInstance(){
MockWebServer server = new MockWebServer();
server.setDispatcher(getMockwebserverDispatcherInstance());
return initializeApiServiceClient(server);
}
private void runMockServer() throws IOException {
server = new MockWebServer();
server.setDispatcher(dispatcher);
server.start(0);
}
private void runMockServer() throws IOException {
server = new MockWebServer();
server.setDispatcher(dispatcher);
server.start(0);
}
@BeforeClass
public static void setup() throws IOException {
server = new MockWebServer();
dispatcher = new CustomDispatcher();
server.setDispatcher(dispatcher);
}