@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 connectFail() throws IOException { setLevel(Level.BASIC); client = new OkHttpClient.Builder() .dns(new Dns() { @Override public List<InetAddress> lookup(String hostname) throws UnknownHostException { throw new UnknownHostException("reason"); } }) .addInterceptor(applicationInterceptor) .build(); try { client.newCall(request().build()).execute(); fail(); } catch (UnknownHostException expected) { } applicationLogs .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("<-- HTTP FAILED: java.net.UnknownHostException: reason") .assertNoMoreLogs(); }
@Test public void basicGet() throws IOException { setLevel(Level.BASIC); server.enqueue(new MockResponse()); client.newCall(request().build()).execute(); applicationLogs .assertLogEqual("--> GET " + url) .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 + " (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) .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) .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 connectFail() throws IOException { setLevel(Level.BASIC); client = new OkHttpClient.Builder() .dns(new Dns() { @Override public List<InetAddress> lookup(String hostname) throws UnknownHostException { throw new UnknownHostException("reason"); } }) .addInterceptor(applicationInterceptor) .build(); try { client.newCall(request().build()).execute(); fail(); } catch (UnknownHostException expected) { } applicationLogs .assertLogEqual("--> GET " + url) .assertLogEqual("<-- HTTP FAILED: java.net.UnknownHostException: reason") .assertNoMoreLogs(); }
@Test public void http2() throws Exception { server.useHttps(sslClient.socketFactory, false); url = server.url("/"); setLevel(Level.BASIC); server.enqueue(new MockResponse()); Response response = client.newCall(request().build()).execute(); assumeThat(response.protocol(), equalTo(Protocol.HTTP_2)); applicationLogs .assertLogEqual("--> GET " + url) .assertLogMatch("<-- 200 " + url + " \\(\\d+ms, 0-byte body\\)") .assertNoMoreLogs(); networkLogs .assertLogEqual("--> GET " + url + " h2") .assertLogMatch("<-- 200 " + url + " \\(\\d+ms, 0-byte body\\)") .assertNoMoreLogs(); }
public static Builder getHeader(String userName, String password) { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(Level.BASIC); OkHttpClient.Builder okBuilder = new OkHttpClient.Builder(); okBuilder.authenticator(new Authenticator() { @Override public Request authenticate(Route route, Response response) throws IOException { String credential = Credentials.basic(userName, password); if (credential.equals(response.request().header("Authorization"))) { try { FortifyExceptionUtil.verifyFortifyResponseCode(response.code(), "Unauthorized access of Fortify Api"); } catch (IntegrationException e) { throw new IOException(e); } return null; } return response.request().newBuilder().header("Authorization", credential).build(); } }); okBuilder.addInterceptor(logging); return okBuilder; }
@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(); }
private MarvelApi() { AuthenticatorInterceptor authenticator = new AuthenticatorInterceptor(); HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(BuildConfig.DEBUG ? Level.BODY : Level.BASIC); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(authenticator) .addInterceptor(logging) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); mService = retrofit.create(MarvelService.class); }
public static Retrofit2Delegate factory(String baseUrl, long visitDuration, final DeviceInfo deviceInfo, boolean loggingEnabled) { HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(loggingEnabled ? Level.BODY : Level.NONE); Interceptor userAgentInterceptor = new Interceptor() { @Override public Response intercept(@NonNull Chain chain) throws IOException { Builder builder = chain.request().newBuilder(); builder.header("User-Agent", deviceInfo.getUserAgent()); return chain.proceed(builder.build()); } }; OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .addInterceptor(userAgentInterceptor) .connectTimeout(100, TimeUnit.SECONDS) .readTimeout(150, TimeUnit.SECONDS) .writeTimeout(100, TimeUnit.SECONDS) .build(); return new Retrofit2Delegate(baseUrl, okHttpClient, visitDuration, deviceInfo); }
/** * 创建网络辅助类 */ public WebClient() { //创建OkHttpClient.Builder OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder() .retryOnConnectionFailure(false) .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS); //添加自定义的配置,比如添加拦截器等 okhttpBuilder = configOkHttp(okhttpBuilder); //创建OkHttpClient HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(BuildConfig.DEBUG == true ? Level.BODY : Level.NONE); okhttpBuilder.addInterceptor(logging); client = okhttpBuilder.build(); }
public Builder(String ApiKey) { retrofit = new Retrofit.Builder(); authRequestInterceptor = new AuthRequestInterceptor(ApiKey); errorInterceptor = new ErrorInterceptor(); httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(Level.NONE); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(authRequestInterceptor) .addInterceptor(errorInterceptor) .addInterceptor(httpLoggingInterceptor) .build(); this.setServer(SERVER_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(gson)); }
public static Level getRetrofitLogLevel() { String prefValue = getString(R.string.preference_key_retrofit_log_level, null); if (prefValue == null) { return ReleaseUtil.isDevRelease() ? Level.BASIC : Level.NONE; } switch (prefValue) { case "BASIC": return Level.BASIC; case "HEADERS": return Level.HEADERS; case "BODY": return Level.BODY; case "NONE": default: return Level.NONE; } }
@Test public void levelGetter() { // The default is NONE. Assert.assertEquals(Level.NONE, applicationInterceptor.getLevel()); for (Level level : Level.values()) { applicationInterceptor.setLevel(level); assertEquals(level, applicationInterceptor.getLevel()); } }
@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") .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(); }
@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") .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") .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") .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(); }
@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") .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") .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") .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") .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") .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") .assertLogEqual("<-- END HTTP (0-byte body)") .assertNoMoreLogs(); }
@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") .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(); }
@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") .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") .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") .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") .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+") .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") .assertLogEqual("") .assertLogEqual("Hello, Hello, Hello") .assertLogEqual("<-- END HTTP (19-byte body)") .assertNoMoreLogs(); }
@Test public void bodyGetMalformedCharset() throws IOException { setLevel(Level.BODY); server.enqueue(new MockResponse() .setHeader("Content-Type", "text/html; charset=0") .setBody("Body with unknown charset")); 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-Type: text/html; charset=0") .assertLogMatch("Content-Length: \\d+") .assertLogMatch("") .assertLogEqual("Body with unknown charset") .assertLogEqual("<-- END HTTP (25-byte body)") .assertNoMoreLogs(); applicationLogs .assertLogEqual("--> GET " + url + " http/1.1") .assertLogEqual("--> END GET") .assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)") .assertLogEqual("Content-Type: text/html; charset=0") .assertLogMatch("Content-Length: \\d+") .assertLogEqual("") .assertLogEqual("Body with unknown charset") .assertLogEqual("<-- END HTTP (25-byte 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) .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(); }