Search in sources :

Example 81 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method basicPost.

@Test
public void basicPost() throws IOException {
    setLevel(Level.BASIC);
    server.enqueue(new MockResponse());
    client.newCall(request().post(RequestBody.create("Hi?", PLAIN)).build()).execute();
    applicationLogs.assertLogEqual("--> POST " + url + " (3-byte body)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)").assertNoMoreLogs();
    networkLogs.assertLogEqual("--> POST " + url + " http/1.1 (3-byte body)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)").assertNoMoreLogs();
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 82 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method http2.

@Test
public void http2() throws Exception {
    platform.assumeNotBouncyCastle();
    server.useHttps(handshakeCertificates.sslSocketFactory(), false);
    url = server.url("/");
    setLevel(Level.BASIC);
    server.enqueue(new MockResponse());
    Response response = client.newCall(request().build()).execute();
    assumeTrue(response.protocol().equals(Protocol.HTTP_2));
    applicationLogs.assertLogEqual("--> GET " + url).assertLogMatch("<-- 200 " + url + " \\(\\d+ms, 0-byte body\\)").assertNoMoreLogs();
    networkLogs.assertLogEqual("--> GET " + url + " h2").assertLogMatch("<-- 200 " + url + " \\(\\d+ms, 0-byte body\\)").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 83 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method basicChunkedResponseBody.

@Test
public void basicChunkedResponseBody() throws IOException {
    setLevel(Level.BASIC);
    server.enqueue(new MockResponse().setChunkedBody("Hello!", 2).setHeader("Content-Type", PLAIN));
    Response response = client.newCall(request().build()).execute();
    response.body().close();
    applicationLogs.assertLogEqual("--> GET " + url).assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)").assertNoMoreLogs();
    networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 84 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method bodyResponseUnknownEncoded.

@Test
public void bodyResponseUnknownEncoded() throws IOException {
    setLevel(Level.BODY);
    server.enqueue(new MockResponse().setHeader("Content-Encoding", "br").setHeader("Content-Type", PLAIN).setBody(new Buffer().write(ByteString.decodeBase64("iwmASGVsbG8sIEhlbGxvLCBIZWxsbwoD"))));
    Response response = client.newCall(request().build()).execute();
    response.body().close();
    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-Encoding: br").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogMatch("Content-Length: \\d+").assertLogEqual("<-- END HTTP (encoded body omitted)").assertNoMoreLogs();
    applicationLogs.assertLogEqual("--> GET " + url).assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Encoding: br").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogMatch("Content-Length: \\d+").assertLogEqual("<-- END HTTP (encoded body omitted)").assertNoMoreLogs();
}
Also used : Buffer(okio.Buffer) Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 85 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CertificatePinnerChainValidationTest method pinIntermediatePresentInChain.

/**
 * The pinner should accept an intermediate from the server's chain.
 */
@Test
public void pinIntermediatePresentInChain() throws Exception {
    // Fails on 11.0.1 https://github.com/square/okhttp/issues/4703
    HeldCertificate rootCa = new HeldCertificate.Builder().serialNumber(1L).certificateAuthority(1).commonName("root").build();
    HeldCertificate intermediateCa = new HeldCertificate.Builder().signedBy(rootCa).certificateAuthority(0).serialNumber(2L).commonName("intermediate_ca").build();
    HeldCertificate certificate = new HeldCertificate.Builder().signedBy(intermediateCa).serialNumber(3L).commonName(server.getHostName()).build();
    CertificatePinner certificatePinner = new CertificatePinner.Builder().add(server.getHostName(), CertificatePinner.pin(intermediateCa.certificate())).build();
    HandshakeCertificates handshakeCertificates = new HandshakeCertificates.Builder().addTrustedCertificate(rootCa.certificate()).build();
    OkHttpClient client = clientTestRule.newClientBuilder().sslSocketFactory(handshakeCertificates.sslSocketFactory(), handshakeCertificates.trustManager()).hostnameVerifier(new RecordingHostnameVerifier()).certificatePinner(certificatePinner).build();
    HandshakeCertificates serverHandshakeCertificates = new HandshakeCertificates.Builder().heldCertificate(certificate, intermediateCa.certificate()).build();
    server.useHttps(serverHandshakeCertificates.sslSocketFactory(), false);
    // The request should complete successfully.
    server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
    Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response1 = call1.execute();
    assertThat(response1.body().string()).isEqualTo("abc");
    response1.close();
    // Force a fresh connection for the next request.
    client.connectionPool().evictAll();
    // Confirm that a second request also succeeds. This should detect caching problems.
    server.enqueue(new MockResponse().setBody("def").setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
    Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response2 = call2.execute();
    assertThat(response2.body().string()).isEqualTo("def");
    response2.close();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) HandshakeCertificates(okhttp3.tls.HandshakeCertificates) CertificatePinner(okhttp3.CertificatePinner) HeldCertificate(okhttp3.tls.HeldCertificate) Request(okhttp3.Request) RecordingHostnameVerifier(okhttp3.RecordingHostnameVerifier) 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