Search in sources :

Example 16 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class WebSocketHttpTest method readTimeoutAppliesWithinFrames.

/**
 * There's no read timeout when reading the first byte of a new frame. But as soon as we start
 * reading a frame we enable the read timeout. In this test we have the server returning the first
 * byte of a frame but no more frames.
 */
@Test
public void readTimeoutAppliesWithinFrames() {
    webServer.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            return upgradeResponse(request).setBody(// Truncated frame.
            new Buffer().write(ByteString.decodeHex("81"))).removeHeader("Content-Length").setSocketPolicy(SocketPolicy.KEEP_OPEN);
        }
    });
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    clientListener.assertFailure(SocketTimeoutException.class, "timeout", "Read timed out");
    assertThat(webSocket.close(1000, null)).isFalse();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) Buffer(okio.Buffer) MockResponse(mockwebserver3.MockResponse) Dispatcher(mockwebserver3.Dispatcher) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 17 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method assertClientSuppliedCondition.

private RecordedRequest assertClientSuppliedCondition(MockResponse seed, String conditionName, String conditionValue) throws Exception {
    server.enqueue(seed.setBody("A"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    HttpUrl url = server.url("/");
    assertThat(get(url).body().string()).isEqualTo("A");
    Request request = new Request.Builder().url(url).header(conditionName, conditionValue).build();
    Response response = client.newCall(request).execute();
    assertThat(response.code()).isEqualTo(HttpURLConnection.HTTP_NOT_MODIFIED);
    assertThat(response.body().string()).isEqualTo("");
    // seed
    server.takeRequest();
    return server.takeRequest();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest)

Example 18 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class DnsOverHttpsTest method failure.

@Test
public void failure() throws Exception {
    server.enqueue(dnsResponse("0000818300010000000100000e7364666c6b686673646c6b6a64660265650000010001c01b00060001" + "000007070038026e7303746c64c01b0a686f73746d61737465720d6565737469696e7465726e657" + "4c01b5adb12c100000e10000003840012750000000e10"));
    try {
        dns.lookup("google.com");
        fail();
    } catch (UnknownHostException uhe) {
        uhe.printStackTrace();
        assertThat(uhe.getMessage()).isEqualTo("google.com: NXDOMAIN");
    }
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod()).isEqualTo("GET");
    assertThat(recordedRequest.getPath()).isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) UnknownHostException(java.net.UnknownHostException) Test(org.junit.jupiter.api.Test)

Example 19 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class DnsOverHttpsTest method usesCacheOnlyIfFresh.

@Test
public void usesCacheOnlyIfFresh() throws Exception {
    Cache cache = new Cache(new File("./target/DnsOverHttpsTest.cache"), 100 * 1024);
    OkHttpClient cachedClient = bootstrapClient.newBuilder().cache(cache).build();
    DnsOverHttps cachedDns = buildLocalhost(cachedClient, false);
    server.enqueue(dnsResponse("0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c00050001" + "00000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010" + "0010000003b00049df00112").setHeader("cache-control", "max-age=1"));
    List<InetAddress> result = cachedDns.lookup("google.com");
    assertThat(result).containsExactly(address("157.240.1.18"));
    RecordedRequest recordedRequest = server.takeRequest(0, TimeUnit.SECONDS);
    assertThat(recordedRequest.getMethod()).isEqualTo("GET");
    assertThat(recordedRequest.getPath()).isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
    Thread.sleep(2000);
    server.enqueue(dnsResponse("0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c00050001" + "00000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010" + "0010000003b00049df00112").setHeader("cache-control", "max-age=1"));
    result = cachedDns.lookup("google.com");
    assertThat(result).isEqualTo(singletonList(address("157.240.1.18")));
    recordedRequest = server.takeRequest(0, TimeUnit.SECONDS);
    assertThat(recordedRequest.getMethod()).isEqualTo("GET");
    assertThat(recordedRequest.getPath()).isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) OkHttpClient(okhttp3.OkHttpClient) File(java.io.File) InetAddress(java.net.InetAddress) Cache(okhttp3.Cache) Test(org.junit.jupiter.api.Test)

Example 20 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class DnsOverHttpsTest method getOne.

@Test
public void getOne() throws Exception {
    server.enqueue(dnsResponse("0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c00050001" + "00000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010" + "0010000003b00049df00112"));
    List<InetAddress> result = dns.lookup("google.com");
    assertThat(result).isEqualTo(singletonList(address("157.240.1.18")));
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod()).isEqualTo("GET");
    assertThat(recordedRequest.getPath()).isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) InetAddress(java.net.InetAddress) 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