Search in sources :

Example 86 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class HttpOverHttp2Test method settingsLimitsMaxConcurrentStreams.

/**
   * Push a setting that permits up to 2 concurrent streams, then make 3 concurrent requests and
   * confirm that the third concurrent request prepared a new connection.
   */
@Test
public void settingsLimitsMaxConcurrentStreams() throws Exception {
    Settings settings = new Settings();
    settings.set(Settings.MAX_CONCURRENT_STREAMS, 2);
    // Read & write a full request to confirm settings are accepted.
    server.enqueue(new MockResponse().withSettings(settings));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("", response.body().string());
    server.enqueue(new MockResponse().setBody("ABC"));
    server.enqueue(new MockResponse().setBody("DEF"));
    server.enqueue(new MockResponse().setBody("GHI"));
    Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response1 = call1.execute();
    Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response2 = call2.execute();
    Call call3 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response3 = call3.execute();
    assertEquals("ABC", response1.body().string());
    assertEquals("DEF", response2.body().string());
    assertEquals("GHI", response3.body().string());
    // Settings connection.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // Reuse settings connection.
    assertEquals(1, server.takeRequest().getSequenceNumber());
    // Reuse settings connection.
    assertEquals(2, server.takeRequest().getSequenceNumber());
    // New connection!
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 87 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class HttpOverHttp2Test method domainFronting.

/** https://github.com/square/okhttp/issues/3103 */
@Test
public void domainFronting() throws Exception {
    client = client.newBuilder().addNetworkInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request().newBuilder().header("Host", "privateobject.com").build();
            return chain.proceed(request);
        }
    }).build();
    server.enqueue(new MockResponse());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("", response.body().string());
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("privateobject.com", recordedRequest.getHeader(":authority"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Example 88 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class HttpOverHttp2Test method sendRequestCookies.

@Test
public void sendRequestCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    Cookie requestCookie = new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build();
    cookieJar.enqueueRequestCookies(requestCookie);
    client = client.newBuilder().cookieJar(cookieJar).build();
    server.enqueue(new MockResponse());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("", response.body().string());
    RecordedRequest request = server.takeRequest();
    assertEquals("a=b", request.getHeader("Cookie"));
}
Also used : Cookie(okhttp3.Cookie) MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecordingCookieJar(okhttp3.RecordingCookieJar) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 89 with Request

use of com.tonyodev.fetch2.Request in project realm-java by realm.

the class HttpUtils method waitAuthServerReady.

// Checking the server
private static boolean waitAuthServerReady() throws InterruptedException {
    int retryTimes = 20;
    // Dummy invalid request, which will trigger a 400 (BAD REQUEST), but indicate the auth
    // server is responsive
    Request request = new Request.Builder().url(Constants.AUTH_SERVER_URL).build();
    while (retryTimes != 0) {
        Response response = null;
        try {
            response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return true;
            }
            RealmLog.error("Error response from auth server: %s", response.toString());
        } catch (IOException e) {
            // TODO As long as the auth server hasn't started yet, OKHttp cannot parse the response
            // correctly. At this point it is unknown weather is a bug in OKHttp or an
            // unknown host is reported. This can cause a lot of "false" errors in the log.
            RealmLog.error(e);
            Thread.sleep(500);
        } finally {
            if (response != null) {
                response.close();
            }
        }
        retryTimes--;
    }
    return false;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException)

Example 90 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class OkHttpURLConnection method buildCall.

private Call buildCall() throws IOException {
    if (call != null) {
        return call;
    }
    connected = true;
    if (doOutput) {
        if (method.equals("GET")) {
            // they are requesting a stream to write to. This implies a POST method
            method = "POST";
        } else if (!HttpMethod.permitsRequestBody(method)) {
            throw new ProtocolException(method + " does not support writing");
        }
    }
    if (requestHeaders.get("User-Agent") == null) {
        requestHeaders.add("User-Agent", defaultUserAgent());
    }
    OutputStreamRequestBody requestBody = null;
    if (HttpMethod.permitsRequestBody(method)) {
        // Add a content type for the request body, if one isn't already present.
        String contentType = requestHeaders.get("Content-Type");
        if (contentType == null) {
            contentType = "application/x-www-form-urlencoded";
            requestHeaders.add("Content-Type", contentType);
        }
        boolean stream = fixedContentLength != -1L || chunkLength > 0;
        long contentLength = -1L;
        String contentLengthString = requestHeaders.get("Content-Length");
        if (fixedContentLength != -1L) {
            contentLength = fixedContentLength;
        } else if (contentLengthString != null) {
            contentLength = Long.parseLong(contentLengthString);
        }
        requestBody = stream ? new StreamedRequestBody(contentLength) : new BufferedRequestBody(contentLength);
        requestBody.timeout().timeout(client.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
    }
    Request request = new Request.Builder().url(Internal.instance.getHttpUrlChecked(getURL().toString())).headers(requestHeaders.build()).method(method, requestBody).build();
    if (urlFilter != null) {
        urlFilter.checkURLPermitted(request.url().url());
    }
    OkHttpClient.Builder clientBuilder = client.newBuilder();
    clientBuilder.interceptors().clear();
    clientBuilder.interceptors().add(UnexpectedException.INTERCEPTOR);
    clientBuilder.networkInterceptors().clear();
    clientBuilder.networkInterceptors().add(networkInterceptor);
    // Use a separate dispatcher so that limits aren't impacted. But use the same executor service!
    clientBuilder.dispatcher(new Dispatcher(client.dispatcher().executorService()));
    // If we're currently not using caches, make sure the engine's client doesn't have one.
    if (!getUseCaches()) {
        clientBuilder.cache(null);
    }
    return call = clientBuilder.build().newCall(request);
}
Also used : ProtocolException(java.net.ProtocolException) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) Dispatcher(okhttp3.Dispatcher)

Aggregations

Request (okhttp3.Request)1617 Response (okhttp3.Response)1022 IOException (java.io.IOException)525 Test (org.junit.Test)407 OkHttpClient (okhttp3.OkHttpClient)331 RequestBody (okhttp3.RequestBody)256 Call (okhttp3.Call)239 ResponseBody (okhttp3.ResponseBody)189 HttpUrl (okhttp3.HttpUrl)146 Callback (okhttp3.Callback)109 Map (java.util.Map)85 File (java.io.File)77 InputStream (java.io.InputStream)77 JSONObject (org.json.JSONObject)76 MediaType (okhttp3.MediaType)75 Buffer (okio.Buffer)73 List (java.util.List)71 Headers (okhttp3.Headers)71 FormBody (okhttp3.FormBody)64 HashMap (java.util.HashMap)63