Search in sources :

Example 26 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.

the class SampleServer method run.

public void run() throws IOException {
    MockWebServer server = new MockWebServer();
    server.useHttps(sslContext.getSocketFactory(), false);
    server.setDispatcher(this);
    server.start(port);
}
Also used : MockWebServer(okhttp3.mockwebserver.MockWebServer)

Example 27 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.

the class OkUrlFactoryTest method testURLFilterRedirect.

@Test
public void testURLFilterRedirect() throws Exception {
    MockWebServer cleartextServer = new MockWebServer();
    cleartextServer.enqueue(new MockResponse().setBody("Blocked!"));
    final URL blockedURL = cleartextServer.url("/").url();
    SslClient contextBuilder = SslClient.localhost();
    server.useHttps(contextBuilder.socketFactory, false);
    factory.setClient(factory.client().newBuilder().sslSocketFactory(contextBuilder.socketFactory, contextBuilder.trustManager).followSslRedirects(true).build());
    factory.setUrlFilter(new URLFilter() {

        @Override
        public void checkURLPermitted(URL url) throws IOException {
            if (blockedURL.equals(url)) {
                throw new IOException("Blocked");
            }
        }
    });
    server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: " + blockedURL).setBody("This page has moved"));
    URL destination = server.url("/").url();
    try {
        HttpsURLConnection httpsConnection = (HttpsURLConnection) factory.open(destination);
        httpsConnection.getInputStream();
        fail("Connection was successful");
    } catch (IOException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) URLFilter(okhttp3.internal.URLFilter) MockWebServer(okhttp3.mockwebserver.MockWebServer) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 28 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project okhttp by square.

the class UrlConnectionCacheTest method assertCached.

private void assertCached(boolean shouldPut, int responseCode) throws Exception {
    int expectedResponseCode = responseCode;
    server = new MockWebServer();
    MockResponse response = new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setResponseCode(responseCode).setBody("ABCDE").addHeader("WWW-Authenticate: challenge");
    if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
        response.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
    } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        response.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
    } else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT || responseCode == HttpURLConnection.HTTP_RESET) {
        // We forbid bodies for 204 and 205.
        response.setBody("");
    }
    server.enqueue(response);
    if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
        // 408's are a bit of an outlier because we may repeat the request if we encounter this
        // response code. In this scenario, there are 2 responses: the initial 408 and then the 200
        // because of the retry. We just want to ensure the initial 408 isn't cached.
        expectedResponseCode = 200;
        server.enqueue(new MockResponse().addHeader("Cache-Control", "no-store").setBody("FGHIJ"));
    }
    server.start();
    URL url = server.url("/").url();
    HttpURLConnection conn = urlFactory.open(url);
    assertEquals(expectedResponseCode, conn.getResponseCode());
    // exhaust the content stream
    readAscii(conn);
    Response cached = cache.get(new Request.Builder().url(url).build());
    if (shouldPut) {
        assertNotNull(Integer.toString(responseCode), cached);
        cached.body().close();
    } else {
        assertNull(Integer.toString(responseCode), cached);
    }
    // tearDown() isn't sufficient; this test starts multiple servers
    server.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) MockWebServer(okhttp3.mockwebserver.MockWebServer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) URL(java.net.URL)

Example 29 with MockWebServer

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

the class HttpUrlFetcherServerTest method testSetsReadTimeout.

@Test
public void testSetsReadTimeout() throws Exception {
    MockWebServer tempWebServer = new MockWebServer();
    tempWebServer.enqueue(new MockResponse().setBody("test").throttleBody(1, TIMEOUT_TIME_MS, TimeUnit.MILLISECONDS));
    tempWebServer.start();
    try {
        getFetcher().loadData(Priority.HIGH, callback);
    } finally {
        tempWebServer.shutdown();
        // shutdown() called before any enqueue() blocks until it times out.
        mockWebServer.enqueue(new MockResponse().setResponseCode(200));
    }
    verify(callback).onLoadFailed(isA(IOException.class));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) IOException(java.io.IOException) Test(org.junit.Test)

Example 30 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project graylog2-server by Graylog2.

the class HTTPAlarmCallbackTest method setUp.

@Before
public void setUp() throws Exception {
    httpClient = new OkHttpClient();
    objectMapper = new ObjectMapperProvider().get();
    alarmCallback = new HTTPAlarmCallback(httpClient, objectMapper);
    server = new MockWebServer();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) ObjectMapperProvider(org.graylog2.shared.bindings.providers.ObjectMapperProvider) Before(org.junit.Before)

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