List of usage examples for com.squareup.okhttp Response body
ResponseBody body
To view the source code for com.squareup.okhttp Response body.
Click Source Link
From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java
License:Open Source License
/** Make sure interceptors can interact with the OkHttp client. */ @Test//from w ww . j av a2s. co m public void interceptorMakesAnUnrelatedRequest() throws Exception { server.enqueue(new MockResponse().setBody("a")); // Fetched by interceptor. server.enqueue(new MockResponse().setBody("b")); // Fetched directly. client.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { if (chain.request().url().getPath().equals("/b")) { Request requestA = new Request.Builder().url(server.getUrl("/a")).build(); Response responseA = null; try { responseA = FiberOkHttpUtil.executeInFiber(client, requestA); } catch (InterruptedException ex) { throw new AssertionError(ex); } assertEquals("a", responseA.body().string()); } return chain.proceed(chain.request()); } }); Request requestB = new Request.Builder().url(server.getUrl("/b")).build(); Response responseB = FiberOkHttpUtil.executeInFiber(client, requestB); assertEquals("b", responseB.body().string()); }
From source file:co.paralleluniverse.fibers.okhttp.RecordedResponse.java
License:Open Source License
/** * Asserts that the current response was redirected and returns the prior * response.//ww w.j av a 2 s.c om */ public RecordedResponse priorResponse() { Response priorResponse = response.priorResponse(); assertNotNull(priorResponse); assertNull(priorResponse.body()); return new RecordedResponse(priorResponse.request(), priorResponse, null, null, null); }
From source file:co.paralleluniverse.fibers.okhttp.RecordedResponse.java
License:Open Source License
/** * Asserts that the current response used the network and returns the network * response./* w w w . j a v a 2 s . c o m*/ */ public RecordedResponse networkResponse() { Response networkResponse = response.networkResponse(); assertNotNull(networkResponse); assertNull(networkResponse.body()); return new RecordedResponse(networkResponse.request(), networkResponse, null, null, null); }
From source file:co.paralleluniverse.fibers.okhttp.RecordedResponse.java
License:Open Source License
/** * Asserts that the current response used the cache and returns the cache * response.//from w w w .j a v a2 s .c o m */ public RecordedResponse cacheResponse() { Response cacheResponse = response.cacheResponse(); assertNotNull(cacheResponse); assertNull(cacheResponse.body()); return new RecordedResponse(cacheResponse.request(), cacheResponse, null, null, null); }
From source file:co.paralleluniverse.fibers.okhttp.RecordingCallback.java
License:Open Source License
@Override public synchronized void onResponse(Response response) throws IOException { Buffer buffer = new Buffer(); ResponseBody body = response.body(); body.source().readAll(buffer);//ww w. j a v a 2 s. c om responses.add(new RecordedResponse(response.request(), response, null, buffer.readUtf8(), null)); notifyAll(); }
From source file:co.paralleluniverse.fibers.okhttp.SocksProxyTest.java
License:Open Source License
@Test public void proxy() throws Exception { server.enqueue(new MockResponse().setBody("abc")); server.enqueue(new MockResponse().setBody("def")); OkHttpClient client = new FiberOkHttpClient().setProxy(socksProxy.proxy()); Request request1 = new Request.Builder().url(server.getUrl("/")).build(); Response response1 = FiberOkHttpUtil.executeInFiber((FiberOkHttpClient) client, request1); assertEquals("abc", response1.body().string()); Request request2 = new Request.Builder().url(server.getUrl("/")).build(); Response response2 = FiberOkHttpUtil.executeInFiber((FiberOkHttpClient) client, request2); assertEquals("def", response2.body().string()); // The HTTP calls should share a single connection. assertEquals(1, socksProxy.connectionCount()); }
From source file:co.paralleluniverse.fibers.okhttp.SocksProxyTest.java
License:Open Source License
@Test public void proxySelector() throws Exception { server.enqueue(new MockResponse().setBody("abc")); ProxySelector proxySelector = new ProxySelector() { @Override/* w w w . jav a 2 s . co m*/ public List<Proxy> select(URI uri) { return Collections.singletonList(socksProxy.proxy()); } @Override public void connectFailed(URI uri, SocketAddress socketAddress, IOException e) { throw new AssertionError(); } }; OkHttpClient client = new FiberOkHttpClient().setProxySelector(proxySelector); Request request = new Request.Builder().url(server.getUrl("/")).build(); Response response = FiberOkHttpUtil.executeInFiber((FiberOkHttpClient) client, request); assertEquals("abc", response.body().string()); assertEquals(1, socksProxy.connectionCount()); }
From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java
License:Apache License
@Test public void networkInterceptorsObserveNetworkHeaders() throws Exception { server.enqueue(new MockResponse().setBody(gzip("abcabcabc")).addHeader("Content-Encoding: gzip")); client.networkInterceptors().add(new Interceptor() { @Override//from www .j ava 2 s . c om public Response intercept(Chain chain) throws IOException { // The network request has everything: User-Agent, Host, Accept-Encoding. Request networkRequest = chain.request(); assertNotNull(networkRequest.header("User-Agent")); assertEquals(server.getHostName() + ":" + server.getPort(), networkRequest.header("Host")); assertNotNull(networkRequest.header("Accept-Encoding")); // The network response also has everything, including the raw gzipped content. Response networkResponse = chain.proceed(networkRequest); assertEquals("gzip", networkResponse.header("Content-Encoding")); return networkResponse; } }); Request request = new Request.Builder().url(server.url("/")).build(); // No extra headers in the application's request. assertNull(request.header("User-Agent")); assertNull(request.header("Host")); assertNull(request.header("Accept-Encoding")); // No extra headers in the application's response. Response response = client.newCall(request).execute(); assertNull(request.header("Content-Encoding")); assertEquals("abcabcabc", response.body().string()); }
From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java
License:Apache License
private void rewriteResponseFromServer(List<Interceptor> interceptors) throws Exception { server.enqueue(new MockResponse().addHeader("Original-Header: foo").setBody("abc")); interceptors.add(new Interceptor() { @Override/*from ww w . jav a2 s . co m*/ public Response intercept(Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder().body(uppercase(originalResponse.body())) .addHeader("OkHttp-Intercepted", "yep").build(); } }); Request request = new Request.Builder().url(server.url("/")).build(); Response response = client.newCall(request).execute(); assertEquals("ABC", response.body().string()); assertEquals("yep", response.header("OkHttp-Intercepted")); assertEquals("foo", response.header("Original-Header")); }
From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java
License:Apache License
@Test public void applicationInterceptorsCanMakeMultipleRequestsToServer() throws Exception { server.enqueue(new MockResponse().setBody("a")); server.enqueue(new MockResponse().setBody("b")); client.interceptors().add(new Interceptor() { @Override/*from w ww . j a v a 2 s.c om*/ public Response intercept(Chain chain) throws IOException { chain.proceed(chain.request()); return chain.proceed(chain.request()); } }); Request request = new Request.Builder().url(server.url("/")).build(); Response response = client.newCall(request).execute(); assertEquals(response.body().string(), "b"); }