Search in sources :

Example 71 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class ConnectionCoalescingTest method worksWithNetworkInterceptors.

/**
 * Network interceptors check for changes to target.
 */
@Test
public void worksWithNetworkInterceptors() throws Exception {
    client = client.newBuilder().addNetworkInterceptor(chain -> chain.proceed(chain.request())).build();
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    assert200Http2Response(execute(url), server.getHostName());
    HttpUrl sanUrl = url.newBuilder().host("san.com").build();
    assert200Http2Response(execute(sanUrl), "san.com");
    assertThat(client.connectionPool().connectionCount()).isEqualTo(1);
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 72 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class ConnectionCoalescingTest method alternativeThenCommon.

/**
 * Test connecting to an alternative host then common name, although only subject alternative
 * names are used if present no special consideration of common name.
 */
@Test
public void alternativeThenCommon() throws Exception {
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    HttpUrl sanUrl = url.newBuilder().host("san.com").build();
    assert200Http2Response(execute(sanUrl), "san.com");
    assert200Http2Response(execute(url), server.getHostName());
    assertThat(client.connectionPool().connectionCount()).isEqualTo(1);
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 73 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class ConnectionCoalescingTest method skipsWhenDnsDontMatch.

/**
 * If the existing connection matches a SAN but not a match for DNS then skip.
 */
@Test
public void skipsWhenDnsDontMatch() throws Exception {
    server.enqueue(new MockResponse());
    assert200Http2Response(execute(url), server.getHostName());
    HttpUrl differentDnsUrl = url.newBuilder().host("differentdns.com").build();
    try {
        execute(differentDnsUrl);
        fail("expected a failed attempt to connect");
    } catch (IOException expected) {
    }
}
Also used : MockResponse(mockwebserver3.MockResponse) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 74 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method bodyGetNoBody.

private void bodyGetNoBody(int code) throws IOException {
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 " + code + " No Content"));
    Response response = client.newCall(request().build()).execute();
    response.body().close();
    applicationLogs.assertLogEqual("--> GET " + url).assertLogEqual("--> END GET").assertLogMatch("<-- " + code + " No Content " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP (0-byte body)").assertNoMoreLogs();
    networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END GET").assertLogMatch("<-- " + code + " No Content " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP (0-byte body)").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse)

Example 75 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method headersResponseBody.

@Test
public void headersResponseBody() throws IOException {
    setLevel(Level.HEADERS);
    server.enqueue(new MockResponse().setBody("Hello!").setHeader("Content-Type", PLAIN));
    Response response = client.newCall(request().build()).execute();
    response.body().close();
    applicationLogs.assertLogEqual("--> GET " + url).assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 6").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
    networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 6").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) 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