Search in sources :

Example 51 with Dispatcher

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();
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) Dispatcher(okhttp3.mockwebserver.Dispatcher) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 52 with Dispatcher

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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) InetSocketAddress(java.net.InetSocketAddress) Dispatcher(okhttp3.mockwebserver.Dispatcher) URL(java.net.URL) ClientBuilder(com.cloudant.client.api.ClientBuilder) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 53 with Dispatcher

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();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache)

Example 54 with Dispatcher

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher) Before(org.junit.Before)

Example 55 with Dispatcher

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]));
}
Also used : ConnectionPool(okhttp3.ConnectionPool) Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) Dispatcher(okhttp3.Dispatcher)

Aggregations

Dispatcher (okhttp3.Dispatcher)40 OkHttpClient (okhttp3.OkHttpClient)34 MockResponse (okhttp3.mockwebserver.MockResponse)27 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 Dispatcher (okhttp3.mockwebserver.Dispatcher)24 IOException (java.io.IOException)17 Before (org.junit.Before)14 Test (org.junit.Test)13 Call (okhttp3.Call)11 MockWebServer (okhttp3.mockwebserver.MockWebServer)11 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)10 ArrayList (java.util.ArrayList)8 Request (okhttp3.Request)8 Response (okhttp3.Response)8 ResponseBody (okhttp3.ResponseBody)7 Gson (com.google.gson.Gson)5 Interceptor (okhttp3.Interceptor)5 CloudantClient (com.cloudant.client.api.CloudantClient)4 RefineTest (com.google.refine.RefineTest)4 DataExtensionConfig (com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig)4