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();
}
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;
}
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));
}
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");
}
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);
}
Aggregations