Search in sources :

Example 91 with Response

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"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 92 with Response

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());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 93 with Response

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"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 94 with Response

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));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Pojo(org.springframework.http.codec.Pojo) Test(org.junit.Test)

Example 95 with Response

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));
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) OkHttpClient(okhttp3.OkHttpClient) GET(com.androidnetworking.common.Method.GET) PUT(com.androidnetworking.common.Method.PUT) POST(com.androidnetworking.common.Method.POST) Request(okhttp3.Request) IOException(java.io.IOException) ResponseProgressBody(com.androidnetworking.internal.ResponseProgressBody) Interceptor(okhttp3.Interceptor)

Aggregations

Test (org.junit.Test)471 Response (okhttp3.Response)444 MockResponse (okhttp3.mockwebserver.MockResponse)380 Request (okhttp3.Request)377 ResponseBody (okhttp3.ResponseBody)351 IOException (java.io.IOException)220 DateTime (org.joda.time.DateTime)194 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)192 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)178 Response (retrofit2.Response)150 ServiceCall (com.microsoft.rest.ServiceCall)140 ServiceResponse (com.microsoft.rest.ServiceResponse)114 Observable (rx.Observable)104 Call (okhttp3.Call)103 List (java.util.List)95 RequestBody (okhttp3.RequestBody)85 PagedList (com.microsoft.azure.PagedList)80 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)78 OkHttpClient (okhttp3.OkHttpClient)78 HttpURLConnection (java.net.HttpURLConnection)47