use of mockwebserver3.MockWebServer in project okhttp by square.
the class CacheTest method assertCached.
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse mockResponse = 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) {
mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
mockResponse.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.
mockResponse.setBody("");
}
server.enqueue(mockResponse);
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().setHeader("Cache-Control", "no-store").setBody("FGHIJ"));
}
server.start();
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
assertThat(response.code()).isEqualTo(expectedResponseCode);
// Exhaust the content stream.
response.body().string();
Response cached = cacheGet(cache, request);
if (shouldPut) {
assertThat(cached).isNotNull();
cached.body().close();
} else {
assertThat(cached).isNull();
}
// tearDown() isn't sufficient; this test starts multiple servers
server.shutdown();
}
use of mockwebserver3.MockWebServer in project okhttp by square.
the class CookiesTest method testRedirectsDoNotIncludeTooManyCookies.
@Test
public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
MockWebServer redirectTarget = new MockWebServer();
redirectTarget.enqueue(new MockResponse().setBody("A"));
redirectTarget.start();
HttpUrl redirectTargetUrl = urlWithIpAddress(redirectTarget, "/");
MockWebServer redirectSource = new MockWebServer();
redirectSource.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + redirectTargetUrl));
redirectSource.start();
HttpUrl redirectSourceUrl = urlWithIpAddress(redirectSource, "/");
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookie = new HttpCookie("c", "cookie");
cookie.setDomain(redirectSourceUrl.host());
cookie.setPath("/");
String portList = Integer.toString(redirectSource.getPort());
cookie.setPortlist(portList);
cookieManager.getCookieStore().add(redirectSourceUrl.uri(), cookie);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
get(redirectSourceUrl);
RecordedRequest request = redirectSource.takeRequest();
assertThat(request.getHeader("Cookie")).isEqualTo("c=cookie");
for (String header : redirectTarget.takeRequest().getHeaders().names()) {
if (header.startsWith("Cookie")) {
fail(header);
}
}
}
use of mockwebserver3.MockWebServer in project okhttp by square.
the class WholeOperationTimeoutTest method timeoutFollowingRedirectOnNewConnection.
@Test
public void timeoutFollowingRedirectOnNewConnection() throws Exception {
MockWebServer otherServer = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).setHeader("Location", otherServer.url("/")));
otherServer.enqueue(new MockResponse().setHeadersDelay(500, TimeUnit.MILLISECONDS));
Request request = new Request.Builder().url(server.url("/")).build();
Call call = client.newCall(request);
call.timeout().timeout(250, TimeUnit.MILLISECONDS);
try {
call.execute();
fail();
} catch (IOException e) {
assertThat(e.getMessage()).isEqualTo("timeout");
assertThat(call.isCanceled()).isTrue();
}
}
use of mockwebserver3.MockWebServer in project sonarlint-core by SonarSource.
the class MockWebServerExtension method beforeEach.
@Override
public void beforeEach(ExtensionContext context) throws Exception {
server = new MockWebServer();
responsesByPath.clear();
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (responsesByPath.containsKey(request.getPath())) {
return responsesByPath.get(request.getPath());
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
server.start();
}
use of mockwebserver3.MockWebServer in project okhttp by square.
the class EventListenerTest method redirectUsingNewConnectionEventSequence.
@Test
public void redirectUsingNewConnectionEventSequence() throws IOException {
MockWebServer otherServer = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + otherServer.url("/foo")));
otherServer.enqueue(new MockResponse());
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
call.execute();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
Aggregations