use of mockwebserver3.RecordedRequest in project okhttp by square.
the class InterceptorTest method multipleInterceptors.
private void multipleInterceptors(boolean network) throws Exception {
server.enqueue(new MockResponse());
addInterceptor(network, chain -> {
Request originalRequest = chain.request();
Response originalResponse = chain.proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", // 1. Added first.
"Android").build());
return originalResponse.newBuilder().addHeader("Response-Interceptor", // 4. Added last.
"Donut").build();
});
addInterceptor(network, chain -> {
Request originalRequest = chain.request();
Response originalResponse = chain.proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", // 2. Added second.
"Bob").build());
return originalResponse.newBuilder().addHeader("Response-Interceptor", // 3. Added third.
"Cupcake").build();
});
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
assertThat(response.headers("Response-Interceptor")).containsExactly("Cupcake", "Donut");
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getHeaders().values("Request-Interceptor")).containsExactly("Android", "Bob");
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class InterceptorTest method networkInterceptorsCanChangeRequestMethodFromGetToPost.
@Test
public void networkInterceptorsCanChangeRequestMethodFromGetToPost() throws Exception {
server.enqueue(new MockResponse());
Interceptor interceptor = chain -> {
Request originalRequest = chain.request();
MediaType mediaType = MediaType.get("text/plain");
RequestBody body = RequestBody.create("abc", mediaType);
return chain.proceed(originalRequest.newBuilder().method("POST", body).header("Content-Type", mediaType.toString()).header("Content-Length", Long.toString(body.contentLength())).build());
};
client = client.newBuilder().addNetworkInterceptor(interceptor).build();
Request request = new Request.Builder().url(server.url("/")).get().build();
client.newCall(request).execute();
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("POST");
assertThat(recordedRequest.getBody().readUtf8()).isEqualTo("abc");
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class InterceptorTest method rewriteRequestToServer.
private void rewriteRequestToServer(boolean network) throws Exception {
server.enqueue(new MockResponse());
addInterceptor(network, chain -> {
Request originalRequest = chain.request();
return chain.proceed(originalRequest.newBuilder().method("POST", uppercase(originalRequest.body())).addHeader("OkHttp-Intercepted", "yep").build());
});
Request request = new Request.Builder().url(server.url("/")).addHeader("Original-Header", "foo").method("PUT", RequestBody.create("abc", MediaType.get("text/plain"))).build();
client.newCall(request).execute();
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getBody().readUtf8()).isEqualTo("ABC");
assertThat(recordedRequest.getHeader("Original-Header")).isEqualTo("foo");
assertThat(recordedRequest.getHeader("OkHttp-Intercepted")).isEqualTo("yep");
assertThat(recordedRequest.getMethod()).isEqualTo("POST");
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CookiesTest method testSendingCookiesFromStore.
@Test
public void testSendingCookiesFromStore() throws Exception {
server.enqueue(new MockResponse());
HttpUrl serverUrl = urlWithIpAddress(server, "/");
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookieA = new HttpCookie("a", "android");
cookieA.setDomain(serverUrl.host());
cookieA.setPath("/");
cookieManager.getCookieStore().add(serverUrl.uri(), cookieA);
HttpCookie cookieB = new HttpCookie("b", "banana");
cookieB.setDomain(serverUrl.host());
cookieB.setPath("/");
cookieManager.getCookieStore().add(serverUrl.uri(), cookieB);
client = client.newBuilder().cookieJar(new JavaNetCookieJar(cookieManager)).build();
get(serverUrl);
RecordedRequest request = server.takeRequest();
assertThat(request.getHeader("Cookie")).isEqualTo("a=android; b=banana");
}
use of mockwebserver3.RecordedRequest 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);
}
}
}
Aggregations