List of usage examples for com.squareup.okhttp Request url
HttpUrl url
To view the source code for com.squareup.okhttp Request url.
Click Source Link
From source file:cn.com.canon.darwin.modules.service.api.interceptor.HttpLoggingInterceptor.java
License:Apache License
@Override public Response intercept(Chain chain) throws IOException { Level level = this.level; Request request = chain.request(); if (level == Level.NONE) { return chain.proceed(request); }/*from w w w. jav a2s . c o m*/ boolean logBody = level == Level.BODY; boolean logHeaders = logBody || level == Level.HEADERS; RequestBody requestBody = request.body(); boolean hasRequestBody = requestBody != null; Connection connection = chain.connection(); Protocol protocol = connection != null ? connection.getProtocol() : Protocol.HTTP_1_1; String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol; if (!logHeaders && hasRequestBody) { requestStartMessage += " (" + requestBody.contentLength() + "-byte body)"; } logger.log(requestStartMessage); if (logHeaders) { // if (hasRequestBody) { // // Request body headers are only present when installed as a // // network interceptor. Force // // them to be included (when available) so there values are // // known. // if (requestBody.contentType() != null) { // logger.log("Content-Type: " + requestBody.contentType()); // } // if (requestBody.contentLength() != -1) { // logger.log("Content-Length: " + requestBody.contentLength()); // } // } // Headers headers = request.headers(); // for (int i = 0, count = headers.size(); i < count; i++) { // String name = headers.name(i); // // Skip headers from the request body as they are explicitly // // logged above. // if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) { // logger.log(name + ": " + headers.value(i)); // } // } if (!logBody || !hasRequestBody) { logger.log("--> END " + request.method()); } else if (bodyEncoded(request.headers())) { logger.log("--> END " + request.method() + " (encoded body omitted)"); } else { Buffer buffer = new Buffer(); requestBody.writeTo(buffer); Charset charset = UTF8; MediaType contentType = requestBody.contentType(); if (contentType != null) { charset = contentType.charset(UTF8); } logger.log(""); if (isPlaintext(buffer)) { logger.log(buffer.readString(charset)); logger.log("--> END " + request.method() + " (" + requestBody.contentLength() + "-byte body)"); } else { logger.log("--> END " + request.method() + " (binary " + requestBody.contentLength() + "-byte body omitted)"); } } } long startNs = System.nanoTime(); Response response; try { response = chain.proceed(request); } catch (Exception e) { logger.log("<-- HTTP FAILED: " + e); throw e; } long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs); ResponseBody responseBody = response.body(); long contentLength = responseBody.contentLength(); String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length"; logger.log("<-- " + response.code() + ' ' + response.message() + ' ' + response.request().url() + " (" + tookMs + "ms" + (!logHeaders ? ", " + bodySize + " body" : "") + ')'); // if (logHeaders) { // Headers headers = response.headers(); // for (int i = 0, count = headers.size(); i < count; i++) { // logger.log(headers.name(i) + ": " + headers.value(i)); // } // if (!logBody || !HttpEngine.hasBody(response)) { logger.log("<-- END HTTP"); } else if (bodyEncoded(response.headers())) { logger.log("<-- END HTTP (encoded body omitted)"); } else { BufferedSource source = responseBody.source(); source.request(Long.MAX_VALUE); // Buffer the entire body. Buffer buffer = source.buffer(); Charset charset = UTF8; MediaType contentType = responseBody.contentType(); if (contentType != null) { try { charset = contentType.charset(UTF8); } catch (UnsupportedCharsetException e) { logger.log(""); logger.log("Couldn't decode the response body; charset is likely malformed."); logger.log("<-- END HTTP"); return response; } } if (!isPlaintext(buffer)) { logger.log(""); logger.log("<-- END HTTP (binary " + buffer.size() + "-byte body omitted)"); return response; } if (contentLength != 0) { logger.log(""); logger.log(buffer.clone().readString(charset)); } logger.log("<-- END HTTP (" + buffer.size() + "-byte body)"); } // } return response; }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void get_Async() throws Exception { server.enqueue(new MockResponse().setBody("abc").addHeader("Content-Type: text/plain")); Request request = new Request.Builder().url(server.getUrl("/")).header("User-Agent", "AsyncApiTest") .build();/*from w ww . j av a2 s. c om*/ client.newCall(request).enqueue(callback); callback.await(request.url()).assertCode(200).assertHeader("Content-Type", "text/plain").assertBody("abc"); assertEquals("AsyncApiTest", server.takeRequest().getHeader("User-Agent")); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void tls_Async() throws Exception { server.get().useHttps(sslContext.getSocketFactory(), false); server.enqueue(new MockResponse().setBody("abc").addHeader("Content-Type: text/plain")); client.setSslSocketFactory(sslContext.getSocketFactory()); client.setHostnameVerifier(new RecordingHostnameVerifier()); Request request = new Request.Builder().url(server.getUrl("/")).build(); client.newCall(request).enqueue(callback); callback.await(request.url()).assertHandshake(); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void recoverFromTlsHandshakeFailure_Async() throws Exception { server.get().useHttps(sslContext.getSocketFactory(), false); server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE)); server.enqueue(new MockResponse().setBody("abc")); suppressTlsFallbackScsv(client);/*from w w w.j a v a2s.c om*/ client.setHostnameVerifier(new RecordingHostnameVerifier()); Request request = new Request.Builder().url(server.getUrl("/")).build(); client.newCall(request).enqueue(callback); callback.await(request.url()).assertBody("abc"); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void post_Async() throws Exception { server.enqueue(new MockResponse().setBody("abc")); Request request = new Request.Builder().url(server.getUrl("/")) .post(RequestBody.create(MediaType.parse("text/plain"), "def")).build(); client.newCall(request).enqueue(callback); callback.await(request.url()).assertCode(200).assertBody("abc"); RecordedRequest recordedRequest = server.takeRequest(); assertEquals("def", recordedRequest.getBody().readUtf8()); assertEquals("3", recordedRequest.getHeader("Content-Length")); assertEquals("text/plain; charset=utf-8", recordedRequest.getHeader("Content-Type")); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void cacheHit() throws Exception { server.enqueue(new MockResponse().addHeader("ETag: v1").addHeader("Cache-Control: max-age=60") .addHeader("Vary: Accept-Charset").setBody("A")); client.setCache(cache);//from w w w.j a v a2 s . c o m // Store a response in the cache. URL url = server.getUrl("/"); Request cacheStoreRequest = new Request.Builder().url(url).addHeader("Accept-Language", "fr-CA") .addHeader("Accept-Charset", "UTF-8").build(); FiberOkHttpTestUtil.executeInFiberRecorded(client, cacheStoreRequest).assertCode(200).assertBody("A"); assertNull(server.takeRequest().getHeader("If-None-Match")); // Hit that stored response. Request cacheHitRequest = new Request.Builder().url(url).addHeader("Accept-Language", "en-US") // Different, but Vary says it doesn't matter. .addHeader("Accept-Charset", "UTF-8").build(); RecordedResponse cacheHit = FiberOkHttpTestUtil.executeInFiberRecorded(client, cacheHitRequest); // Check the merged response. The request is the application's original request. cacheHit.assertCode(200).assertBody("A").assertHeader("ETag", "v1") .assertRequestUrl(cacheStoreRequest.url()).assertRequestHeader("Accept-Language", "en-US") .assertRequestHeader("Accept-Charset", "UTF-8"); // Check the cached response. Its request contains only the saved Vary headers. cacheHit.cacheResponse().assertCode(200).assertHeader("ETag", "v1").assertRequestMethod("GET") .assertRequestUrl(cacheStoreRequest.url()).assertRequestHeader("Accept-Language") .assertRequestHeader("Accept-Charset", "UTF-8"); cacheHit.assertNoNetworkResponse(); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void conditionalCacheHit() throws Exception { server.enqueue(new MockResponse().addHeader("ETag: v1").addHeader("Vary: Accept-Charset") .addHeader("Donut: a").setBody("A")); server.enqueue(new MockResponse().clearHeaders().addHeader("Donut: b") .setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED)); client.setCache(cache);/* w w w.j av a 2s.co m*/ // Store a response in the cache. URL url = server.getUrl("/"); Request cacheStoreRequest = new Request.Builder().url(url).addHeader("Accept-Language", "fr-CA") .addHeader("Accept-Charset", "UTF-8").build(); FiberOkHttpTestUtil.executeInFiberRecorded(client, cacheStoreRequest).assertCode(200) .assertHeader("Donut", "a").assertBody("A"); assertNull(server.takeRequest().getHeader("If-None-Match")); // Hit that stored response. Request cacheHitRequest = new Request.Builder().url(url).addHeader("Accept-Language", "en-US") // Different, but Vary says it doesn't matter. .addHeader("Accept-Charset", "UTF-8").build(); RecordedResponse cacheHit = FiberOkHttpTestUtil.executeInFiberRecorded(client, cacheHitRequest); assertEquals("v1", server.takeRequest().getHeader("If-None-Match")); // Check the merged response. The request is the application's original request. cacheHit.assertCode(200).assertBody("A").assertHeader("Donut", "b") .assertRequestUrl(cacheStoreRequest.url()).assertRequestHeader("Accept-Language", "en-US") .assertRequestHeader("Accept-Charset", "UTF-8").assertRequestHeader("If-None-Match"); // No If-None-Match on the user's request. // Check the cached response. Its request contains only the saved Vary headers. cacheHit.cacheResponse().assertCode(200).assertHeader("Donut", "a").assertHeader("ETag", "v1") .assertRequestUrl(cacheStoreRequest.url()).assertRequestHeader("Accept-Language") // No Vary on Accept-Language. .assertRequestHeader("Accept-Charset", "UTF-8") // Because of Vary on Accept-Charset. .assertRequestHeader("If-None-Match"); // This wasn't present in the original request. // Check the network response. It has the caller's request, plus some caching headers. cacheHit.networkResponse().assertCode(304).assertHeader("Donut", "b") .assertRequestHeader("Accept-Language", "en-US").assertRequestHeader("Accept-Charset", "UTF-8") .assertRequestHeader("If-None-Match", "v1"); // If-None-Match in the validation request. }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void conditionalCacheHit_Async() throws Exception { server.enqueue(new MockResponse().setBody("A").addHeader("ETag: v1")); server.enqueue(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED)); client.setCache(cache);/*from w w w.ja v a 2 s.c o m*/ Request request1 = new Request.Builder().url(server.getUrl("/")).build(); client.newCall(request1).enqueue(callback); callback.await(request1.url()).assertCode(200).assertBody("A"); assertNull(server.takeRequest().getHeader("If-None-Match")); Request request2 = new Request.Builder().url(server.getUrl("/")).build(); client.newCall(request2).enqueue(callback); callback.await(request2.url()).assertCode(200).assertBody("A"); assertEquals("v1", server.takeRequest().getHeader("If-None-Match")); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void conditionalCacheMiss() throws Exception { server.enqueue(new MockResponse().addHeader("ETag: v1").addHeader("Vary: Accept-Charset") .addHeader("Donut: a").setBody("A")); server.enqueue(new MockResponse().addHeader("Donut: b").setBody("B")); client.setCache(cache);//from w w w .ja v a 2s.c o m Request cacheStoreRequest = new Request.Builder().url(server.getUrl("/")) .addHeader("Accept-Language", "fr-CA").addHeader("Accept-Charset", "UTF-8").build(); FiberOkHttpTestUtil.executeInFiberRecorded(client, cacheStoreRequest).assertCode(200).assertBody("A"); assertNull(server.takeRequest().getHeader("If-None-Match")); Request cacheMissRequest = new Request.Builder().url(server.getUrl("/")) .addHeader("Accept-Language", "en-US") // Different, but Vary says it doesn't matter. .addHeader("Accept-Charset", "UTF-8").build(); RecordedResponse cacheHit = FiberOkHttpTestUtil.executeInFiberRecorded(client, cacheMissRequest); assertEquals("v1", server.takeRequest().getHeader("If-None-Match")); // Check the user response. It has the application's original request. cacheHit.assertCode(200).assertBody("B").assertHeader("Donut", "b") .assertRequestUrl(cacheStoreRequest.url()); // Check the cache response. Even though it's a miss, we used the cache. cacheHit.cacheResponse().assertCode(200).assertHeader("Donut", "a").assertHeader("ETag", "v1") .assertRequestUrl(cacheStoreRequest.url()); // Check the network response. It has the network request, plus caching headers. cacheHit.networkResponse().assertCode(200).assertHeader("Donut", "b") .assertRequestHeader("If-None-Match", "v1") // If-None-Match in the validation request. .assertRequestUrl(cacheStoreRequest.url()); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void conditionalCacheMiss_Async() throws Exception { server.enqueue(new MockResponse().setBody("A").addHeader("ETag: v1")); server.enqueue(new MockResponse().setBody("B")); client.setCache(cache);//from w ww. j av a2 s . c om Request request1 = new Request.Builder().url(server.getUrl("/")).build(); client.newCall(request1).enqueue(callback); callback.await(request1.url()).assertCode(200).assertBody("A"); assertNull(server.takeRequest().getHeader("If-None-Match")); Request request2 = new Request.Builder().url(server.getUrl("/")).build(); client.newCall(request2).enqueue(callback); callback.await(request2.url()).assertCode(200).assertBody("B"); assertEquals("v1", server.takeRequest().getHeader("If-None-Match")); }