Search in sources :

Example 1 with ClientHttpResponse

use of org.springframework.http.client.reactive.ClientHttpResponse in project spring-framework by spring-projects.

the class WebTestClientConnectorTests method captureAndClaim.

@Test
@SuppressWarnings("deprecation")
public void captureAndClaim() throws Exception {
    ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/test");
    ClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));
    ClientRequest clientRequest = ClientRequest.method(HttpMethod.GET, URI.create("/test")).header(WiretapConnector.REQUEST_ID_HEADER_NAME, "1").build();
    WiretapConnector wiretapConnector = new WiretapConnector(connector);
    ExchangeFunction function = ExchangeFunctions.create(wiretapConnector);
    function.exchange(clientRequest).blockMillis(0);
    ExchangeResult actual = wiretapConnector.claimRequest("1");
    assertNotNull(actual);
    assertEquals(HttpMethod.GET, actual.getMethod());
    assertEquals("/test", actual.getUrl().toString());
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Assert.assertNotNull(org.junit.Assert.assertNotNull) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) HttpStatus(org.springframework.http.HttpStatus) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) ExchangeFunctions(org.springframework.web.reactive.function.client.ExchangeFunctions) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) Assert.assertEquals(org.junit.Assert.assertEquals) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Test(org.junit.Test)

Example 2 with ClientHttpResponse

use of org.springframework.http.client.reactive.ClientHttpResponse in project spring-framework by spring-projects.

the class HttpHandlerConnector method connect.

@Override
public Mono<ClientHttpResponse> connect(HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
    MonoProcessor<ClientHttpResponse> result = MonoProcessor.create();
    MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
    MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();
    mockClientRequest.setWriteHandler(requestBody -> {
        log("Invoking HttpHandler for ", httpMethod, uri);
        ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
        this.handler.handle(mockServerRequest, mockServerResponse).subscribe(aVoid -> {
        }, result::onError);
        return Mono.empty();
    });
    mockServerResponse.setWriteHandler(responseBody -> {
        log("Creating client response for ", httpMethod, uri);
        result.onNext(adaptResponse(mockServerResponse, responseBody));
        return Mono.empty();
    });
    log("Writing client request for ", httpMethod, uri);
    requestCallback.apply(mockClientRequest).subscribe(aVoid -> {
    }, result::onError);
    return result;
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse)

Example 3 with ClientHttpResponse

use of org.springframework.http.client.reactive.ClientHttpResponse in project spring-framework by spring-projects.

the class HttpHandlerConnectorTests method adaptResponse.

@Test
public void adaptResponse() throws Exception {
    ResponseCookie cookie = ResponseCookie.from("custom-cookie", "c0").build();
    TestHttpHandler handler = new TestHttpHandler(response -> {
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().put("custom-header", Arrays.asList("h0", "h1"));
        response.getCookies().add(cookie.getName(), cookie);
        return response.writeWith(Mono.just(toDataBuffer("Custom body")));
    });
    ClientHttpResponse response = new HttpHandlerConnector(handler).connect(HttpMethod.GET, URI.create("/custom-path"), ReactiveHttpOutputMessage::setComplete).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(Arrays.asList("h0", "h1"), response.getHeaders().get("custom-header"));
    assertEquals(cookie, response.getCookies().getFirst("custom-cookie"));
    DataBuffer buffer = response.getBody().blockFirst(Duration.ZERO);
    assertEquals("Custom body", DataBufferTestUtils.dumpString(buffer, UTF_8));
}
Also used : ResponseCookie(org.springframework.http.ResponseCookie) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

ClientHttpResponse (org.springframework.http.client.reactive.ClientHttpResponse)3 Test (org.junit.Test)2 MockClientHttpRequest (org.springframework.mock.http.client.reactive.MockClientHttpRequest)2 MockClientHttpResponse (org.springframework.mock.http.client.reactive.MockClientHttpResponse)2 URI (java.net.URI)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 HttpMethod (org.springframework.http.HttpMethod)1 HttpStatus (org.springframework.http.HttpStatus)1 ResponseCookie (org.springframework.http.ResponseCookie)1 ClientHttpConnector (org.springframework.http.client.reactive.ClientHttpConnector)1 ClientHttpRequest (org.springframework.http.client.reactive.ClientHttpRequest)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)1 MockServerHttpResponse (org.springframework.mock.http.server.reactive.MockServerHttpResponse)1 ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)1 ExchangeFunction (org.springframework.web.reactive.function.client.ExchangeFunction)1 ExchangeFunctions (org.springframework.web.reactive.function.client.ExchangeFunctions)1 Mono (reactor.core.publisher.Mono)1