Search in sources :

Example 26 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method retainServedDateFormat.

/**
 * For Last-Modified and Date headers, we should echo the date back in the exact format we were
 * served.
 */
@Test
public void retainServedDateFormat() throws Exception {
    // Serve a response with a non-standard date format that OkHttp supports.
    Date lastModifiedDate = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(-1));
    Date servedDate = new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(-2));
    DateFormat dateFormat = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z", Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
    String lastModifiedString = dateFormat.format(lastModifiedDate);
    String servedString = dateFormat.format(servedDate);
    // This response should be conditionally cached.
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + lastModifiedString).addHeader("Expires: " + servedString).setBody("A"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    // The first request has no conditions.
    RecordedRequest request1 = server.takeRequest();
    assertThat(request1.getHeader("If-Modified-Since")).isNull();
    // The 2nd request uses the server's date format.
    RecordedRequest request2 = server.takeRequest();
    assertThat(request2.getHeader("If-Modified-Since")).isEqualTo(lastModifiedString);
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 27 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class WebSocketHttpTest method successfulExtensions.

private void successfulExtensions(String extensionsHeader) throws Exception {
    webServer.enqueue(new MockResponse().addHeader("Sec-WebSocket-Extensions", extensionsHeader).withWebSocketUpgrade(serverListener));
    WebSocket client = newWebSocket();
    clientListener.assertOpen();
    WebSocket server = serverListener.assertOpen();
    // Server to client message big enough to be compressed.
    String message1 = TestUtil.repeat('a', (int) RealWebSocket.DEFAULT_MINIMUM_DEFLATE_SIZE);
    server.send(message1);
    clientListener.assertTextMessage(message1);
    // Client to server message big enough to be compressed.
    String message2 = TestUtil.repeat('b', (int) RealWebSocket.DEFAULT_MINIMUM_DEFLATE_SIZE);
    client.send(message2);
    serverListener.assertTextMessage(message2);
    // Empty server to client message.
    String message3 = "";
    server.send(message3);
    clientListener.assertTextMessage(message3);
    // Empty client to server message.
    String message4 = "";
    client.send(message4);
    serverListener.assertTextMessage(message4);
    // Server to client message that shares context with message1.
    String message5 = message1 + message1;
    server.send(message5);
    clientListener.assertTextMessage(message5);
    // Client to server message that shares context with message2.
    String message6 = message2 + message2;
    client.send(message6);
    serverListener.assertTextMessage(message6);
    closeWebSockets(client, server);
    RecordedRequest upgradeRequest = webServer.takeRequest();
    assertThat(upgradeRequest.getHeader("Sec-WebSocket-Extensions")).isEqualTo("permessage-deflate");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) ByteString(okio.ByteString) WebSocket(okhttp3.WebSocket)

Example 28 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CookiesTest method testCookiesSentIgnoresCase.

@Test
public void testCookiesSentIgnoresCase() throws Exception {
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(new CookieManager() {

        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) {
            Map<String, List<String>> result = new LinkedHashMap<>();
            result.put("COOKIE", Collections.singletonList("Bar=bar"));
            result.put("cooKIE2", Collections.singletonList("Baz=baz"));
            return result;
        }
    })).build();
    server.enqueue(new MockResponse());
    get(server.url("/"));
    RecordedRequest request = server.takeRequest();
    assertThat(request.getHeader("Cookie")).isEqualTo("Bar=bar; Baz=baz");
    assertThat(request.getHeader("Cookie2")).isNull();
    assertThat(request.getHeader("Quux")).isNull();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) List(java.util.List) Arrays.asList(java.util.Arrays.asList) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CookieManager(java.net.CookieManager) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 29 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CookiesTest method cookieHandlerLikeAndroid.

@Test
public void cookieHandlerLikeAndroid() throws Exception {
    server.enqueue(new MockResponse());
    final HttpUrl serverUrl = urlWithIpAddress(server, "/");
    CookieHandler androidCookieHandler = new CookieHandler() {

        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> map) {
            return Collections.singletonMap("Cookie", Collections.singletonList("$Version=\"1\"; " + "a=\"android\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\"; " + "b=\"banana\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\""));
        }

        @Override
        public void put(URI uri, Map<String, List<String>> map) throws IOException {
        }
    };
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(androidCookieHandler)).build();
    get(serverUrl);
    RecordedRequest request = server.takeRequest();
    assertThat(request.getHeader("Cookie")).isEqualTo("a=android; b=banana");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) List(java.util.List) Arrays.asList(java.util.Arrays.asList) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CookieHandler(java.net.CookieHandler) Test(org.junit.jupiter.api.Test)

Example 30 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CookiesTest method receiveAndSendMultipleCookies.

@Test
public void receiveAndSendMultipleCookies() throws Exception {
    server.enqueue(new MockResponse().addHeader("Set-Cookie", "a=android").addHeader("Set-Cookie", "b=banana"));
    server.enqueue(new MockResponse());
    CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
    get(urlWithIpAddress(server, "/"));
    RecordedRequest request1 = server.takeRequest();
    assertThat(request1.getHeader("Cookie")).isNull();
    get(urlWithIpAddress(server, "/"));
    RecordedRequest request2 = server.takeRequest();
    assertThat(request2.getHeader("Cookie")).isEqualTo("a=android; b=banana");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) 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