HttpLoggingInterceptor implements Interceptor { public HttpLoggingInterceptor setLevel(Level level) { if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead."); this.level = level; return this; } HttpLoggingInterceptor();  HttpLoggingInterceptor(Logger logger); HttpLoggingInterceptor setLevel(Level level); Level getLevel(); @Override Response intercept(Chain chain);  }
@Test public void setLevelShouldPreventNullValue() { try { applicationInterceptor.setLevel(null); fail(); } catch (NullPointerException expected) { assertEquals("level == null. Use Level.NONE instead.", expected.getMessage()); } }
@Test public void setLevelShouldReturnSameInstanceOfInterceptor() { for (Level level : Level.values()) { assertSame(applicationInterceptor, applicationInterceptor.setLevel(level)); } }
@Test public void basicGet() throws IOException { setLevel(Level.BASIC); server.enqueue(new MockResponse()); client.newCall(request().build()).execute(); applicationLogs .assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)") .assertNoMoreLogs(); networkLogs .assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)") .assertNoMoreLogs(); }
@Test public void basicPost() throws IOException { setLevel(Level.BASIC); server.enqueue(new MockResponse()); client.newCall(request().post(RequestBody.create(PLAIN, "Hi?")).build()).execute(); applicationLogs .assertLogEqual("--> POST " + url + " HTTP/1.1 (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(); }
@Test public void basicResponseBody() throws IOException { setLevel(Level.BASIC); server.enqueue(new MockResponse() .setBody("Hello!") .setHeader("Content-Type", PLAIN)); Response response = client.newCall(request().build()).execute(); response.body().close(); applicationLogs .assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 6-byte body\\)") .assertNoMoreLogs(); networkLogs .assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 6-byte body\\)") .assertNoMoreLogs(); }
@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 + " HTTP/1.1") .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(); }
@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 + " HTTP/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .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") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP") .assertNoMoreLogs(); }
@Test public void headersPost() throws IOException { setLevel(Level.HEADERS); server.enqueue(new MockResponse()); Request request = request().post(RequestBody.create(PLAIN, "Hi?")).build(); Response response = client.newCall(request).execute(); response.body().close(); applicationLogs .assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Content-Length: 3") .assertLogEqual("--> END POST") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP") .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("--> END POST") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP") .assertNoMoreLogs(); }
@Test public void headersPostNoContentType() throws IOException { setLevel(Level.HEADERS); server.enqueue(new MockResponse()); Request request = request().post(RequestBody.create(null, "Hi?")).build(); Response response = client.newCall(request).execute(); response.body().close(); applicationLogs .assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("Content-Length: 3") .assertLogEqual("--> END POST") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .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") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP") .assertNoMoreLogs(); }
@Test public void headersPostNoLength() throws IOException { setLevel(Level.HEADERS); server.enqueue(new MockResponse()); RequestBody body = new RequestBody() { @Override public MediaType contentType() { return PLAIN; } @Override public void writeTo(BufferedSink sink) throws IOException { sink.writeUtf8("Hi!"); } }; Response response = client.newCall(request().post(body).build()).execute(); response.body().close(); applicationLogs .assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("--> END POST") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP") .assertNoMoreLogs(); networkLogs .assertLogEqual("--> POST " + url + " HTTP/1.1") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogEqual("Transfer-Encoding: chunked") .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") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP") .assertNoMoreLogs(); }
@Test public void headersResponseBody() throws IOException { setLevel(Level.HEADERS); server.enqueue(new MockResponse() .setBody("Hello!") .setHeader("Content-Type", PLAIN)); Response response = client.newCall(request().build()).execute(); response.body().close(); applicationLogs .assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 6") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .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: 6") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP") .assertNoMoreLogs(); }
@Test public void bodyGet() throws IOException { setLevel(Level.BODY); server.enqueue(new MockResponse()); Response response = client.newCall(request().build()).execute(); response.body().close(); applicationLogs .assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 0") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP (0-byte body)") .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") .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); }
@Test public void bodyPost() throws IOException { setLevel(Level.BODY); server.enqueue(new MockResponse()); Request request = request().post(RequestBody.create(PLAIN, "Hi?")).build(); Response response = client.newCall(request).execute(); response.body().close(); applicationLogs .assertLogEqual("--> POST " + url + " HTTP/1.1") .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") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .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") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP (0-byte body)") .assertNoMoreLogs(); }
@Test public void bodyResponseBody() throws IOException { setLevel(Level.BODY); server.enqueue(new MockResponse() .setBody("Hello!") .setHeader("Content-Type", PLAIN)); Response response = client.newCall(request().build()).execute(); response.body().close(); applicationLogs .assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Length: 6") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("") .assertLogEqual("Hello!") .assertLogEqual("<-- END HTTP (6-byte body)") .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: 6") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .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 { setLevel(Level.BODY); 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 + " HTTP/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Transfer-encoding: chunked") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("") .assertLogEqual("Hello!") .assertLogEqual("<-- END HTTP (6-byte body)") .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("Transfer-encoding: chunked") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("") .assertLogEqual("Hello!") .assertLogEqual("<-- END HTTP (6-byte body)") .assertNoMoreLogs(); }
@Test public void bodyResponseNotIdentityEncoded() 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(); 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: gzip") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogMatch("Content-Length: \\d+") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("<-- END HTTP (encoded body omitted)") .assertNoMoreLogs(); applicationLogs .assertLogEqual("--> GET " + url + " HTTP/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Type: text/plain; charset=utf-8") .assertLogMatch("OkHttp-Sent-Millis: \\d+") .assertLogMatch("OkHttp-Received-Millis: \\d+") .assertLogEqual("") .assertLogEqual("Hello, Hello, Hello") .assertLogEqual("<-- END HTTP (19-byte body)") .assertNoMoreLogs(); }