use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method networkInterceptorReturnsConnectionOnEmptyBody.
@Test
public void networkInterceptorReturnsConnectionOnEmptyBody() throws Exception {
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_END).addHeader("Connection", "Close"));
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response = chain.proceed(chain.request());
assertNotNull(chain.connection());
return response;
}
};
client = client.newBuilder().addNetworkInterceptor(interceptor).build();
Request request = new Request.Builder().url(server.url("/")).build();
Response response = client.newCall(request).execute();
response.body().close();
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method rewriteRequestToServer.
private void rewriteRequestToServer(boolean network) throws Exception {
server.enqueue(new MockResponse());
addInterceptor(network, new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
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(MediaType.parse("text/plain"), "abc")).build();
client.newCall(request).execute();
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("ABC", recordedRequest.getBody().readUtf8());
assertEquals("foo", recordedRequest.getHeader("Original-Header"));
assertEquals("yep", recordedRequest.getHeader("OkHttp-Intercepted"));
assertEquals("POST", recordedRequest.getMethod());
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method asyncInterceptors.
private void asyncInterceptors(boolean network) throws Exception {
server.enqueue(new MockResponse());
addInterceptor(network, new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().addHeader("OkHttp-Intercepted", "yep").build();
}
});
Request request = new Request.Builder().url(server.url("/")).build();
client.newCall(request).enqueue(callback);
callback.await(request.url()).assertCode(200).assertHeader("OkHttp-Intercepted", "yep");
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method networkInterceptorsCannotShortCircuitResponses.
@Test
public void networkInterceptorsCannotShortCircuitResponses() throws Exception {
server.enqueue(new MockResponse().setResponseCode(500));
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
return new Response.Builder().request(chain.request()).protocol(Protocol.HTTP_1_1).code(200).message("Intercepted!").body(ResponseBody.create(MediaType.parse("text/plain; charset=utf-8"), "abc")).build();
}
};
client = client.newBuilder().addNetworkInterceptor(interceptor).build();
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (IllegalStateException expected) {
assertEquals("network interceptor " + interceptor + " must call proceed() exactly once", expected.getMessage());
}
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method multipleInterceptors.
private void multipleInterceptors(boolean network) throws Exception {
server.enqueue(new MockResponse());
addInterceptor(network, new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
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, new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
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();
assertEquals(Arrays.asList("Cupcake", "Donut"), response.headers("Response-Interceptor"));
RecordedRequest recordedRequest = server.takeRequest();
assertEquals(Arrays.asList("Android", "Bob"), recordedRequest.getHeaders().values("Request-Interceptor"));
}
Aggregations