Search in sources :

Example 61 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method getHeadersDeletesCached100LevelWarnings.

@Test
public void getHeadersDeletesCached100LevelWarnings() throws Exception {
    server.enqueue(new MockResponse().addHeader("Warning: 199 test danger").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    Response response1 = get(server.url("/"));
    assertThat(response1.body().string()).isEqualTo("A");
    assertThat(response1.header("Warning")).isEqualTo("199 test danger");
    Response response2 = get(server.url("/"));
    assertThat(response2.body().string()).isEqualTo("A");
    assertThat(response2.header("Warning")).isNull();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 62 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method previouslyNotGzippedContentIsNotModifiedAndSpecifiesGzipEncoding.

@Test
public void previouslyNotGzippedContentIsNotModifiedAndSpecifiesGzipEncoding() throws Exception {
    server.enqueue(new MockResponse().setBody("ABCABCABC").addHeader("Content-Type: text/plain").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED).addHeader("Content-Type: text/plain").addHeader("Content-Encoding: gzip"));
    server.enqueue(new MockResponse().setBody("DEFDEFDEF"));
    assertThat(get(server.url("/")).body().string()).isEqualTo("ABCABCABC");
    assertThat(get(server.url("/")).body().string()).isEqualTo("ABCABCABC");
    assertThat(get(server.url("/")).body().string()).isEqualTo("DEFDEFDEF");
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 63 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method etagAndExpirationDateInThePast.

/**
 * If both If-Modified-Since and If-None-Match conditions apply, send only If-None-Match.
 */
@Test
public void etagAndExpirationDateInThePast() throws Exception {
    String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("ETag: v1").addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
    assertThat(conditionalRequest.getHeader("If-None-Match")).isEqualTo("v1");
    assertThat(conditionalRequest.getHeader("If-Modified-Since")).isNull();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 64 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method combinedCacheHeadersCanBeNonAscii.

@Test
public void combinedCacheHeadersCanBeNonAscii() throws Exception {
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").addHeaderLenient("Alpha", "α").addHeaderLenient("β", "Beta").setBody("abcd"));
    server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none").addHeaderLenient("Gamma", "Γ").addHeaderLenient("Δ", "Delta").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    Response response1 = get(server.url("/"));
    assertThat(response1.header("Alpha")).isEqualTo("α");
    assertThat(response1.header("β")).isEqualTo("Beta");
    assertThat(response1.body().string()).isEqualTo("abcd");
    Response response2 = get(server.url("/"));
    assertThat(response2.header("Alpha")).isEqualTo("α");
    assertThat(response2.header("β")).isEqualTo("Beta");
    assertThat(response2.header("Gamma")).isEqualTo("Γ");
    assertThat(response2.header("Δ")).isEqualTo("Delta");
    assertThat(response2.body().string()).isEqualTo("abcd");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 65 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method etag.

@Test
public void etag() throws Exception {
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("ETag: v1"));
    assertThat(conditionalRequest.getHeader("If-None-Match")).isEqualTo("v1");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockResponse (mockwebserver3.MockResponse)283 Test (org.junit.jupiter.api.Test)261 RecordedRequest (mockwebserver3.RecordedRequest)79 IOException (java.io.IOException)41 Response (okhttp3.Response)39 BufferedSink (okio.BufferedSink)28 WebSocket (okhttp3.WebSocket)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 MockWebServer (mockwebserver3.MockWebServer)22 Request (okhttp3.Request)21 Buffer (okio.Buffer)21 InetAddress (java.net.InetAddress)20 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 Assertions.fail (org.junit.jupiter.api.Assertions.fail)17 BeforeEach (org.junit.jupiter.api.BeforeEach)17 Tag (org.junit.jupiter.api.Tag)17 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)17 SocketTimeoutException (java.net.SocketTimeoutException)15 Duration (java.time.Duration)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)15