Search in sources :

Example 36 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project glide by bumptech.

the class VolleyStreamFetcherServerTest method setUp.

@Before
public void setUp() throws IOException {
    MockitoAnnotations.initMocks(this);
    waitForResponseLatch = new CountDownLatch(1);
    doAnswer(new CountDown()).when(callback).onDataReady(any(InputStream.class));
    doAnswer(new CountDown()).when(callback).onLoadFailed(any(Exception.class));
    requestQueue = Volley.newRequestQueue(RuntimeEnvironment.application);
    mockWebServer = new MockWebServer();
    mockWebServer.start();
    streamCaptor = ArgumentCaptor.forClass(InputStream.class);
}
Also used : InputStream(java.io.InputStream) MockWebServer(okhttp3.mockwebserver.MockWebServer) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Before(org.junit.Before)

Example 37 with MockWebServer

use of okhttp3.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.url("/")).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(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with MockWebServer

use of okhttp3.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(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) Request(okhttp3.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project sonarqube by SonarSource.

the class HttpConnectorTest method setUp.

@Before
public void setUp() throws Exception {
    server = new MockWebServer();
    server.start();
    serverUrl = server.url("").url().toString();
}
Also used : MockWebServer(okhttp3.mockwebserver.MockWebServer) Before(org.junit.Before)

Example 40 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project scribejava by scribejava.

the class OkHttpHttpClientTest method shouldReadResponseStream.

@Test
public void shouldReadResponseStream() throws Exception {
    final String expectedResponseBody = "response body";
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setBody(expectedResponseBody));
    server.start();
    final HttpUrl baseUrl = server.url("/testUrl");
    final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
    final Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
    assertEquals(expectedResponseBody, StreamUtils.getStreamContents(response.getStream()));
    final RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET", recordedRequest.getMethod());
    server.shutdown();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Aggregations

MockWebServer (okhttp3.mockwebserver.MockWebServer)48 MockResponse (okhttp3.mockwebserver.MockResponse)31 Test (org.junit.Test)20 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 Before (org.junit.Before)14 IOException (java.io.IOException)9 CookieManager (java.net.CookieManager)7 HttpCookie (java.net.HttpCookie)5 Retrofit (retrofit2.Retrofit)5 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 Request (okhttp3.Request)4 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)3 Response (com.github.scribejava.core.model.Response)3 InputStream (java.io.InputStream)3 InterruptedIOException (java.io.InterruptedIOException)3 HttpURLConnection (java.net.HttpURLConnection)3 URI (java.net.URI)3 URL (java.net.URL)3 List (java.util.List)3