本文整理汇总了Java中com.squareup.okhttp.mockwebserver.Dispatcher类的典型用法代码示例。如果您正苦于以下问题:Java Dispatcher类的具体用法?Java Dispatcher怎么用?Java Dispatcher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Dispatcher类属于com.squareup.okhttp.mockwebserver包,在下文中一共展示了Dispatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMockwebserverDispatcherInstance
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
private Dispatcher getMockwebserverDispatcherInstance() {
return new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request)
throws InterruptedException {
try {
if (request.getPath().equals("/ok/")) {
return new MockResponse().setResponseCode(200).setBody(
TestUtils.getContentFromFile("testOkHttp.json", this));
} else if (request.getPath().equals("/error/")) {
return new MockResponse().setStatus("HTTP/1.1 500 KO")
.setBody(TestUtils.getContentFromFile("testErrorHttp.json", this));
} else {
return new MockResponse().setResponseCode(ERROR_RESPONSE_CODE).setBody(
TestUtils.getContentFromFile("testBadHttp.json", this));
}
} catch (Exception e) {
e.printStackTrace();
return new MockResponse().setResponseCode(ERROR_RESPONSE_CODE).setBody("");
}
}
};
}
示例2: setUp
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
@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);
}
}
});
}
示例3: setCustomAnswer
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
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);
}
});
}
示例4: getApiInterface
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
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());
}
示例5: setUp
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
@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("/account/info")) {
return new MockResponse().setResponseCode(200)
.setBody(jsonReader.readString("json/user.json"));
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
HttpUrl baseUrl = server.url("/");
service = ServiceCreator.createTestService(baseUrl.toString(),
UserService.class);
}
示例6: startServer
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
private MockWebServer startServer() throws IOException {
Logger.getLogger(MockWebServer.class.getName()).setLevel(Level.WARNING);
MockWebServer server = new MockWebServer();
if (tls) {
SSLContext sslContext = SslContextBuilder.localhost();
server.useHttps(sslContext.getSocketFactory(), false);
server.setNpnEnabled(true);
server.setNpnProtocols(protocols);
}
final MockResponse response = newResponse();
server.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) {
return response;
}
});
server.play();
return server;
}
示例7: configureServer
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
@Before
public void configureServer() {
mockServer.setDispatcher(
new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
assertThat(request.getHeader(INJECTED_HEADER_NAME)).isEqualTo(HEADER_VALUE);
switch (request.getPath()) {
case "/simple":
return new MockResponse()
.setResponseCode(Response.Status.NOT_FOUND.getStatusCode())
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
.setHeader(HEADER_NAME, HEADER_VALUE)
.setBody("Not found");
case "/writerInterceptor":
assertThat(request.getBody().readByteArray()).isEqualTo(PAYLOAD);
// fall-through
case "/requestHeader":
assertThat(request.getHeader(HEADER_NAME)).isEqualTo(HEADER_VALUE);
return new MockResponse()
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
.setBody(request.getHeader(HEADER_NAME));
}
throw new AssertionError("Unexpected request: " + request);
}
});
}
示例8: setErrorAnswerWebServer
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
protected void setErrorAnswerWebServer() {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setResponseCode(500);
}
});
}
示例9: setUp
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
@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());
}
示例10: getMockwebserverDispatcherInstance
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
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("");
}
}
};
}
示例11: init
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
public void init() {
new Thread(new Runnable() {
public void run() {
mockWebServer.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request)
throws InterruptedException {
if (request.getPath().equals("/1/")) {
Thread.sleep(2000);
return new MockResponse().setResponseCode(OK_STATUS).setBody("{\"value\":\"1\"}");
} else if (request.getPath().equals("/2/")) {
Thread.sleep(3000);
return new MockResponse().setResponseCode(OK_STATUS).setBody("{\"value\":\"2\"}");
} else {
Thread.sleep(4000);
return new MockResponse().setResponseCode(OK_STATUS).setBody("{\"value\":\"3\"}");
}
}
});
try {
mockWebServer.start();
} catch (Exception e) {
Log.e(LOGTAG, "ERROR ", e);
}
}
}, "serverThread").start();
}
示例12: setUp
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
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"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/branches"));
} else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors")) {
return new MockResponse().setResponseCode(200)
.setBody(testUtils.readString("json/contributors"));
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
HttpUrl baseUrl = server.url("/");
Retrofit.Builder builder = new Retrofit.Builder().
baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create());
apiInterface = builder.build().create(ApiInterface.class);
}
示例13: canceledBeforeResponseReadIsNeverDelivered
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
@Test public void canceledBeforeResponseReadIsNeverDelivered() throws Exception {
client.getDispatcher().setMaxRequests(1); // Force requests to be executed serially.
server.setDispatcher(new Dispatcher() {
char nextResponse = 'A';
@Override public MockResponse dispatch(RecordedRequest request) {
client.cancel("request A");
return new MockResponse().setBody(Character.toString(nextResponse++));
}
});
server.play();
// Canceling a request after the server has received a request but before
// it has delivered the response. That request will never be received to the
// client.
Request requestA = new Request.Builder().url(server.getUrl("/a")).tag("request A").build();
client.enqueue(requestA, receiver);
assertEquals("/a", server.takeRequest().getPath());
// We then make a second request (not canceled) to make sure the receiver
// has nothing left to wait for.
Request requestB = new Request.Builder().url(server.getUrl("/b")).tag("request B").build();
client.enqueue(requestB, receiver);
assertEquals("/b", server.takeRequest().getPath());
receiver.await(requestB.url()).assertBody("B");
// At this point we know the receiver is ready: if it hasn't received 'A'
// yet it never will.
receiver.assertNoResponse(requestA.url());
}
示例14: setUp
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
@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 {
String p = request.getPath();
MockResponse response = new MockResponse().setResponseCode(200);
if (p.matches("/board/all.*") ||
p.matches("/board/moderator/all.*")) {
return response.setBody(jsonReader.readString
("json/bulletins.json"));
} else if (p.equals("/board") ||
p.equals("/board/" + BULLETIN_ID)) {
return response.setBody(jsonReader.readString
("json/ok.json"));
} else if (p.equals("/board/" + BULLETIN_ID + "/recipient")) {
return response.setBody(jsonReader.readString
("json/bull_recipients.json"));
} else if (p.equals("/subdivision/" + SUBDIV_ID +
"/children")) {
return response.setBody(jsonReader.readString
("json/desc_subdivision.json"));
} else if (p.equals("/roles")) {
return response.setBody(jsonReader.readString("json/roles" +
".json"));
} else if (p.equals("/subdivision/" + SUBDIV_ID + "/group")) {
return response.setBody(jsonReader.readString
("json/groups.json"));
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
HttpUrl baseUrl = server.url("/");
service = ServiceCreator.createTestService(baseUrl.toString(),
BulletinService.class);
}
示例15: getDispatcher
import com.squareup.okhttp.mockwebserver.Dispatcher; //导入依赖的package包/类
public Dispatcher getDispatcher() {
return dispatcher;
}