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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations