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"));
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method applicationInterceptorReturnsNull.
@Test
public void applicationInterceptorReturnsNull() throws Exception {
server.enqueue(new MockResponse());
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
chain.proceed(chain.request());
return null;
}
};
client = client.newBuilder().addInterceptor(interceptor).build();
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (NullPointerException expected) {
assertEquals("interceptor " + interceptor + " returned null", expected.getMessage());
}
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method networkInterceptorsCannotChangeServerAddress.
@Test
public void networkInterceptorsCannotChangeServerAddress() throws Exception {
server.enqueue(new MockResponse().setResponseCode(500));
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Address address = chain.connection().route().address();
String sameHost = address.url().host();
int differentPort = address.url().port() + 1;
return chain.proceed(chain.request().newBuilder().url(HttpUrl.parse("http://" + sameHost + ":" + differentPort + "/")).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 retain the same host and port", expected.getMessage());
}
}
Aggregations