List of usage examples for com.squareup.okhttp ResponseBody create
public static ResponseBody create(final MediaType contentType, byte[] content)
From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java
License:Open Source License
@Test public void applicationInterceptorsCanShortCircuitResponses() throws Exception { server.get().shutdown(); // Accept no connections. Request request = new Request.Builder().url("https://localhost:1/").build(); final Response interceptorResponse = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1) .code(200).message("Intercepted!") .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build(); client.interceptors().add(new Interceptor() { @Override/*from w w w . j a va 2 s .co m*/ public Response intercept(Chain chain) throws IOException { return interceptorResponse; } }); Response response = FiberOkHttpUtil.executeInFiber(client, request); assertSame(interceptorResponse, response); }
From source file:co.paralleluniverse.fibers.okhttp.InterceptorTest.java
License:Open Source License
@Ignore @Test// w ww . j av a2s . com public void networkInterceptorsCannotShortCircuitResponses() throws Exception { server.enqueue(new MockResponse().setResponseCode(500)); Interceptor interceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(200) .message("Intercepted!") .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build(); } }; client.networkInterceptors().add(interceptor); Request request = new Request.Builder().url(server.getUrl("/")).build(); try { FiberOkHttpUtil.executeInFiber(client, request); fail(); } catch (IllegalStateException expected) { assertEquals("network interceptor " + interceptor + " must call proceed() exactly once", expected.getMessage()); } }
From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java
License:Apache License
@Test public void applicationInterceptorsCanShortCircuitResponses() throws Exception { server.shutdown(); // Accept no connections. Request request = new Request.Builder().url("https://localhost:1/").build(); final Response interceptorResponse = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1) .code(200).message("Intercepted!") .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build(); client.interceptors().add(new Interceptor() { @Override// ww w.j a v a2 s .c o m public Response intercept(Chain chain) throws IOException { return interceptorResponse; } }); Response response = client.newCall(request).execute(); assertSame(interceptorResponse, response); }
From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java
License:Apache License
@Ignore @Test/*from ww w .j ava 2 s.c o m*/ public void networkInterceptorsCannotShortCircuitResponses() throws Exception { server.enqueue(new MockResponse().setResponseCode(500)); Interceptor interceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(200) .message("Intercepted!") .body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build(); } }; client.networkInterceptors().add(interceptor); Request request = new Request.Builder().url(server.url("/")).build(); try { client.newCall(request).execute(); fail(); } catch (IllegalStateException expected) { assertEquals("network interceptor " + interceptor + " must call proceed() exactly once", expected.getMessage()); } }
From source file:com.carlospinan.demoretrofit2.helpers.LoggingInterceptor.java
License:Open Source License
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); Log.d(LOG_TAG, String.format("--> Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers()));/*from w w w .j a v a2s .c o m*/ Buffer requestBuffer = new Buffer(); if (request.body() != null) { request.body().writeTo(requestBuffer); Log.d(LOG_TAG, requestBuffer.readUtf8()); } Response response = chain.proceed(request); long t2 = System.nanoTime(); Log.d(LOG_TAG, String.format("<-- Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())); MediaType contentType = response.body().contentType(); String content = response.body().string(); Log.d(LOG_TAG, content); ResponseBody wrappedBody = ResponseBody.create(contentType, content); return response.newBuilder().body(wrappedBody).build(); }
From source file:com.dreamdigitizers.androidsoundcloudapi.core.Api.java
private Api(final String pClientId, final String pOauthToken) { OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.interceptors().add(new Interceptor() { @Override//from w w w. j av a 2 s . c o m public Response intercept(Chain pChain) throws IOException { Request request = pChain.request(); HttpUrl.Builder builder = request.httpUrl().newBuilder(); builder.addQueryParameter("client_id", pClientId); if (!TextUtils.isEmpty(pOauthToken)) { builder.addQueryParameter("oauth_token", pOauthToken); } HttpUrl httpUrl = builder.build(); Log.d(Api.TAG, httpUrl.url().toString()); request = request.newBuilder().url(httpUrl).build(); Response response = pChain.proceed(request); String bodyString = response.body().string(); response = response.newBuilder() .body(ResponseBody.create(response.body().contentType(), bodyString)).build(); Log.d(Api.TAG, bodyString); return response; } }); Retrofit retrofit = new Retrofit.Builder().baseUrl(IApi.API_URL__BASE).client(okHttpClient) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); this.mApi = retrofit.create(IApi.class); }
From source file:com.facebook.buck.artifact_cache.HttpArtifactCacheTest.java
License:Apache License
private ResponseBody createResponseBody(ImmutableSet<RuleKey> ruleKeys, ImmutableMap<String, String> metadata, ByteSource source, String data) throws IOException { try (ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(out)) { byte[] rawMetadata = HttpArtifactCacheBinaryProtocol.createMetadataHeader(ruleKeys, metadata, source); dataOut.writeInt(rawMetadata.length); dataOut.write(rawMetadata);//from w w w. j av a2 s . c o m dataOut.write(data.getBytes(Charsets.UTF_8)); return ResponseBody.create(OCTET_STREAM, out.toByteArray()); } }
From source file:com.facebook.buck.artifact_cache.HttpArtifactCacheTest.java
License:Apache License
@Test public void testFetchNotFound() throws Exception { final List<Response> responseList = Lists.newArrayList(); HttpArtifactCache cache = new HttpArtifactCache("http", null, null, new URI("http://localhost:8080"), /* doStore */ true, new FakeProjectFilesystem(), BUCK_EVENT_BUS) { @Override// www . j a v a 2s.c o m protected Response fetchCall(Request request) throws IOException { Response response = new Response.Builder().code(HttpURLConnection.HTTP_NOT_FOUND) .body(ResponseBody.create(MediaType.parse("application/octet-stream"), "extraneous")) .protocol(Protocol.HTTP_1_1).request(request).build(); responseList.add(response); return response; } }; CacheResult result = cache.fetch(new RuleKey("00000000000000000000000000000000"), Paths.get("output/file")); assertEquals(result.getType(), CacheResultType.MISS); assertTrue("response wasn't fully read!", responseList.get(0).body().source().exhausted()); cache.close(); }
From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java
License:Apache License
private ResponseBody createBody(HashCode code, String contents) throws IOException { return ResponseBody.create(MediaType.parse("application/octet-stream"), createArtifact(code, contents)); }
From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java
License:Apache License
private ResponseBody createBody(int length, String contents) throws IOException { return ResponseBody.create(MediaType.parse("application/octet-stream"), createArtifact(length, contents)); }