Search in sources :

Example 36 with MockResponse

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

the class CacheSwitcherTest method testCacheSwitching.

@Test
public void testCacheSwitching() throws IOException {
    final String response1 = "<response to be cached by the FIRST testCache>";
    final String response2 = "<response to be cached by the SECOND testCache>";
    final MockWebServer webServer = getWebServer();
    // two responses
    webServer.enqueue(new MockResponse().setBody(response1));
    webServer.enqueue(new MockResponse().setBody(response2));
    // first request, first cache => not cached
    perform("/1/", cache1, response1, false);
    // second request, first cache => cached
    perform("/1/", cache1, response1, true);
    // first request, second cache => not cached
    perform("/2/", cache2, response2, false);
    // second request, second cache => cached
    perform("/2/", cache2, response2, true);
}
Also used : MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) MockWebServer(com.squareup.okhttp.mockwebserver.MockWebServer) Test(org.junit.Test) AbstractMockServerTest(com.stanfy.enroscar.net.test.AbstractMockServerTest)

Example 37 with MockResponse

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

the class RequestDescriptionTest method makeGetConnectionShouldSendGoodParametersAndHeaders.

@Test
public void makeGetConnectionShouldSendGoodParametersAndHeaders() throws Exception {
    getWebServer().enqueue(new MockResponse().setBody("test response"));
    read(makeConnection(new MyRequestBuilder<String>(Robolectric.application) {
    }.setUrl(getWebServer().getUrl("/r1").toString()).addParam("p1", "v1").addParam("p2", "v2")));
    // check that request was as expected
    final RecordedRequest request = getWebServer().takeRequest();
    // url
    assertThat(request.getPath()).isEqualTo("/r1?p1=v1&p2=v2");
    // headers: language, gzip
    final String lang = Robolectric.application.getResources().getConfiguration().locale.getLanguage();
    assertThat(request.getHeaders().toMultimap()).containsEntry("Accept-Language: " + lang, Collections.singletonList("Accept-Encoding: gzip"));
}
Also used : RecordedRequest(com.squareup.okhttp.mockwebserver.RecordedRequest) MockResponse(com.squareup.okhttp.mockwebserver.MockResponse) Test(org.junit.Test) AbstractMockServerTest(com.stanfy.enroscar.net.test.AbstractMockServerTest)

Example 38 with MockResponse

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

the class ImageRequestTest method startServer.

@Override
public void startServer() throws IOException {
    super.startServer();
    server.enqueue(new MockResponse().setResponseCode(200).setBody(""));
    defaultUrl = server.getUrl("/").toString();
}
Also used : MockResponse(com.squareup.okhttp.mockwebserver.MockResponse)

Example 39 with MockResponse

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

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