Search in sources :

Example 46 with MockResponse

use of com.google.mockwebserver.MockResponse in project ribbon by Netflix.

the class RibbonTest method testCacheMiss.

@Test
public void testCacheMiss() throws IOException, InterruptedException {
    MockWebServer server = new MockWebServer();
    String content = "Hello world";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content));
    server.play();
    HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withConfigurationBasedServerList("localhost:" + server.getPort()).withMaxAutoRetriesNextServer(1));
    final String cacheKey = "somekey";
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test").withCacheProvider(cacheKey, new CacheProvider<ByteBuf>() {

        @Override
        public Observable<ByteBuf> get(String key, Map<String, Object> vars) {
            return Observable.error(new Exception("Cache miss again"));
        }
    }).withMethod("GET").withUriTemplate("/").build();
    RibbonRequest<ByteBuf> request = template.requestBuilder().build();
    String result = toStringBlocking(request);
    assertEquals(content, result);
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 47 with MockResponse

use of com.google.mockwebserver.MockResponse in project ribbon by Netflix.

the class RibbonTest method testCommand.

@Test
public void testCommand() throws IOException, InterruptedException, ExecutionException {
    MockWebServer server = new MockWebServer();
    String content = "Hello world";
    MockResponse response = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content);
    server.enqueue(response);
    server.enqueue(response);
    server.enqueue(response);
    server.play();
    HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withMaxAutoRetriesNextServer(3).withReadTimeout(300000).withConfigurationBasedServerList("localhost:12345, localhost:10092, localhost:" + server.getPort()));
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("/").withMethod("GET").build();
    RibbonRequest<ByteBuf> request = template.requestBuilder().build();
    String result = request.execute().toString(Charset.defaultCharset());
    assertEquals(content, result);
    // repeat the same request
    ByteBuf raw = request.execute();
    result = raw.toString(Charset.defaultCharset());
    raw.release();
    assertEquals(content, result);
    result = request.queue().get().toString(Charset.defaultCharset());
    assertEquals(content, result);
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 48 with MockResponse

use of com.google.mockwebserver.MockResponse in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerBaseTest method buildResponse.

/**
     * Helper to build a response from the MockWebServer with no body.
     *
     * @param status The HTTP status code to return for this response
     * @return Returns the mock web server response that was queued (which can be modified)
     */
protected MockResponse buildResponse(int status) {
    MockResponse response = new MockResponse().setResponseCode(status);
    response.setHeader("Content-type", mFileType);
    return response;
}
Also used : MockResponse(com.google.mockwebserver.MockResponse)

Example 49 with MockResponse

use of com.google.mockwebserver.MockResponse in project android_frameworks_base by ParanoidAndroid.

the class DownloadManagerFunctionalTest method testErrorHttpDataError_invalidRedirect.

/**
     * Tests the download failure error from an unhandled HTTP status code
     */
@LargeTest
public void testErrorHttpDataError_invalidRedirect() throws Exception {
    Uri uri = getServerUri(DEFAULT_FILENAME);
    final MockResponse resp = buildResponse(HTTP_REDIRECT);
    resp.setHeader("Location", "://blah.blah.blah.com");
    enqueueResponse(resp);
    doErrorTest(uri, DownloadManager.ERROR_HTTP_DATA_ERROR);
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Uri(android.net.Uri) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 50 with MockResponse

use of com.google.mockwebserver.MockResponse in project android_frameworks_base by ParanoidAndroid.

the class AbstractProxyTest method testExplicitNoProxyCancelsSystemProperty.

public void testExplicitNoProxyCancelsSystemProperty() throws Exception {
    server.enqueue(new MockResponse().setBody("Via the origin server!"));
    server.play();
    System.setProperty("http.proxyHost", "proxy.foo");
    System.setProperty("http.proxyPort", "8080");
    HttpClient client = newHttpClient();
    HttpGet request = new HttpGet(server.getUrl("/bar").toURI());
    request.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, ConnRouteParams.NO_HOST);
    HttpResponse response = client.execute(request);
    assertEquals("Via the origin server!", contentToString(response));
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET /bar HTTP/1.1", recordedRequest.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse)

Aggregations

MockResponse (com.google.mockwebserver.MockResponse)240 HttpURLConnection (java.net.HttpURLConnection)89 RecordedRequest (com.google.mockwebserver.RecordedRequest)80 HttpGet (org.apache.http.client.methods.HttpGet)54 HttpClient (org.apache.http.client.HttpClient)48 HttpResponse (org.apache.http.HttpResponse)42 MockWebServer (com.google.mockwebserver.MockWebServer)39 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)36 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)32 URLConnection (java.net.URLConnection)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)23 LargeTest (android.test.suitebuilder.annotation.LargeTest)18 GZIPInputStream (java.util.zip.GZIPInputStream)18 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)18 OutputStream (java.io.OutputStream)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)17 CookieManager (java.net.CookieManager)14 URL (java.net.URL)14