List of usage examples for com.squareup.okhttp ResponseBody create
public static ResponseBody create(final MediaType contentType, byte[] content)
From source file:com.facebook.buck.rules.HttpArtifactCacheTest.java
License:Apache License
private ResponseBody createBody(String contents) throws IOException { return ResponseBody.create(MediaType.parse("application/octet-stream"), createArtifact(contents)); }
From source file:com.facebook.stetho.okhttp.StethoInterceptorTest.java
License:Open Source License
@Test public void testHappyPath() throws IOException { InOrder inOrder = Mockito.inOrder(mMockEventReporter); hookAlmostRealRequestWillBeSent(mMockEventReporter); ByteArrayOutputStream capturedOutput = hookAlmostRealInterpretResponseStream(mMockEventReporter); Uri requestUri = Uri.parse("http://www.facebook.com/nowhere"); String requestText = "Test input"; Request request = new Request.Builder().url(requestUri.toString()) .method("POST", RequestBody.create(MediaType.parse("text/plain"), requestText)).build(); String originalBodyData = "Success!"; Response reply = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(200) .body(ResponseBody.create(MediaType.parse("text/plain"), originalBodyData)).build(); Response filteredResponse = mInterceptor.intercept(new SimpleTestChain(request, reply, null)); inOrder.verify(mMockEventReporter).isEnabled(); inOrder.verify(mMockEventReporter).requestWillBeSent(any(NetworkEventReporter.InspectorRequest.class)); inOrder.verify(mMockEventReporter).dataSent(anyString(), eq(requestText.length()), eq(requestText.length()));/*w ww . ja v a 2 s .c o m*/ inOrder.verify(mMockEventReporter) .responseHeadersReceived(any(NetworkEventReporter.InspectorResponse.class)); String filteredResponseString = filteredResponse.body().string(); String interceptedOutput = capturedOutput.toString(); inOrder.verify(mMockEventReporter).dataReceived(anyString(), anyInt(), anyInt()); inOrder.verify(mMockEventReporter).responseReadFinished(anyString()); assertEquals(originalBodyData, filteredResponseString); assertEquals(originalBodyData, interceptedOutput); inOrder.verifyNoMoreInteractions(); }
From source file:com.kubeiwu.easyandroid.easyhttp.core.retrofit.KOkHttpCall.java
License:Apache License
private Response<T> execCacheRequest(Request request) { if (responseConverter instanceof GsonConverter) { GsonConverter<T> converter = (GsonConverter<T>) responseConverter; Cache cache = converter.getCache(); if (cache == null) { return null; }/*ww w.ja v a 2s . c o m*/ Entry entry = cache.get(request.urlString());// ?entry if (entry == null) { return null; } if (entry.isExpired()) {// return null; } if (entry.data != null) {// ? MediaType contentType = MediaType.parse(entry.mimeType); byte[] bytes = entry.data; try { com.squareup.okhttp.Response rawResponse = new com.squareup.okhttp.Response.Builder()// .code(200).request(request).protocol(Protocol.HTTP_1_1) .body(ResponseBody.create(contentType, bytes)).build(); return parseResponse(rawResponse, request); } catch (Exception e) { e.printStackTrace(); } } } return null; }
From source file:com.magnet.max.android.rest.qos.internal.CachedResponse.java
License:Apache License
public Response toResponse(Request request) { Headers responseHeaders = Headers.of(headers); String contentType = responseHeaders.get("Content-Type"); Response cachedResponse = new Response.Builder().code(code).protocol(getProtocolEnum()).message(message) .headers(responseHeaders).request(request).build(); return cachedResponse.newBuilder().body(ResponseBody.create(MediaType.parse(contentType), body)) .cacheResponse(cachedResponse).build(); }
From source file:com.magnet.max.android.rest.qos.internal.CacheManager.java
License:Apache License
public Response cacheResponse(Request request, Response response, CacheOptions options) { String requestHash = CacheUtils.getRequestHash(request); ResponseCacheEntity operation = findLatestCache(requestHash, request, options); long currentTimestamp = System.currentTimeMillis(); if (null == operation) { operation = new ResponseCacheEntity(); operation.createdAt = currentTimestamp; operation.url = request.urlString(); operation.httpMethod = request.method(); operation.requestHash = requestHash; operation.response = new CachedResponse(response); operation.responseCode = response.code(); operation.isOfflineCache = options.isAlwaysUseCacheIfOffline(); Log.d(TAG, "Adding cache for request " + request); } else {/*from www . jav a 2s . c o m*/ //Update body operation.response = new CachedResponse(response); Log.d(TAG, "Updating cache for request " + request); } operation.updatedAt = currentTimestamp; long newExpiredTime = 0; if (options.getMaxCacheAge() > 0) { newExpiredTime = currentTimestamp + options.getMaxCacheAge() * 1000; } if (null == operation.getExpiredAt() || newExpiredTime > operation.getExpiredAt()) { operation.expiredAt = newExpiredTime; } operation.save(); if (null != response.body()) { return response.newBuilder() .body(ResponseBody.create(response.body().contentType(), operation.response.body)).build(); } else { return response; } }
From source file:com.magnet.max.android.tests.MagnetGsonConverterTest.java
License:Apache License
@SmallTest public void testDateTypeUnmarshalling() { Converter magnetGsonConverter = magnetGsonConverterFactory.get(Date.class); String str = "2015-01-01T01:01:00.000Z"; try {/*www. j a v a 2 s . c o m*/ Date date = (Date) magnetGsonConverter .fromBody(ResponseBody.create(MediaType.parse("application/json"), str)); assertNotNull(date); Calendar cal = Calendar.getInstance(); cal.setTime(date); assertEquals(2015, cal.get(Calendar.YEAR)); assertEquals(0, cal.get(Calendar.MONTH)); assertEquals(1, cal.get(Calendar.DAY_OF_MONTH)); assertEquals(1, cal.get(Calendar.HOUR_OF_DAY)); assertEquals(1, cal.get(Calendar.MINUTE)); assertEquals(0, cal.get(Calendar.SECOND)); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } }
From source file:com.magnet.max.android.tests.MagnetGsonConverterTest.java
License:Apache License
@SmallTest public void testStringTypeUnmarshalling() { Converter magnetGsonConverter = magnetGsonConverterFactory.get(String.class); String str = "hello world"; try {//from ww w .j a v a 2 s .co m String value = (String) magnetGsonConverter .fromBody(ResponseBody.create(MediaType.parse("application/json"), str)); assertEquals(str, value); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } }
From source file:com.magnet.max.android.tests.MagnetGsonConverterTest.java
License:Apache License
@SmallTest public void testEnumTypeUnmarshalling() { Converter magnetGsonConverter = magnetGsonConverterFactory.get(TestEnumType.class); try {//from w w w . j av a 2s . co m TestEnumType enumType = (TestEnumType) magnetGsonConverter .fromBody(ResponseBody.create(MediaType.parse("application/json"), "ENUM1")); assertEquals(TestEnumType.ENUM1, enumType); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } }
From source file:com.magnet.max.android.tests.MagnetGsonConverterTest.java
License:Apache License
@SmallTest public void testEnumTypeWithDoubleQuoteUnmarshalling() { Converter magnetGsonConverter = magnetGsonConverterFactory.get(TestEnumType.class); try {//www . jav a2 s. c om TestEnumType enumType = (TestEnumType) magnetGsonConverter .fromBody(ResponseBody.create(MediaType.parse("application/json"), "\"ENUM1\"")); assertEquals(TestEnumType.ENUM1, enumType); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } }
From source file:com.magnet.max.android.tests.MagnetGsonConverterTest.java
License:Apache License
@SmallTest public void testEnumListUnmarshalling() { Converter magnetGsonConverter = magnetGsonConverterFactory.get(new TypeToken<ArrayList<TestEnumType>>() { }.getType());/* ww w . j a va2 s. c o m*/ try { List<TestEnumType> enums = (List<TestEnumType>) magnetGsonConverter .fromBody(ResponseBody.create(MediaType.parse("application/json"), "[\"ENUM1\",\"ENUM2\"]")); assertEquals(2, enums.size()); assertEquals(TestEnumType.ENUM1, enums.get(0)); assertEquals(TestEnumType.ENUM2, enums.get(1)); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } }