use of okhttp3.Response in project spring-framework by spring-projects.
the class WebClientIntegrationTests method buildFilter.
@Test
public void buildFilter() throws Exception {
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
WebClient filteredClient = this.webClient.filter((request, next) -> {
ClientRequest filteredRequest = ClientRequest.from(request).header("foo", "bar").build();
return next.exchange(filteredRequest);
});
Mono<String> result = filteredClient.get().uri("/greeting?name=Spring").exchange().then(response -> response.bodyToMono(String.class));
StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("bar", recordedRequest.getHeader("foo"));
}
use of okhttp3.Response in project spring-framework by spring-projects.
the class WebClientIntegrationTests method notFound.
@Test
public void notFound() throws Exception {
this.server.enqueue(new MockResponse().setResponseCode(404).setHeader("Content-Type", "text/plain").setBody("Not Found"));
Mono<ClientResponse> result = this.webClient.get().uri("/greeting?name=Spring").exchange();
StepVerifier.create(result).consumeNextWith(response -> assertEquals(HttpStatus.NOT_FOUND, response.statusCode())).expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath());
}
use of okhttp3.Response in project spring-framework by spring-projects.
the class WebClientIntegrationTests method filter.
@Test
public void filter() throws Exception {
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
WebClient filteredClient = this.webClient.filter((request, next) -> {
ClientRequest filteredRequest = ClientRequest.from(request).header("foo", "bar").build();
return next.exchange(filteredRequest);
});
Mono<String> result = filteredClient.get().uri("/greeting?name=Spring").exchange().then(response -> response.bodyToMono(String.class));
StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("bar", recordedRequest.getHeader("foo"));
}
use of okhttp3.Response in project spring-framework by spring-projects.
the class WebClientIntegrationTests method postJsonPojo.
@Test
public void postJsonPojo() throws Exception {
this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setBody("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}"));
Mono<Pojo> result = this.webClient.post().uri("/pojo/capitalize").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).exchange(fromObject(new Pojo("foofoo", "barbar"))).then(response -> response.bodyToMono(Pojo.class));
StepVerifier.create(result).consumeNextWith(p -> assertEquals("BARBAR", p.getBar())).expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("/pojo/capitalize", recordedRequest.getPath());
Assert.assertEquals("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", recordedRequest.getBody().readUtf8());
Assert.assertEquals("chunked", recordedRequest.getHeader(HttpHeaders.TRANSFER_ENCODING));
Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE));
}
use of okhttp3.Response in project Fast-Android-Networking by amitshekhariitbhu.
the class RxInternalNetworking method generateDownloadObservable.
public static <T> Observable<T> generateDownloadObservable(final RxANRequest request) {
Request okHttpRequest;
Request.Builder builder = new Request.Builder().url(request.getUrl());
InternalNetworking.addHeadersToRequestBuilder(builder, request);
builder = builder.get();
if (request.getCacheControl() != null) {
builder.cacheControl(request.getCacheControl());
}
okHttpRequest = builder.build();
OkHttpClient okHttpClient;
if (request.getOkHttpClient() != null) {
okHttpClient = request.getOkHttpClient().newBuilder().cache(InternalNetworking.sHttpClient.cache()).addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
}
}).build();
} else {
okHttpClient = InternalNetworking.sHttpClient.newBuilder().addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
}
}).build();
}
request.setCall(okHttpClient.newCall(okHttpRequest));
return Observable.create(new ANOnSubscribe<T>(request));
}
Aggregations