Search in sources :

Example 11 with MockResponse

use of com.squareup.okhttp.mockwebserver.MockResponse 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 12 with MockResponse

use of com.squareup.okhttp.mockwebserver.MockResponse in project enroscar by stanfy.

the class ImageRequestAndroidTest method prepareServer.

private MockWebServer prepareServer() throws IOException {
    MockWebServer server = new MockWebServer();
    server.start();
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    testBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    server.enqueue(new MockResponse().setResponseCode(200).setBody(new Buffer().write(bytes.toByteArray())));
    return server;
}
Also used : Buffer(okio.Buffer) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 13 with MockResponse

use of com.squareup.okhttp.mockwebserver.MockResponse in project enroscar by stanfy.

the class JsonRequestTest method enqueueResponse.

void enqueueResponse(final MyModel model) {
    final Gson gson = new GsonBuilder().create();
    final String text = gson.toJson(model);
    assertThat(text).contains("shouldParseJson");
    getWebServer().enqueue(new MockResponse().setBody(text));
}
Also used : MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 14 with MockResponse

use of com.squareup.okhttp.mockwebserver.MockResponse in project enroscar by stanfy.

the class RequestDescriptionTest method makePostConnectionShouldReceiveCorrectResponse.

@Test
public void makePostConnectionShouldReceiveCorrectResponse() throws Exception {
    getWebServer().enqueue(new MockResponse().setBody("POST response"));
    final URLConnection connection = makeConnection(new MyRequestBuilder<String>(Robolectric.application) {
    }.setUrl(getWebServer().getUrl("/post").toString()).addParam("p1", "v1").setOperationType(OperationType.SIMPLE_POST));
    assertThat(ResponseCache.getDefault()).isNull();
    final String response = read(connection);
    final RecordedRequest request = getWebServer().takeRequest();
    assertThat(request.getMethod()).isEqualTo("POST");
    assertThat(request.getBody().readUtf8()).isEqualTo("p1=v1");
    final HttpURLConnection http = (HttpURLConnection) UrlConnectionWrapper.unwrap(connection);
    assertThat(http.getResponseCode()).isEqualTo(HttpURLConnection.HTTP_OK);
    assertThat(response).isEqualTo("POST response");
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Test(org.junit.Test) AbstractMockServerTest(com.stanfy.enroscar.net.test.AbstractMockServerTest)

Example 15 with MockResponse

use of com.squareup.okhttp.mockwebserver.MockResponse in project enroscar by stanfy.

the class CompressedResponseTest method testCompressedResponse.

@Test
public void testCompressedResponse() throws IOException {
    final String text = "Some response text; Some response text; Some response text.";
    final byte[] zipped = getZippedText(text);
    // it's really zipped
    assertThat(zipped).isNotEqualTo(text.getBytes(IoUtils.UTF_8_NAME));
    getWebServer().enqueue(new MockResponse().setBody(new Buffer().write(zipped)).setHeader("Content-Encoding", "gzip"));
    final URL url = getWebServer().getUrl("/");
    // response is unzipped
    assertResponse(url.openConnection(), text, false);
    // response is correctly read from the cache
    assertResponse(url.openConnection(), text, true);
}
Also used : Buffer(okio.Buffer) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) URL(java.net.URL) Test(org.junit.Test) AbstractOneCacheTest(com.stanfy.enroscar.net.test.cache.AbstractOneCacheTest)

Aggregations

MockResponse (com.squareup.okhttp.mockwebserver.MockResponse)39 Test (org.junit.Test)34 Path (org.apache.hadoop.fs.Path)18 MockWebServer (com.squareup.okhttp.mockwebserver.MockWebServer)10 FileStatus (org.apache.hadoop.fs.FileStatus)6 AclEntry (org.apache.hadoop.fs.permission.AclEntry)5 RecordedRequest (com.squareup.okhttp.mockwebserver.RecordedRequest)4 AbstractMockServerTest (com.stanfy.enroscar.net.test.AbstractMockServerTest)4 ArrayList (java.util.ArrayList)4 Buffer (okio.Buffer)4 HttpUrl (com.squareup.okhttp.HttpUrl)3 URL (java.net.URL)3 URLConnection (java.net.URLConnection)3 Request (com.squareup.okhttp.Request)2 Response (com.squareup.okhttp.Response)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 URI (java.net.URI)2