Search in sources :

Example 41 with RecordedRequest

use of com.google.mockwebserver.RecordedRequest in project j2objc by google.

the class URLConnectionTest method testRedirectToAnotherOriginServer.

public void testRedirectToAnotherOriginServer() throws Exception {
    MockWebServer server2 = new MockWebServer();
    server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
    server2.play();
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.getUrl("/").toString()).setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("This is the first server again!"));
    server.play();
    URLConnection connection = server.getUrl("/").openConnection();
    assertEquals("This is the 2nd server!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertEquals(server2.getUrl("/"), connection.getURL());
    // make sure the first server was careful to recycle the connection
    assertEquals("This is the first server again!", readAscii(server.getUrl("/").openStream(), Integer.MAX_VALUE));
    RecordedRequest first = server.takeRequest();
    assertContains(first.getHeaders(), "Host: " + hostName + ":" + server.getPort());
    RecordedRequest second = server2.takeRequest();
    assertContains(second.getHeaders(), "Host: " + hostName + ":" + server2.getPort());
    RecordedRequest third = server.takeRequest();
    //         assertEquals("Expected connection reuse", 1, third.getSequenceNumber());  // JVM failure
    server2.shutdown();
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 42 with RecordedRequest

use of com.google.mockwebserver.RecordedRequest in project robospice by stephanenicolas.

the class SpiceArrayAdapterTest method testGetView_fills_list_item_view_with_data_and_executes_request.

// I have 3 views on my adapter, name, number and photo
public void testGetView_fills_list_item_view_with_data_and_executes_request() throws IOException, InterruptedException {
    // given;
    byte[] data = IOUtils.toByteArray(getContext().getResources().openRawResource(R.raw.binary));
    mockWebServer.enqueue(new MockResponse().setBody(data));
    mockWebServer.play();
    // when
    View view = adapter.getView(0, null, null);
    adapter.await(ADAPTER_UPDATE_TIME_OUT);
    assertTrue(adapter.isLoadBitmapHasBeenCalled());
    // then
    TextView nameView = (TextView) view.findViewById(R.id.user_name_textview);
    ImageView photoView = (ImageView) view.findViewById(R.id.thumbnail_imageview);
    assertNotNull("View is null. ", view);
    assertNotNull("Name TextView is null. ", nameView);
    assertNotNull("Photo ImageView is null. ", photoView);
    assertEquals("Names doesn't match.", data1.getFoo(), nameView.getText());
    // could we get notified of this request ?
    assertEquals(1, mockWebServer.getRequestCount());
    RecordedRequest first = mockWebServer.takeRequest();
    assertEquals("GET /" + data1.getImageUrl() + " HTTP/1.1", first.getRequestLine());
    InputStream cacheInputStream = new FileInputStream(cacheFile);
    assertTrue(IOUtils.contentEquals(cacheInputStream, getContext().getResources().openRawResource(R.raw.binary)));
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TextView(android.widget.TextView) ImageView(android.widget.ImageView) ImageView(android.widget.ImageView) View(android.view.View) SpiceListItemView(com.octo.android.robospice.spicelist.SpiceListItemView) TextView(android.widget.TextView) FileInputStream(java.io.FileInputStream)

Example 43 with RecordedRequest

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

the class AbstractProxyTest method testParamPreferredOverSystemProperty.

private void testParamPreferredOverSystemProperty(ProxyConfig proxyConfig) throws Exception {
    server.enqueue(new MockResponse().setBody("Via request parameter proxy!"));
    server.play();
    System.setProperty("http.proxyHost", "proxy.foo");
    System.setProperty("http.proxyPort", "8080");
    HttpClient client = newHttpClient();
    HttpGet request = new HttpGet("http://origin.foo/bar");
    proxyConfig.configure(server, client, request);
    HttpResponse response = client.execute(request);
    assertEquals("Via request parameter proxy!", contentToString(response));
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET http://origin.foo/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)

Example 44 with RecordedRequest

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

the class AbstractProxyTest method testConnectViaHttpProxyToHttps.

private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via a secure proxy"));
    server.play();
    HttpClient httpProxyClient = newHttpClient();
    SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpProxyClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, 443));
    HttpGet request = new HttpGet("https://android.com/foo");
    proxyConfig.configure(server, httpProxyClient, request);
    HttpResponse response = httpProxyClient.execute(request);
    assertEquals("this response comes via a secure proxy", contentToString(response));
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy " + proxyConfig, "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
    assertContains(connect.getHeaders(), "Host: android.com");
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertContains(get.getHeaders(), "Host: android.com");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 45 with RecordedRequest

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

the class CookiesTest method testCookiesWithNonMatchingCase.

/**
     * Test that cookies aren't case-sensitive with respect to hostname.
     * http://b/3167208
     */
public void testCookiesWithNonMatchingCase() throws Exception {
    // use a proxy so we can manipulate the origin server's host name
    server = new MockWebServer();
    server.enqueue(new MockResponse().addHeader("Set-Cookie: a=first; Domain=my.t-mobile.com").addHeader("Set-Cookie: b=second; Domain=.T-mobile.com").addHeader("Set-Cookie: c=third; Domain=.t-mobile.com").setBody("This response sets some cookies."));
    server.enqueue(new MockResponse().setBody("This response gets those cookies back."));
    server.play();
    HttpClient client = new DefaultHttpClient();
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost("localhost", server.getPort()));
    HttpResponse getCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
    getCookies.getEntity().consumeContent();
    server.takeRequest();
    HttpResponse sendCookies = client.execute(new HttpGet("http://my.t-mobile.com/"));
    sendCookies.getEntity().consumeContent();
    RecordedRequest sendCookiesRequest = server.takeRequest();
    assertContains(sendCookiesRequest.getHeaders(), "Cookie: a=first; b=second; c=third");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) HttpHost(org.apache.http.HttpHost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

MockResponse (com.google.mockwebserver.MockResponse)80 RecordedRequest (com.google.mockwebserver.RecordedRequest)80 HttpResponse (org.apache.http.HttpResponse)36 HttpClient (org.apache.http.client.HttpClient)36 HttpGet (org.apache.http.client.methods.HttpGet)36 HttpURLConnection (java.net.HttpURLConnection)26 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)22 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)15 MockWebServer (com.google.mockwebserver.MockWebServer)14 Scheme (org.apache.http.conn.scheme.Scheme)12 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)12 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 OutputStream (java.io.OutputStream)11 GZIPOutputStream (java.util.zip.GZIPOutputStream)11 URL (java.net.URL)9 URLConnection (java.net.URLConnection)8 CookieManager (java.net.CookieManager)6 HttpHost (org.apache.http.HttpHost)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6