Search in sources :

Example 11 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class InterceptorTest method multipleInterceptors.

private void multipleInterceptors(boolean network) throws Exception {
    server.enqueue(new MockResponse());
    addInterceptor(network, chain -> {
        Request originalRequest = chain.request();
        Response originalResponse = chain.proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", // 1. Added first.
        "Android").build());
        return originalResponse.newBuilder().addHeader("Response-Interceptor", // 4. Added last.
        "Donut").build();
    });
    addInterceptor(network, chain -> {
        Request originalRequest = chain.request();
        Response originalResponse = chain.proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", // 2. Added second.
        "Bob").build());
        return originalResponse.newBuilder().addHeader("Response-Interceptor", // 3. Added third.
        "Cupcake").build();
    });
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    assertThat(response.headers("Response-Interceptor")).containsExactly("Cupcake", "Donut");
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getHeaders().values("Request-Interceptor")).containsExactly("Android", "Bob");
}
Also used : MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest)

Example 12 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class InterceptorTest method networkInterceptorsCanChangeRequestMethodFromGetToPost.

@Test
public void networkInterceptorsCanChangeRequestMethodFromGetToPost() throws Exception {
    server.enqueue(new MockResponse());
    Interceptor interceptor = chain -> {
        Request originalRequest = chain.request();
        MediaType mediaType = MediaType.get("text/plain");
        RequestBody body = RequestBody.create("abc", mediaType);
        return chain.proceed(originalRequest.newBuilder().method("POST", body).header("Content-Type", mediaType.toString()).header("Content-Length", Long.toString(body.contentLength())).build());
    };
    client = client.newBuilder().addNetworkInterceptor(interceptor).build();
    Request request = new Request.Builder().url(server.url("/")).get().build();
    client.newCall(request).execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod()).isEqualTo("POST");
    assertThat(recordedRequest.getBody().readUtf8()).isEqualTo("abc");
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Okio(okio.Okio) BeforeEach(org.junit.jupiter.api.BeforeEach) Socket(java.net.Socket) Source(okio.Source) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) Sink(okio.Sink) MockWebServer(mockwebserver3.MockWebServer) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) SocketPolicy(mockwebserver3.SocketPolicy) BufferedSink(okio.BufferedSink) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Locale(java.util.Locale) SocketTimeoutException(java.net.SocketTimeoutException) Duration(java.time.Duration) ForwardingSource(okio.ForwardingSource) Tag(org.junit.jupiter.api.Tag) Buffer(okio.Buffer) SynchronousQueue(java.util.concurrent.SynchronousQueue) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Unit(kotlin.Unit) GzipSink(okio.GzipSink) ForwardingSink(okio.ForwardingSink) MockResponse(mockwebserver3.MockResponse) TestUtil.assertSuppressed(okhttp3.TestUtil.assertSuppressed) RecordedRequest(mockwebserver3.RecordedRequest) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 13 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class InterceptorTest method rewriteRequestToServer.

private void rewriteRequestToServer(boolean network) throws Exception {
    server.enqueue(new MockResponse());
    addInterceptor(network, chain -> {
        Request originalRequest = chain.request();
        return chain.proceed(originalRequest.newBuilder().method("POST", uppercase(originalRequest.body())).addHeader("OkHttp-Intercepted", "yep").build());
    });
    Request request = new Request.Builder().url(server.url("/")).addHeader("Original-Header", "foo").method("PUT", RequestBody.create("abc", MediaType.get("text/plain"))).build();
    client.newCall(request).execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getBody().readUtf8()).isEqualTo("ABC");
    assertThat(recordedRequest.getHeader("Original-Header")).isEqualTo("foo");
    assertThat(recordedRequest.getHeader("OkHttp-Intercepted")).isEqualTo("yep");
    assertThat(recordedRequest.getMethod()).isEqualTo("POST");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest)

Example 14 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CookiesTest method testSendingCookiesFromStore.

@Test
public void testSendingCookiesFromStore() throws Exception {
    server.enqueue(new MockResponse());
    HttpUrl serverUrl = urlWithIpAddress(server, "/");
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    HttpCookie cookieA = new HttpCookie("a", "android");
    cookieA.setDomain(serverUrl.host());
    cookieA.setPath("/");
    cookieManager.getCookieStore().add(serverUrl.uri(), cookieA);
    HttpCookie cookieB = new HttpCookie("b", "banana");
    cookieB.setDomain(serverUrl.host());
    cookieB.setPath("/");
    cookieManager.getCookieStore().add(serverUrl.uri(), cookieB);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    get(serverUrl);
    RecordedRequest request = server.takeRequest();
    assertThat(request.getHeader("Cookie")).isEqualTo("a=android; b=banana");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

Example 15 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CookiesTest method testRedirectsDoNotIncludeTooManyCookies.

@Test
public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
    MockWebServer redirectTarget = new MockWebServer();
    redirectTarget.enqueue(new MockResponse().setBody("A"));
    redirectTarget.start();
    HttpUrl redirectTargetUrl = urlWithIpAddress(redirectTarget, "/");
    MockWebServer redirectSource = new MockWebServer();
    redirectSource.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + redirectTargetUrl));
    redirectSource.start();
    HttpUrl redirectSourceUrl = urlWithIpAddress(redirectSource, "/");
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    HttpCookie cookie = new HttpCookie("c", "cookie");
    cookie.setDomain(redirectSourceUrl.host());
    cookie.setPath("/");
    String portList = Integer.toString(redirectSource.getPort());
    cookie.setPortlist(portList);
    cookieManager.getCookieStore().add(redirectSourceUrl.uri(), cookie);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    get(redirectSourceUrl);
    RecordedRequest request = redirectSource.takeRequest();
    assertThat(request.getHeader("Cookie")).isEqualTo("c=cookie");
    for (String header : redirectTarget.takeRequest().getHeaders().names()) {
        if (header.startsWith("Cookie")) {
            fail(header);
        }
    }
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) MockWebServer(mockwebserver3.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.jupiter.api.Test)

Aggregations

RecordedRequest (mockwebserver3.RecordedRequest)30 MockResponse (mockwebserver3.MockResponse)26 Test (org.junit.jupiter.api.Test)25 CookieManager (java.net.CookieManager)5 InetAddress (java.net.InetAddress)5 URI (java.net.URI)3 Arrays.asList (java.util.Arrays.asList)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)3 MockWebServer (mockwebserver3.MockWebServer)3 HttpCookie (java.net.HttpCookie)2 Dispatcher (mockwebserver3.Dispatcher)2 Cache (okhttp3.Cache)2 OkHttpClient (okhttp3.OkHttpClient)2 WebSocket (okhttp3.WebSocket)2 Buffer (okio.Buffer)2 File (java.io.File)1 IOException (java.io.IOException)1 CookieHandler (java.net.CookieHandler)1