mirror of
https://github.com/square/okhttp.git
synced 2025-11-29 06:23:09 +03:00
Support 204 and 205 'No Content' replies when application interceptor.
This commit is contained in:
@@ -27,6 +27,7 @@ import com.squareup.okhttp.RequestBody;
|
|||||||
import com.squareup.okhttp.Response;
|
import com.squareup.okhttp.Response;
|
||||||
import com.squareup.okhttp.ResponseBody;
|
import com.squareup.okhttp.ResponseBody;
|
||||||
import com.squareup.okhttp.internal.Platform;
|
import com.squareup.okhttp.internal.Platform;
|
||||||
|
import com.squareup.okhttp.internal.http.HttpEngine;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -194,7 +195,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String endMessage = "<-- END HTTP";
|
String endMessage = "<-- END HTTP";
|
||||||
if (logBody) {
|
if (logBody && HttpEngine.hasBody(response)) {
|
||||||
BufferedSource source = responseBody.source();
|
BufferedSource source = responseBody.source();
|
||||||
source.request(Long.MAX_VALUE); // Buffer the entire body.
|
source.request(Long.MAX_VALUE); // Buffer the entire body.
|
||||||
Buffer buffer = source.buffer();
|
Buffer buffer = source.buffer();
|
||||||
|
|||||||
@@ -41,23 +41,32 @@ public final class HttpLoggingInterceptorTest {
|
|||||||
@Rule public final MockWebServer server = new MockWebServer();
|
@Rule public final MockWebServer server = new MockWebServer();
|
||||||
|
|
||||||
private final OkHttpClient client = new OkHttpClient();
|
private final OkHttpClient client = new OkHttpClient();
|
||||||
private final List<String> logs = new ArrayList<>();
|
private String host;
|
||||||
private HttpLoggingInterceptor interceptor;
|
|
||||||
|
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() {
|
@Before public void setUp() {
|
||||||
HttpLoggingInterceptor.Logger logger = new HttpLoggingInterceptor.Logger() {
|
client.networkInterceptors().add(networkInterceptor);
|
||||||
@Override public void log(String message) {
|
client.interceptors().add(applicationInterceptor);
|
||||||
logs.add(message);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
interceptor = new HttpLoggingInterceptor(logger);
|
|
||||||
client.networkInterceptors().add(interceptor);
|
|
||||||
client.setConnectionPool(null);
|
client.setConnectionPool(null);
|
||||||
|
|
||||||
|
host = server.getHostName() + ":" + server.getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void setLevelShouldPreventNullValue() {
|
@Test public void setLevelShouldPreventNullValue() {
|
||||||
try {
|
try {
|
||||||
interceptor.setLevel(null);
|
applicationInterceptor.setLevel(null);
|
||||||
fail();
|
fail();
|
||||||
} catch (NullPointerException expected) {
|
} catch (NullPointerException expected) {
|
||||||
assertEquals("level == null. Use Level.NONE instead.", expected.getMessage());
|
assertEquals("level == null. Use Level.NONE instead.", expected.getMessage());
|
||||||
@@ -66,218 +75,403 @@ public final class HttpLoggingInterceptorTest {
|
|||||||
|
|
||||||
@Test public void setLevelShouldReturnSameInstanceOfInterceptor() {
|
@Test public void setLevelShouldReturnSameInstanceOfInterceptor() {
|
||||||
for (Level level : Level.values()) {
|
for (Level level : Level.values()) {
|
||||||
assertSame(interceptor, interceptor.setLevel(level));
|
assertSame(applicationInterceptor, applicationInterceptor.setLevel(level));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void none() throws IOException {
|
@Test public void none() throws IOException {
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
client.newCall(request().build()).execute();
|
client.newCall(request().build()).execute();
|
||||||
assertTrue(logs.isEmpty());
|
|
||||||
|
applicationLogs.assertNoMoreLogs();
|
||||||
|
networkLogs.assertNoMoreLogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void basicGet() throws IOException {
|
@Test public void basicGet() throws IOException {
|
||||||
interceptor.setLevel(Level.BASIC);
|
setLevel(Level.BASIC);
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
client.newCall(request().build()).execute();
|
client.newCall(request().build()).execute();
|
||||||
|
|
||||||
assertEquals(2, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> GET / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> GET / HTTP/1.1")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 0-byte body\\)", logs.get(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 {
|
@Test public void basicPost() throws IOException {
|
||||||
interceptor.setLevel(Level.BASIC);
|
setLevel(Level.BASIC);
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
|
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
|
||||||
|
|
||||||
assertEquals(2, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> POST / HTTP/1.1 (3-byte body)", logs.get(0));
|
.assertLogEqual("--> POST / HTTP/1.1 (3-byte body)")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 0-byte body\\)", logs.get(1)));
|
.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 {
|
@Test public void basicResponseBody() throws IOException {
|
||||||
interceptor.setLevel(Level.BASIC);
|
setLevel(Level.BASIC);
|
||||||
|
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.setBody("Hello!")
|
.setBody("Hello!")
|
||||||
.setHeader("Content-Type", PLAIN.toString()));
|
.setHeader("Content-Type", PLAIN));
|
||||||
client.newCall(request().build()).execute();
|
client.newCall(request().build()).execute();
|
||||||
|
|
||||||
assertEquals(2, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> GET / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> GET / HTTP/1.1")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms, 6-byte body\\)", logs.get(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 {
|
@Test public void headersGet() throws IOException {
|
||||||
interceptor.setLevel(Level.HEADERS);
|
setLevel(Level.HEADERS);
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
client.newCall(request().build()).execute();
|
client.newCall(request().build()).execute();
|
||||||
|
|
||||||
assertEquals(12, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> GET / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> GET / HTTP/1.1")
|
||||||
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
|
.assertLogEqual("--> END GET")
|
||||||
assertEquals("Connection: Keep-Alive", logs.get(2));
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
assertEquals("Accept-Encoding: gzip", logs.get(3));
|
.assertLogEqual("Content-Length: 0")
|
||||||
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
|
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
|
||||||
assertEquals("--> END GET", logs.get(5));
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
assertEquals("Content-Length: 0", logs.get(7));
|
.assertLogEqual("<-- END HTTP")
|
||||||
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(8));
|
.assertNoMoreLogs();
|
||||||
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(9)));
|
|
||||||
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(10)));
|
networkLogs
|
||||||
assertEquals("<-- END HTTP", logs.get(11));
|
.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 {
|
@Test public void headersPost() throws IOException {
|
||||||
interceptor.setLevel(Level.HEADERS);
|
setLevel(Level.HEADERS);
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
|
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
|
||||||
|
|
||||||
assertEquals(14, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> POST / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> POST / HTTP/1.1")
|
||||||
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(1));
|
// TODO force content-type and content-length to show here
|
||||||
assertEquals("Content-Length: 3", logs.get(2));
|
.assertLogEqual("--> END POST")
|
||||||
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(3));
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
assertEquals("Connection: Keep-Alive", logs.get(4));
|
.assertLogEqual("Content-Length: 0")
|
||||||
assertEquals("Accept-Encoding: gzip", logs.get(5));
|
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
|
||||||
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(6)));
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
assertEquals("--> END POST", logs.get(7));
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(8)));
|
.assertLogEqual("<-- END HTTP")
|
||||||
assertEquals("Content-Length: 0", logs.get(9));
|
.assertNoMoreLogs();
|
||||||
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(10));
|
|
||||||
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(11)));
|
networkLogs
|
||||||
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(12)));
|
.assertLogEqual("--> POST / HTTP/1.1")
|
||||||
assertEquals("<-- END HTTP", logs.get(13));
|
.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 {
|
@Test public void headersResponseBody() throws IOException {
|
||||||
interceptor.setLevel(Level.HEADERS);
|
setLevel(Level.HEADERS);
|
||||||
|
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.setBody("Hello!")
|
.setBody("Hello!")
|
||||||
.setHeader("Content-Type", PLAIN.toString()));
|
.setHeader("Content-Type", PLAIN));
|
||||||
client.newCall(request().build()).execute();
|
client.newCall(request().build()).execute();
|
||||||
|
|
||||||
assertEquals(13, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> GET / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> GET / HTTP/1.1")
|
||||||
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
|
.assertLogEqual("--> END GET")
|
||||||
assertEquals("Connection: Keep-Alive", logs.get(2));
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
assertEquals("Accept-Encoding: gzip", logs.get(3));
|
.assertLogEqual("Content-Length: 6")
|
||||||
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
|
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
|
||||||
assertEquals("--> END GET", logs.get(5));
|
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
assertEquals("Content-Length: 6", logs.get(7));
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(8));
|
.assertLogEqual("<-- END HTTP")
|
||||||
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(9));
|
.assertNoMoreLogs();
|
||||||
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(10)));
|
|
||||||
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(11)));
|
networkLogs
|
||||||
assertEquals("<-- END HTTP", logs.get(12));
|
.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 {
|
@Test public void bodyGet() throws IOException {
|
||||||
interceptor.setLevel(Level.BODY);
|
setLevel(Level.BODY);
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
client.newCall(request().build()).execute();
|
client.newCall(request().build()).execute();
|
||||||
|
|
||||||
assertEquals(12, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> GET / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> GET / HTTP/1.1")
|
||||||
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
|
.assertLogEqual("--> END GET")
|
||||||
assertEquals("Connection: Keep-Alive", logs.get(2));
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
assertEquals("Accept-Encoding: gzip", logs.get(3));
|
.assertLogEqual("Content-Length: 0")
|
||||||
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
|
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
|
||||||
assertEquals("--> END GET", logs.get(5));
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
assertEquals("Content-Length: 0", logs.get(7));
|
.assertLogEqual("<-- END HTTP (0-byte body)")
|
||||||
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(8));
|
.assertNoMoreLogs();
|
||||||
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(9)));
|
|
||||||
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(10)));
|
networkLogs
|
||||||
assertEquals("<-- END HTTP (0-byte body)", logs.get(11));
|
.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 {
|
@Test public void bodyPost() throws IOException {
|
||||||
interceptor.setLevel(Level.BODY);
|
setLevel(Level.BODY);
|
||||||
|
|
||||||
server.enqueue(new MockResponse());
|
server.enqueue(new MockResponse());
|
||||||
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
|
client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute();
|
||||||
|
|
||||||
assertEquals(16, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> POST / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> POST / HTTP/1.1")
|
||||||
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(1));
|
// TODO force content-type and content-length to show here
|
||||||
assertEquals("Content-Length: 3", logs.get(2));
|
.assertLogEqual("")
|
||||||
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(3));
|
.assertLogEqual("Hi?")
|
||||||
assertEquals("Connection: Keep-Alive", logs.get(4));
|
.assertLogEqual("--> END POST (3-byte body)")
|
||||||
assertEquals("Accept-Encoding: gzip", logs.get(5));
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(6)));
|
.assertLogEqual("Content-Length: 0")
|
||||||
assertEquals("", logs.get(7));
|
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
|
||||||
assertEquals("Hi?", logs.get(8));
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
assertEquals("--> END POST (3-byte body)", logs.get(9));
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(10)));
|
.assertLogEqual("<-- END HTTP (0-byte body)")
|
||||||
assertEquals("Content-Length: 0", logs.get(11));
|
.assertNoMoreLogs();
|
||||||
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(12));
|
|
||||||
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(13)));
|
networkLogs
|
||||||
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(14)));
|
.assertLogEqual("--> POST / HTTP/1.1")
|
||||||
assertEquals("<-- END HTTP (0-byte body)", logs.get(15));
|
.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 {
|
@Test public void bodyResponseBody() throws IOException {
|
||||||
interceptor.setLevel(Level.BODY);
|
setLevel(Level.BODY);
|
||||||
|
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.setBody("Hello!")
|
.setBody("Hello!")
|
||||||
.setHeader("Content-Type", PLAIN.toString()));
|
.setHeader("Content-Type", PLAIN));
|
||||||
client.newCall(request().build()).execute();
|
client.newCall(request().build()).execute();
|
||||||
|
|
||||||
assertEquals(15, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> GET / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> GET / HTTP/1.1")
|
||||||
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
|
.assertLogEqual("--> END GET")
|
||||||
assertEquals("Connection: Keep-Alive", logs.get(2));
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
assertEquals("Accept-Encoding: gzip", logs.get(3));
|
.assertLogEqual("Content-Length: 6")
|
||||||
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
|
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
|
||||||
assertEquals("--> END GET", logs.get(5));
|
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
assertEquals("Content-Length: 6", logs.get(7));
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(8));
|
.assertLogEqual("")
|
||||||
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(9));
|
.assertLogEqual("Hello!")
|
||||||
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(10)));
|
.assertLogEqual("<-- END HTTP (6-byte body)")
|
||||||
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(11)));
|
.assertNoMoreLogs();
|
||||||
assertEquals("", logs.get(12));
|
|
||||||
assertEquals("Hello!", logs.get(13));
|
networkLogs
|
||||||
assertEquals("<-- END HTTP (6-byte body)", logs.get(14));
|
.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 {
|
@Test public void bodyResponseBodyChunked() throws IOException {
|
||||||
interceptor.setLevel(Level.BODY);
|
setLevel(Level.BODY);
|
||||||
|
|
||||||
server.enqueue(new MockResponse()
|
server.enqueue(new MockResponse()
|
||||||
.setChunkedBody("Hello!", 2)
|
.setChunkedBody("Hello!", 2)
|
||||||
.setHeader("Content-Type", PLAIN.toString()));
|
.setHeader("Content-Type", PLAIN));
|
||||||
client.newCall(request().build()).execute();
|
client.newCall(request().build()).execute();
|
||||||
|
|
||||||
assertEquals(15, logs.size());
|
applicationLogs
|
||||||
assertEquals("--> GET / HTTP/1.1", logs.get(0));
|
.assertLogEqual("--> GET / HTTP/1.1")
|
||||||
assertEquals("Host: " + server.getHostName() + ":" + server.getPort(), logs.get(1));
|
.assertLogEqual("--> END GET")
|
||||||
assertEquals("Connection: Keep-Alive", logs.get(2));
|
.assertLogMatch("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)")
|
||||||
assertEquals("Accept-Encoding: gzip", logs.get(3));
|
.assertLogEqual("Transfer-encoding: chunked")
|
||||||
assertTrue(Pattern.matches("User-Agent: okhttp/.+", logs.get(4)));
|
.assertLogEqual("Content-Type: text/plain; charset=utf-8")
|
||||||
assertEquals("--> END GET", logs.get(5));
|
.assertLogEqual("OkHttp-Selected-Protocol: http/1.1")
|
||||||
assertTrue(Pattern.matches("<-- HTTP/1\\.1 200 OK \\(\\d+ms\\)", logs.get(6)));
|
.assertLogMatch("OkHttp-Sent-Millis: \\d+")
|
||||||
assertEquals("Transfer-encoding: chunked", logs.get(7));
|
.assertLogMatch("OkHttp-Received-Millis: \\d+")
|
||||||
assertEquals("Content-Type: text/plain; charset=utf-8", logs.get(8));
|
.assertLogEqual("")
|
||||||
assertEquals("OkHttp-Selected-Protocol: http/1.1", logs.get(9));
|
.assertLogEqual("Hello!")
|
||||||
assertTrue(Pattern.matches("OkHttp-Sent-Millis: \\d+", logs.get(10)));
|
.assertLogEqual("<-- END HTTP (6-byte body)")
|
||||||
assertTrue(Pattern.matches("OkHttp-Received-Millis: \\d+", logs.get(11)));
|
.assertNoMoreLogs();
|
||||||
assertEquals("", logs.get(12));
|
|
||||||
assertEquals("Hello!", logs.get(13));
|
networkLogs
|
||||||
assertEquals("<-- END HTTP (6-byte body)", logs.get(14));
|
.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() {
|
private Request.Builder request() {
|
||||||
return new Request.Builder().url(server.url("/"));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user