Search in sources :

Example 76 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method headersGet.

@Test
public void headersGet() throws IOException {
    setLevel(Level.HEADERS);
    server.enqueue(new MockResponse());
    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: 0").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: 0").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 77 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method bodyPost.

@Test
public void bodyPost() throws IOException {
    setLevel(Level.BODY);
    server.enqueue(new MockResponse());
    Request request = request().post(RequestBody.create("Hi?", PLAIN)).build();
    Response response = client.newCall(request).execute();
    response.body().close();
    applicationLogs.assertLogEqual("--> POST " + url).assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("Content-Length: 3").assertLogEqual("").assertLogEqual("Hi?").assertLogEqual("--> END POST (3-byte body)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP (0-byte body)").assertNoMoreLogs();
    networkLogs.assertLogEqual("--> POST " + url + " http/1.1").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("Content-Length: 3").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("").assertLogEqual("Hi?").assertLogEqual("--> END POST (3-byte body)").assertLogMatch("<-- 200 OK " + 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) Request(okhttp3.Request) Test(org.junit.jupiter.api.Test)

Example 78 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method bodyResponseGzipEncoded.

@Test
public void bodyResponseGzipEncoded() throws IOException {
    setLevel(Level.BODY);
    server.enqueue(new MockResponse().setHeader("Content-Encoding", "gzip").setHeader("Content-Type", PLAIN).setBody(new Buffer().write(ByteString.decodeBase64("H4sIAAAAAAAAAPNIzcnJ11HwQKIAdyO+9hMAAAA="))));
    Response response = client.newCall(request().build()).execute();
    ResponseBody responseBody = response.body();
    assertThat(responseBody.string()).overridingErrorMessage("Expected response body to be valid").isEqualTo("Hello, Hello, Hello");
    responseBody.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: gzip").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogMatch("Content-Length: \\d+").assertLogEqual("").assertLogEqual("Hello, Hello, Hello").assertLogEqual("<-- END HTTP (19-byte, 29-gzipped-byte body)").assertNoMoreLogs();
    applicationLogs.assertLogEqual("--> GET " + url).assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogEqual("").assertLogEqual("Hello, Hello, Hello").assertLogEqual("<-- END HTTP (19-byte body)").assertNoMoreLogs();
}
Also used : Buffer(okio.Buffer) Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) ResponseBody(okhttp3.ResponseBody) Test(org.junit.jupiter.api.Test)

Example 79 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method headersPostNoContentType.

@Test
public void headersPostNoContentType() throws IOException {
    setLevel(Level.HEADERS);
    server.enqueue(new MockResponse());
    Request request = request().post(RequestBody.create("Hi?", null)).build();
    Response response = client.newCall(request).execute();
    response.body().close();
    applicationLogs.assertLogEqual("--> POST " + url).assertLogEqual("Content-Length: 3").assertLogEqual("--> END POST").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
    networkLogs.assertLogEqual("--> POST " + url + " http/1.1").assertLogEqual("Content-Length: 3").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END POST").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Length: 0").assertLogEqual("<-- END HTTP").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Request(okhttp3.Request) Test(org.junit.jupiter.api.Test)

Example 80 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class HttpLoggingInterceptorTest method duplexRequestsAreNotLogged.

@Test
public void duplexRequestsAreNotLogged() throws Exception {
    platform.assumeHttp2Support();
    platform.assumeNotBouncyCastle();
    // HTTP/2
    server.useHttps(handshakeCertificates.sslSocketFactory(), false);
    url = server.url("/");
    setLevel(Level.BODY);
    server.enqueue(new MockResponse().setBody("Hello response!"));
    RequestBody asyncRequestBody = new RequestBody() {

        @Override
        @Nullable
        public MediaType contentType() {
            return null;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8("Hello request!");
            sink.close();
        }

        @Override
        public boolean isDuplex() {
            return true;
        }
    };
    Request request = request().post(asyncRequestBody).build();
    Response response = client.newCall(request).execute();
    assumeTrue(response.protocol().equals(Protocol.HTTP_2));
    assertThat(response.body().string()).isEqualTo("Hello response!");
    applicationLogs.assertLogEqual("--> POST " + url).assertLogEqual("--> END POST (duplex request body omitted)").assertLogMatch("<-- 200 " + url + " \\(\\d+ms\\)").assertLogEqual("content-length: 15").assertLogEqual("").assertLogEqual("Hello response!").assertLogEqual("<-- END HTTP (15-byte body)").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) RequestBody(okhttp3.RequestBody) 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