use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method networkInterceptorModifiedRequestIsReturned.
@Test
public void networkInterceptorModifiedRequestIsReturned() throws IOException {
server.enqueue(new MockResponse());
Interceptor modifyHeaderInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
return chain.proceed(chain.request().newBuilder().header("User-Agent", "intercepted request").build());
}
};
client = client.newBuilder().addNetworkInterceptor(modifyHeaderInterceptor).build();
Request request = new Request.Builder().url(server.url("/")).header("User-Agent", "user request").build();
Response response = client.newCall(request).execute();
assertNotNull(response.request().header("User-Agent"));
assertEquals("user request", response.request().header("User-Agent"));
assertEquals("intercepted request", response.networkResponse().request().header("User-Agent"));
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method networkInterceptorsHaveConnectionAccess.
@Test
public void networkInterceptorsHaveConnectionAccess() throws Exception {
server.enqueue(new MockResponse());
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Connection connection = chain.connection();
assertNotNull(connection);
return chain.proceed(chain.request());
}
};
client = client.newBuilder().addNetworkInterceptor(interceptor).build();
Request request = new Request.Builder().url(server.url("/")).build();
client.newCall(request).execute();
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method interceptorMakesAnUnrelatedAsyncRequest.
/** Make sure interceptors can interact with the OkHttp client asynchronously. */
@Test
public void interceptorMakesAnUnrelatedAsyncRequest() throws Exception {
// Fetched by interceptor.
server.enqueue(new MockResponse().setBody("a"));
// Fetched directly.
server.enqueue(new MockResponse().setBody("b"));
client = client.newBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
if (chain.request().url().encodedPath().equals("/b")) {
Request requestA = new Request.Builder().url(server.url("/a")).build();
try {
RecordingCallback callbackA = new RecordingCallback();
client.newCall(requestA).enqueue(callbackA);
callbackA.await(requestA.url()).assertBody("a");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return chain.proceed(chain.request());
}
}).build();
Request requestB = new Request.Builder().url(server.url("/b")).build();
RecordingCallback callbackB = new RecordingCallback();
client.newCall(requestB).enqueue(callbackB);
callbackB.await(requestB.url()).assertBody("b");
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method networkInterceptorsObserveNetworkHeaders.
@Test
public void networkInterceptorsObserveNetworkHeaders() throws Exception {
server.enqueue(new MockResponse().setBody(gzip("abcabcabc")).addHeader("Content-Encoding: gzip"));
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
// The network request has everything: User-Agent, Host, Accept-Encoding.
Request networkRequest = chain.request();
assertNotNull(networkRequest.header("User-Agent"));
assertEquals(server.getHostName() + ":" + server.getPort(), networkRequest.header("Host"));
assertNotNull(networkRequest.header("Accept-Encoding"));
// The network response also has everything, including the raw gzipped content.
Response networkResponse = chain.proceed(networkRequest);
assertEquals("gzip", networkResponse.header("Content-Encoding"));
return networkResponse;
}
};
client = client.newBuilder().addNetworkInterceptor(interceptor).build();
Request request = new Request.Builder().url(server.url("/")).build();
// No extra headers in the application's request.
assertNull(request.header("User-Agent"));
assertNull(request.header("Host"));
assertNull(request.header("Accept-Encoding"));
// No extra headers in the application's response.
Response response = client.newCall(request).execute();
assertNull(request.header("Content-Encoding"));
assertEquals("abcabcabc", response.body().string());
}
use of okhttp3.Interceptor in project okhttp by square.
the class InterceptorTest method interceptorMakesAnUnrelatedRequest.
/** Make sure interceptors can interact with the OkHttp client. */
@Test
public void interceptorMakesAnUnrelatedRequest() throws Exception {
// Fetched by interceptor.
server.enqueue(new MockResponse().setBody("a"));
// Fetched directly.
server.enqueue(new MockResponse().setBody("b"));
client = client.newBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
if (chain.request().url().encodedPath().equals("/b")) {
Request requestA = new Request.Builder().url(server.url("/a")).build();
Response responseA = client.newCall(requestA).execute();
assertEquals("a", responseA.body().string());
}
return chain.proceed(chain.request());
}
}).build();
Request requestB = new Request.Builder().url(server.url("/b")).build();
Response responseB = client.newCall(requestB).execute();
assertEquals("b", responseB.body().string());
}
Aggregations