use of okhttp3.Dispatcher in project java-cloudant by cloudant.
the class CloudantClientTests method gatewayStyleURL.
@Test
public void gatewayStyleURL() throws Exception {
final String gatewayPath = "/gateway";
// Set a dispatcher that returns 200 if the requests have the correct path /gateway/_all_dbs
// Otherwise return 400.
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals(gatewayPath + "/_all_dbs")) {
return new MockResponse();
} else {
return new MockResponse().setResponseCode(400);
}
}
});
// Build a client with a URL that includes a path
CloudantClient c = ClientBuilder.url(new URL(server.url(gatewayPath).toString())).build();
// If the request path is wrong this call will return 400 and throw an exception failing the
// test.
c.getAllDbs();
// Build a client with a URL that includes a path with a trailing /
c = ClientBuilder.url(new URL(server.url(gatewayPath + "/").toString())).build();
// If the request path is wrong this call will return 400 and throw an exception failing the
// test.
c.getAllDbs();
}
use of okhttp3.Dispatcher in project java-cloudant by cloudant.
the class HttpProxyTest method proxiedRequest.
/**
* This test validates that a request can successfully traverse a proxy to our mock server.
*/
@TestTemplate
public void proxiedRequest(final boolean okUsable, final boolean useSecureProxy, final boolean useHttpsServer, final boolean useProxyAuth) throws Exception {
// Test uses disableSSLAuthentication because the mock server doesn't have a trusted cert
// - need to disable the test for OkHttp and Java 8_252 or newer
Utils.assumeCustomSslAuthUsable(okUsable);
// mock a 200 OK
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse();
}
});
InetSocketAddress address = proxy.getListenAddress();
URL proxyUrl = new URL((useSecureProxy) ? "https" : "http", address.getHostName(), address.getPort(), "/");
ClientBuilder builder = CloudantClientHelper.newMockWebServerClientBuilder(server).proxyURL(proxyUrl);
if (useProxyAuth) {
builder.proxyUser(mockProxyUser).proxyPassword(mockProxyPass);
}
// We don't use SSL authentication for this test
CloudantClient client = builder.disableSSLAuthentication().build();
String response = client.executeRequest(Http.GET(client.getBaseUri())).responseAsString();
assertTrue(response.isEmpty(), "There should be no response body on the mock response");
// if it wasn't a 20x then an exception should have been thrown by now
RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
assertNotNull(request);
}
use of okhttp3.Dispatcher in project AndLang by wugemu.
the class HttpU method newOkHttpClient.
public OkHttpClient newOkHttpClient() {
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
if (openDns) {
return // 设置读取超时时间
new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
TimeUnit.SECONDS).addNetworkInterceptor(// 添加网络请求日志
logInterceptor).dns(new HttpDns()).build();
} else if (openLog) {
return // 设置读取超时时间
new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
TimeUnit.SECONDS).addNetworkInterceptor(// 添加网络请求日志
logInterceptor).build();
} else if (BaseLangUtil.isHaveSDPer()) {
// 开启本地存储权限后使用缓存机制
// 新建一个cache,指定目录为外部目录下的okhttp_cache目录,大小为100M
Cache cache = new Cache(PicSelUtil.getCacheDir(), 100 * 1024 * 1024);
return // 设置读取超时时间
new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
TimeUnit.SECONDS).cache(// 缓存设置
cache).addInterceptor(// 请求网络拦截
new RequestCacheI()).build();
} else {
// 新建一个cache,指定目录为外部目录下的okhttp_cache目录,大小为100M
return // 设置读取超时时间
new OkHttpClient.Builder().dispatcher(new Dispatcher(ExecutorServiceUtil.getInstence().getExecutorService())).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, // 设置写的超时时间
TimeUnit.SECONDS).connectTimeout(20, // 设置连接超时时间
TimeUnit.SECONDS).build();
}
}
use of okhttp3.Dispatcher in project apps-android-wikipedia by wikimedia.
the class MockWebServerTest method setUp.
@Before
public void setUp() throws Throwable {
OkHttpClient.Builder builder = OkHttpConnectionFactory.getClient().newBuilder();
okHttpClient = builder.dispatcher(new Dispatcher(new ImmediateExecutorService())).build();
server.setUp();
}
use of okhttp3.Dispatcher in project retrofit by square.
the class Crawler method main.
public static void main(String... args) throws Exception {
Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
dispatcher.setMaxRequests(20);
dispatcher.setMaxRequestsPerHost(1);
OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(dispatcher).connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS)).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(HttpUrl.parse("https://example.com/")).addConverterFactory(PageAdapter.FACTORY).client(okHttpClient).build();
PageService pageService = retrofit.create(PageService.class);
Crawler crawler = new Crawler(pageService);
crawler.crawlPage(HttpUrl.parse(args[0]));
}
Aggregations