Search in sources :

Example 6 with MockWebServer

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();
}
Also used : Buffer(okio.Buffer) Response(com.squareup.okhttp.Response) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) Request(com.squareup.okhttp.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with MockWebServer

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();
}
Also used : Response(com.squareup.okhttp.Response) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) Request(com.squareup.okhttp.Request) RequestBody(com.squareup.okhttp.RequestBody) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with MockWebServer

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();
}
Also used : Buffer(okio.Buffer) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer)

Example 9 with MockWebServer

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();
}
Also used : MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer)

Example 10 with MockWebServer

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();
}
Also used : MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer)

Aggregations

MockWebServer (com.squareup.okhttp.mockwebserver.MockWebServer)13 MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)6 Before (org.junit.Before)4 Test (org.junit.Test)4 Buffer (okio.Buffer)3 Request (com.squareup.okhttp.Request)2 Response (com.squareup.okhttp.Response)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Bitmap (android.graphics.Bitmap)1 RequestBody (com.squareup.okhttp.RequestBody)1 AbstractMockServerTest (com.stanfy.enroscar.net.test.AbstractMockServerTest)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URL (java.net.URL)1 RestAdapter (retrofit.RestAdapter)1