use of com.squareup.okhttp.mockwebserver.MockWebServer in project stetho by facebook.
the class StethoInterceptorTest method testWithResponseCompression.
@Test
public void testWithResponseCompression() throws IOException {
ByteArrayOutputStream capturedOutput = hookAlmostRealInterpretResponseStream(mMockEventReporter);
byte[] uncompressedData = repeat(".", 1024).getBytes();
byte[] compressedData = compress(uncompressedData);
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse().setBody(new Buffer().write(compressedData)).addHeader("Content-Encoding: gzip"));
Request request = new Request.Builder().url(server.url("/")).build();
Response response = mClientWithInterceptor.newCall(request).execute();
// Verify that the final output and the caller both saw the uncompressed stream.
assertArrayEquals(uncompressedData, response.body().bytes());
assertArrayEquals(uncompressedData, capturedOutput.toByteArray());
// And verify that the StethoInterceptor was able to see both.
Mockito.verify(mMockEventReporter).dataReceived(anyString(), eq(compressedData.length), eq(uncompressedData.length));
server.shutdown();
}
use of com.squareup.okhttp.mockwebserver.MockWebServer in project stetho by facebook.
the class StethoInterceptorTest method testWithRequestCompression.
@Test
public void testWithRequestCompression() throws IOException {
AtomicReference<NetworkEventReporter.InspectorRequest> capturedRequest = hookAlmostRealRequestWillBeSent(mMockEventReporter);
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse().setBody("Success!"));
final byte[] decompressed = "Request text".getBytes();
final byte[] compressed = compress(decompressed);
assertNotEquals("Bogus test: decompressed and compressed lengths match", compressed.length, decompressed.length);
RequestBody compressedBody = RequestBody.create(MediaType.parse("text/plain"), compress(decompressed));
Request request = new Request.Builder().url(server.getUrl("/")).addHeader("Content-Encoding", "gzip").post(compressedBody).build();
Response response = mClientWithInterceptor.newCall(request).execute();
// Force a read to complete the flow.
response.body().string();
assertArrayEquals(decompressed, capturedRequest.get().body());
Mockito.verify(mMockEventReporter).dataSent(anyString(), eq(decompressed.length), eq(compressed.length));
server.shutdown();
}
use of com.squareup.okhttp.mockwebserver.MockWebServer in project enroscar by stanfy.
the class ImageRequestAndroidTest method testRealRemoteImage.
public void testRealRemoteImage() throws IOException {
MockWebServer server = new MockWebServer();
server.start();
final InputStream imageStream = getContext().getResources().getAssets().open("mustard.jpg");
final byte[] imageBytes = new byte[imageStream.available()];
assertThat(imageBytes.length).isNotZero();
imageStream.read(imageBytes);
IoUtils.closeQuietly(imageStream);
server.enqueue(new MockResponse().setResponseCode(200).setBody(new Buffer().write(imageBytes)));
String url = server.getUrl("/image").toString();
ImageRequest request = new ImageRequest(imagesManager, url, 1);
ImageResult result = request.readImage();
assertThat(result.getType()).isSameAs(ImageSourceType.NETWORK);
final Bitmap resultBitmap = result.getBitmap();
assertThat(resultBitmap).isNotNull();
}
use of com.squareup.okhttp.mockwebserver.MockWebServer in project enroscar by stanfy.
the class ImageRequestAndroidTest method testCaching.
public void testCaching() throws IOException {
MockWebServer server = prepareServer();
String url = server.getUrl("/image").toString();
ImageResult result = new ImageRequest(imagesManager, url, -1).readImage();
checkBitmap(result);
assertThat(result.getType()).isSameAs(ImageSourceType.NETWORK);
ImageResult result2 = new ImageRequest(imagesManager, url, -1).readImage();
checkBitmap(result2);
assertThat(result2.getType()).isSameAs(ImageSourceType.DISK);
server.shutdown();
}
use of com.squareup.okhttp.mockwebserver.MockWebServer in project enroscar by stanfy.
the class ImageRequestAndroidTest method testStoreToDiskBigRatio.
public void testStoreToDiskBigRatio() throws IOException {
MockWebServer server = prepareServer();
String url = server.getUrl("/image").toString();
ImageRequest request = new ImageRequest(imagesManager, url, 1);
assertThat(imagesManager.isPresentOnDisk(url)).isFalse();
request.storeToDisk();
assertThat(imagesManager.isPresentOnDisk(url)).isTrue();
server.shutdown();
}
Aggregations