1
0
mirror of https://github.com/square/okhttp.git synced 2025-11-27 18:21:14 +03:00

Support 204 and 205 'No Content' replies when application interceptor.

This commit is contained in:
Jake Wharton
2015-11-24 17:05:46 -05:00
parent 57d7367bba
commit b93104c9e6
2 changed files with 335 additions and 140 deletions

View File

@@ -27,6 +27,7 @@ import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import com.squareup.okhttp.internal.Platform;
import com.squareup.okhttp.internal.http.HttpEngine;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
@@ -194,7 +195,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
}
String endMessage = "<-- END HTTP";
if (logBody) {
if (logBody && HttpEngine.hasBody(response)) {
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();

View File

@@ -41,23 +41,32 @@ public final class HttpLoggingInterceptorTest {
@Rule public final MockWebServer server = new MockWebServer();
private final OkHttpClient client = new OkHttpClient();
private final List<String> logs = new ArrayList<>();
private HttpLoggingInterceptor interceptor;
private String host;
private final LogRecorder networkLogs = new LogRecorder();
private final HttpLoggingInterceptor networkInterceptor =
new HttpLoggingInterceptor(networkLogs);
private final LogRecorder applicationLogs = new LogRecorder();
private final HttpLoggingInterceptor applicationInterceptor =
new HttpLoggingInterceptor(applicationLogs);
private void setLevel(Level level) {
networkInterceptor.setLevel(level);
applicationInterceptor.setLevel(level);
}
@Before public void setUp() {
HttpLoggingInterceptor.Logger logger = new HttpLoggingInterceptor.Logger() {
@Override public void log(String message) {
logs.add(message);
}
};
interceptor = new HttpLoggingInterceptor(logger);
client.networkInterceptors().add(interceptor);
client.networkInterceptors().add(networkInterceptor);
client.interceptors().add(applicationInterceptor);
client.setConnectionPool(null);
host = server.getHostName() + ":" + server.getPort();
}
@Test public void setLevelShouldPreventNullValue() {
try {
interceptor.setLevel(null);
applicationInterceptor.setLevel(null);
fail();
} catch (NullPointerException expected) {
assertEquals("level == null. Use Level.NONE instead.", expected.getMessage());
@@ -66,218 +75,403 @@ public final class HttpLoggingInterceptorTest {
@Test public void setLevelShouldReturnSameInstanceOfInterceptor() {
for (Level level : Level.values()) {
assertSame(interceptor, interceptor.setLevel(level));
assertSame(applicationInterceptor, applicationInterceptor.setLevel(level));
}
}
@Test public void none() throws IOException {
server.enqueue(new MockResponse());
client.newCall(request().build()).execute();
assertTrue(logs.isEmpty());
applicationLogs.assertNoMoreLogs();
networkLogs.assertNoMoreLogs();
}
@Test public void basicGet() throws IOException {
interceptor.setLevel(Level.BASIC);
setLevel(Level.BASIC);
server.enqueue(new MockResponse());
client.newCall(request().build()).execute();
assertEquals(2, logs.size());
assertEquals("--> GET / HTTP/1.1", logs.get(0));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 0-byte body\\)", logs.get(1)));
applicationLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 0-byte body\\)")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 0-byte body\\)")
.assertNoMoreLogs();
}
@Test public void basicPost() throws IOException {
interceptor.setLevel(Level.BASIC);
setLevel(Level.BASIC);
server.enqueue(new MockResponse());
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
assertEquals(2, logs.size());
assertEquals("--> POST / HTTP/1.1 (3-byte body)", logs.get(0));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 0-byte body\\)", logs.get(1)));
applicationLogs
.assertLogEqual("--> POST / HTTP/1.1 (3-byte body)")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 0-byte body\\)")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> POST / HTTP/1.1 (3-byte body)")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 0-byte body\\)")
.assertNoMoreLogs();
}
@Test public void basicResponseBody() throws IOException {
interceptor.setLevel(Level.BASIC);
setLevel(Level.BASIC);
server.enqueue(new MockResponse()
.setBody("Hello!")
.setHeader("Content-Type", PLAIN.toString()));
.setHeader("Content-Type", PLAIN));
client.newCall(request().build()).execute();
assertEquals(2, logs.size());
assertEquals("--> GET / HTTP/1.1", logs.get(0));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 6-byte body\\)", logs.get(1)));
applicationLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 6-byte body\\)")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 6-byte body\\)")
.assertNoMoreLogs();
}
@Test public void headersGet() throws IOException {
interceptor.setLevel(Level.HEADERS);
setLevel(Level.HEADERS);
server.enqueue(new MockResponse());
client.newCall(request().build()).execute();
assertEquals(12, logs.size());
assertEquals("--> GET / HTTP/1.1", logs.get(0));
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
assertEquals("Connection: Keep-Alive", logs.get(2));
assertEquals("Accept-Encoding: gzip", logs.get(3));
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
assertEquals("--> END GET", logs.get(5));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
assertEquals("Content-Length: 0", logs.get(7));
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(8));
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(9)));
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(10)));
assertEquals("<-- END HTTP", logs.get(11));
applicationLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip")
.assertLogMatch("User-Agent: okhttp/.+")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP")
.assertNoMoreLogs();
}
@Test public void headersPost() throws IOException {
interceptor.setLevel(Level.HEADERS);
setLevel(Level.HEADERS);
server.enqueue(new MockResponse());
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
assertEquals(14, logs.size());
assertEquals("--> POST / HTTP/1.1", logs.get(0));
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(1));
assertEquals("Content-Length: 3", logs.get(2));
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(3));
assertEquals("Connection: Keep-Alive", logs.get(4));
assertEquals("Accept-Encoding: gzip", logs.get(5));
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(6)));
assertEquals("--> END POST", logs.get(7));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(8)));
assertEquals("Content-Length: 0", logs.get(9));
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(10));
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(11)));
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(12)));
assertEquals("<-- END HTTP", logs.get(13));
applicationLogs
.assertLogEqual("--> POST / HTTP/1.1")
// TODO force content-type and content-length to show here
.assertLogEqual("--> END POST")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> POST / 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("--> END POST")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP")
.assertNoMoreLogs();
}
@Test public void headersResponseBody() throws IOException {
interceptor.setLevel(Level.HEADERS);
setLevel(Level.HEADERS);
server.enqueue(new MockResponse()
.setBody("Hello!")
.setHeader("Content-Type", PLAIN.toString()));
.setHeader("Content-Type", PLAIN));
client.newCall(request().build()).execute();
assertEquals(13, logs.size());
assertEquals("--> GET / HTTP/1.1", logs.get(0));
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
assertEquals("Connection: Keep-Alive", logs.get(2));
assertEquals("Accept-Encoding: gzip", logs.get(3));
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
assertEquals("--> END GET", logs.get(5));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
assertEquals("Content-Length: 6", logs.get(7));
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(8));
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(9));
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(10)));
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(11)));
assertEquals("<-- END HTTP", logs.get(12));
applicationLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 6")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip")
.assertLogMatch("User-Agent: okhttp/.+")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 6")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP")
.assertNoMoreLogs();
}
@Test public void bodyGet() throws IOException {
interceptor.setLevel(Level.BODY);
setLevel(Level.BODY);
server.enqueue(new MockResponse());
client.newCall(request().build()).execute();
assertEquals(12, logs.size());
assertEquals("--> GET / HTTP/1.1", logs.get(0));
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
assertEquals("Connection: Keep-Alive", logs.get(2));
assertEquals("Accept-Encoding: gzip", logs.get(3));
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
assertEquals("--> END GET", logs.get(5));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
assertEquals("Content-Length: 0", logs.get(7));
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(8));
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(9)));
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(10)));
assertEquals("<-- END HTTP (0-byte body)", logs.get(11));
applicationLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP (0-byte body)")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip")
.assertLogMatch("User-Agent: okhttp/.+")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP (0-byte body)")
.assertNoMoreLogs();
}
@Test public void bodyGet204() throws IOException {
setLevel(Level.BODY);
bodyGetNoBody(204);
}
@Test public void bodyGet205() throws IOException {
setLevel(Level.BODY);
bodyGetNoBody(205);
}
private void bodyGetNoBody(int code) throws IOException {
server.enqueue(new MockResponse()
.setStatus("HTTP/1.1 " + code + " No Content"));
client.newCall(request().build()).execute();
applicationLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 " + code + " No Content \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP (0-byte body)")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip")
.assertLogMatch("User-Agent: okhttp/.+")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 " + code + " No Content \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP (0-byte body)")
.assertNoMoreLogs();
}
@Test public void bodyPost() throws IOException {
interceptor.setLevel(Level.BODY);
setLevel(Level.BODY);
server.enqueue(new MockResponse());
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
assertEquals(16, logs.size());
assertEquals("--> POST / HTTP/1.1", logs.get(0));
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(1));
assertEquals("Content-Length: 3", logs.get(2));
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(3));
assertEquals("Connection: Keep-Alive", logs.get(4));
assertEquals("Accept-Encoding: gzip", logs.get(5));
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(6)));
assertEquals("", logs.get(7));
assertEquals("Hi?", logs.get(8));
assertEquals("--> END POST (3-byte body)", logs.get(9));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(10)));
assertEquals("Content-Length: 0", logs.get(11));
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(12));
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(13)));
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(14)));
assertEquals("<-- END HTTP (0-byte body)", logs.get(15));
applicationLogs
.assertLogEqual("--> POST / HTTP/1.1")
// TODO force content-type and content-length to show here
.assertLogEqual("")
.assertLogEqual("Hi?")
.assertLogEqual("--> END POST (3-byte body)")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP (0-byte body)")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> POST / 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("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 0")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("<-- END HTTP (0-byte body)")
.assertNoMoreLogs();
}
@Test public void bodyResponseBody() throws IOException {
interceptor.setLevel(Level.BODY);
setLevel(Level.BODY);
server.enqueue(new MockResponse()
.setBody("Hello!")
.setHeader("Content-Type", PLAIN.toString()));
.setHeader("Content-Type", PLAIN));
client.newCall(request().build()).execute();
assertEquals(15, logs.size());
assertEquals("--> GET / HTTP/1.1", logs.get(0));
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
assertEquals("Connection: Keep-Alive", logs.get(2));
assertEquals("Accept-Encoding: gzip", logs.get(3));
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
assertEquals("--> END GET", logs.get(5));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
assertEquals("Content-Length: 6", logs.get(7));
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(8));
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(9));
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(10)));
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(11)));
assertEquals("", logs.get(12));
assertEquals("Hello!", logs.get(13));
assertEquals("<-- END HTTP (6-byte body)", logs.get(14));
applicationLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 6")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("")
.assertLogEqual("Hello!")
.assertLogEqual("<-- END HTTP (6-byte body)")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip")
.assertLogMatch("User-Agent: okhttp/.+")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Content-Length: 6")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("")
.assertLogEqual("Hello!")
.assertLogEqual("<-- END HTTP (6-byte body)")
.assertNoMoreLogs();
}
@Test public void bodyResponseBodyChunked() throws IOException {
interceptor.setLevel(Level.BODY);
setLevel(Level.BODY);
server.enqueue(new MockResponse()
.setChunkedBody("Hello!", 2)
.setHeader("Content-Type", PLAIN.toString()));
.setHeader("Content-Type", PLAIN));
client.newCall(request().build()).execute();
assertEquals(15, logs.size());
assertEquals("--> GET / HTTP/1.1", logs.get(0));
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
assertEquals("Connection: Keep-Alive", logs.get(2));
assertEquals("Accept-Encoding: gzip", logs.get(3));
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
assertEquals("--> END GET", logs.get(5));
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
assertEquals("Transfer-encoding: chunked", logs.get(7));
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(8));
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(9));
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(10)));
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(11)));
assertEquals("", logs.get(12));
assertEquals("Hello!", logs.get(13));
assertEquals("<-- END HTTP (6-byte body)", logs.get(14));
applicationLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Transfer-encoding: chunked")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("")
.assertLogEqual("Hello!")
.assertLogEqual("<-- END HTTP (6-byte body)")
.assertNoMoreLogs();
networkLogs
.assertLogEqual("--> GET / HTTP/1.1")
.assertLogEqual("Host: " + host)
.assertLogEqual("Connection: Keep-Alive")
.assertLogEqual("Accept-Encoding: gzip")
.assertLogMatch("User-Agent: okhttp/.+")
.assertLogEqual("--> END GET")
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
.assertLogEqual("Transfer-encoding: chunked")
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
.assertLogMatch("OkHttp-Received-Millis: \\d+")
.assertLogEqual("")
.assertLogEqual("Hello!")
.assertLogEqual("<-- END HTTP (6-byte body)")
.assertNoMoreLogs();
}
private Request.Builder request() {
return new Request.Builder().url(server.url("/"));
}
private static class LogRecorder implements HttpLoggingInterceptor.Logger {
private final List<String> logs = new ArrayList<>();
private int index;
LogRecorder assertLogEqual(String expected) {
assertTrue("No more messages found", index < logs.size());
String actual = logs.get(index++);
assertEquals(expected, actual);
return this;
}
LogRecorder assertLogMatch(String pattern) {
assertTrue("No more messages found", index < logs.size());
String actual = logs.get(index++);
assertTrue("<" + actual + "> did not match pattern <" + pattern + ">",
Pattern.matches(pattern, actual));
return this;
}
void assertNoMoreLogs() {
assertTrue("More messages remain: " + logs.subList(index, logs.size()), index == logs.size());
}
@Override public void log(String message) {
logs.add(message);
}
}
}