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);
}
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) {
}
}
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();
}
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));
}
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();
}
Aggregations