本文整理汇总了Java中okhttp3.mockwebserver.Dispatcher类的典型用法代码示例。如果您正苦于以下问题:Java Dispatcher类的具体用法?Java Dispatcher怎么用?Java Dispatcher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Dispatcher类属于okhttp3.mockwebserver包,在下文中一共展示了Dispatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: canceledBeforeResponseReadSignalsOnFailure
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
@Test public void canceledBeforeResponseReadSignalsOnFailure() throws Exception {
Request requestA = new Request.Builder().url(server.url("/a")).build();
final Call call = client.newCall(requestA);
server.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) {
call.cancel();
return new MockResponse().setBody("A");
}
});
call.enqueue(callback);
assertEquals("/a", server.takeRequest().getPath());
callback.await(requestA.url()).assertFailure("Canceled", "stream was reset: CANCEL",
"Socket closed");
}
示例2: readTimeoutAppliesWithinFrames
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
/**
* There's no read timeout when reading the first byte of a new frame. But as soon as we start
* reading a frame we enable the read timeout. In this test we have the server returning the first
* byte of a frame but no more frames.
*/
@Test public void readTimeoutAppliesWithinFrames() throws IOException {
webServer.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return upgradeResponse(request)
.setBody(new Buffer().write(ByteString.decodeHex("81"))) // Truncated frame.
.removeHeader("Content-Length")
.setSocketPolicy(SocketPolicy.KEEP_OPEN);
}
});
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
clientListener.assertFailure(SocketTimeoutException.class, "timeout");
assertFalse(webSocket.close(1000, null));
}
示例3: startServer
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
private MockWebServer startServer() throws IOException {
Logger.getLogger(MockWebServer.class.getName()).setLevel(Level.WARNING);
MockWebServer server = new MockWebServer();
if (tls) {
SslClient sslClient = SslClient.localhost();
server.useHttps(sslClient.socketFactory, false);
server.setProtocols(protocols);
}
final MockResponse response = newResponse();
server.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) {
return response;
}
});
server.start();
return server;
}
示例4: readTimeoutAppliesWithinFrames
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
/**
* There's no read timeout when reading the first byte of a new frame. But as soon as we start
* reading a frame we enable the read timeout. In this test we have the server returning the first
* byte of a frame but no more frames.
*/
@Test public void readTimeoutAppliesWithinFrames() throws IOException {
webServer.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return upgradeResponse(request)
.setBody(new Buffer().write(ByteString.decodeHex("81"))) // Truncated frame.
.removeHeader("Content-Length")
.setSocketPolicy(SocketPolicy.KEEP_OPEN);
}
});
WebSocket webSocket = newWebSocket();
clientListener.assertOpen();
clientListener.assertFailure(SocketTimeoutException.class, "timeout", "Read timed out");
assertFalse(webSocket.close(1000, null));
}
示例5: canceledBeforeIOSignalsOnFailure
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
/**
* This test puts a request in front of one that is to be canceled, so that it is canceled before
* I/O takes place.
*/
@Test public void canceledBeforeIOSignalsOnFailure() throws Exception {
client.dispatcher().setMaxRequests(1); // Force requests to be executed serially.
Request requestA = new Request.Builder().url(server.url("/a")).build();
Request requestB = new Request.Builder().url(server.url("/b")).build();
final Call callA = client.newCall(requestA);
final Call callB = client.newCall(requestB);
server.setDispatcher(new Dispatcher() {
char nextResponse = 'A';
@Override public MockResponse dispatch(RecordedRequest request) {
callB.cancel();
return new MockResponse().setBody(Character.toString(nextResponse++));
}
});
callA.enqueue(callback);
callB.enqueue(callback);
assertEquals("/a", server.takeRequest().getPath());
callback.await(requestA.url()).assertBody("A");
// At this point we know the callback is ready, and that it will receive a cancel failure.
callback.await(requestB.url()).assertFailure("Canceled");
}
示例6: startServer
import okhttp3.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.setProtocols(protocols);
}
final MockResponse response = newResponse();
server.setDispatcher(new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) {
return response;
}
});
server.start();
return server;
}
示例7: testCompletes1
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
/**
* This test show how to create CallAdapter.Factory without adding a reaction for errors.
* <br>
* Test created by Robert Zagorski on 19.10.2016
*/
@Test
public void testCompletes1() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return MockWebServerUtils.getSuccessfulResponse();
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.setLoggingEnabled(true)
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestObserver testObserver = observable.test();
testObserver.awaitTerminalEvent();
testObserver.assertComplete();
}
示例8: testCompletes2
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
/**
* Test shows, that when the server responds with different error
* {@link retrofit2.HttpException} than
* {@link com.rzagorski.retrofitrx2errorhandler.backoff.BackoffStrategy backoff strategies} react to
* ({@link java.net.SocketTimeoutException}) the execution ends immediately.
* <br>
* Test created by Robert Zagorski on 19.10.2016
*/
@Test
public void testCompletes2() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
switch (mockWebServer.getRequestCount()) {
case 1:
return new MockResponse().setResponseCode(500);
case 2:
return new MockResponse().setResponseCode(404);
case 3:
return MockWebServerUtils.getSuccessfulResponse();
default:
return new MockResponse().setResponseCode(500);
}
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Exponential.init()
.addThrowable(SocketTimeoutException.class)
.setBase(2)
.setMaxRetries(3).build())
.addBackoffStrategy(Simple.init()
.addThrowable(SocketTimeoutException.class)
.setMaxRetries(3).build())
.setLoggingEnabled(true)
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
long startTime = System.currentTimeMillis();
Observable observable = github.repos("square");
TestObserver testObserver = observable.test();
testObserver.awaitTerminalEvent();
long endTime = System.currentTimeMillis();
System.out.println((endTime - startTime));
//four SocketTimeoutExceptions and 3 exponential backoff's
assertTrue((endTime - startTime) <= (1) * MockWebServerUtils.ONE_SEC);
}
示例9: testBackoffError1
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
/**
* Test demonstrates the simple usage of {@link com.rzagorski.retrofitrx2errorhandler.backoff.strategies.Simple}
* backoff strategy. The server does not responds (every time {@link java.net.SocketTimeoutException}
* occurrs). The backoff strategy is executed immediately after each error, so there should be
* 4 times 10 second timeout.
* <br>
* Test created by Robert Zagorski on 19.10.2016
*/
@Test
public void testBackoffError1() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE);
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Simple.init()
.addThrowable(SocketTimeoutException.class)
.setMaxRetries(3).build())
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
long startTime = System.currentTimeMillis();
Observable observable = github.repos("square");
TestObserver testObserver = observable.test();
testObserver.awaitTerminalEvent();
long endTime = System.currentTimeMillis();
System.out.println((endTime - startTime));
//four SocketTimeoutExceptions
assertTrue((endTime - startTime) >= 4 * 10 * MockWebServerUtils.ONE_SEC);
}
示例10: testBackoffCompletes1
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
/**
* Test demonstrates the usage of {@link com.rzagorski.retrofitrx2errorhandler.backoff.strategies.Simple}
* backoff strategy. The server responds with 500 server error every time. The backoff strategy
* is executed and after {@link com.rzagorski.retrofitrx2errorhandler.backoff.strategies.Simple.Builder#setMaxRetries(int)}
* is run out, the appropriate error is passed to client.
* 4 times 10 second timeout.
* <br>
* Test created by Robert Zagorski on 19.10.2016
*/
@Test
public void testBackoffCompletes1() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setResponseCode(500);
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Simple.init()
.addHttpCode(500)
.setMaxRetries(300).build())
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestObserver testObserver = observable.test();
testObserver.awaitTerminalEvent();
testObserver.assertError(HttpException.class);
}
示例11: testBackoffCompletes2
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
/**
* The same as {@link #testBackoffCompletes1()} but with different assertion.
* Test created by Robert Zagorski on 19.10.2016
*/
@Test
public void testBackoffCompletes2() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setResponseCode(500);
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Simple.init()
.addHttpCode(500)
.setMaxRetries(300).build())
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestObserver testObserver = observable.test();
testObserver.awaitTerminalEvent();
assertEquals(1, testObserver.errorCount());
}
示例12: testBackoffOneTime1
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
@Test
public void testBackoffOneTime1() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (mockWebServer.getRequestCount() == 1) {
return new MockResponse().setResponseCode(500);
} else if (mockWebServer.getRequestCount() == 2) {
return MockWebServerUtils.getSuccessfulResponse();
}
return new MockResponse().setResponseCode(404);
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Exponential.init()
.addThrowable(HttpException.class)
.setMaxRetries(3).build())
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestObserver testObserver = observable.test();
testObserver.awaitTerminalEvent();
assertEquals(1, testObserver.valueCount());
}
示例13: testBackoffTwoTimes
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
@Test
public void testBackoffTwoTimes() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (mockWebServer.getRequestCount() < 3) {
return new MockResponse().setResponseCode(500);
} else if (mockWebServer.getRequestCount() == 3) {
return MockWebServerUtils.getSuccessfulResponse();
}
return new MockResponse().setResponseCode(404);
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Exponential.init()
.addThrowable(HttpException.class)
.setMaxRetries(3).build())
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestObserver testObserver = observable.test();
testObserver.awaitTerminalEvent();
assertEquals(1, testObserver.valueCount());
}
示例14: testBackoffCompletes
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
@Test
public void testBackoffCompletes() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return MockWebServerUtils.getSuccessfulResponse();
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Exponential.init()
.addThrowable(HttpException.class)
.setMaxRetries(3).build())
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestObserver testSubscriber = observable.test();
testSubscriber.awaitTerminalEvent();
testSubscriber.assertComplete();
}
示例15: testBackoffError1
import okhttp3.mockwebserver.Dispatcher; //导入依赖的package包/类
@Test
public void testBackoffError1() throws Exception {
mockWebServer.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setResponseCode(500);
}
});
RxCallAdapter rxCallAdapter = new RxCallAdapter.Builder()
.addBackoffStrategy(Exponential.init()
.addThrowable(HttpException.class)
.setMaxRetries(3).build())
.setLoggingEnabled(true)
.build();
GitHub github = createRetrofitInstance(mockWebServer.url("/").toString(),
new RxErrorHandingFactory(rxCallAdapter));
Observable observable = github.repos("square");
TestObserver testObserver = observable.test();
testObserver.awaitTerminalEvent();
assertEquals(1, testObserver.errorCount());
}