Search in sources :

Example 1 with MockClientHttpResponse

use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.

the class BodyExtractorsTests method toMonoVoidAsClientShouldConsumeAndCancel.

@Test
public void toMonoVoidAsClientShouldConsumeAndCancel() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    TestPublisher<DataBuffer> body = TestPublisher.create();
    BodyExtractor<Mono<Void>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(Void.class);
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.setBody(body.flux());
    StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
        body.assertWasSubscribed();
        body.emit(dataBuffer);
    }).verifyComplete();
    body.assertCancelled();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with MockClientHttpResponse

use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.

the class DefaultClientResponseBuilderTests method mutate.

@Test
public void mutate() {
    Flux<DataBuffer> otherBody = Flux.just("foo", "bar").map(s -> s.getBytes(StandardCharsets.UTF_8)).map(DefaultDataBufferFactory.sharedInstance::wrap);
    HttpRequest mockClientHttpRequest = new MockClientHttpRequest(HttpMethod.GET, "/path");
    MockClientHttpResponse httpResponse = new MockClientHttpResponse(HttpStatus.OK);
    httpResponse.getHeaders().add("foo", "bar");
    httpResponse.getHeaders().add("bar", "baz");
    httpResponse.getCookies().add("baz", ResponseCookie.from("baz", "qux").build());
    httpResponse.setBody(otherBody);
    DefaultClientResponse otherResponse = new DefaultClientResponse(httpResponse, ExchangeStrategies.withDefaults(), "my-prefix", "", () -> mockClientHttpRequest);
    ClientResponse result = otherResponse.mutate().statusCode(HttpStatus.BAD_REQUEST).headers(headers -> headers.set("foo", "baar")).cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())).build();
    assertThat(result.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
    assertThat(result.headers().asHttpHeaders().size()).isEqualTo(3);
    assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
    assertThat(result.headers().asHttpHeaders().getFirst("bar")).isEqualTo("baz");
    assertThat(result.cookies().size()).isEqualTo(1);
    assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
    assertThat(result.logPrefix()).isEqualTo("my-prefix");
    StepVerifier.create(result.bodyToFlux(String.class)).expectNext("foobar").verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) HttpRequest(org.springframework.http.HttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ResponseCookie(org.springframework.http.ResponseCookie) HttpRequest(org.springframework.http.HttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with MockClientHttpResponse

use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.

the class BodyExtractorsTests method unsupportedMediaTypeShouldConsumeAndCancel.

// SPR-17054
@Test
public void unsupportedMediaTypeShouldConsumeAndCancel() {
    NettyDataBufferFactory factory = new NettyDataBufferFactory(new PooledByteBufAllocator(true));
    NettyDataBuffer buffer = factory.wrap(ByteBuffer.wrap("spring".getBytes(StandardCharsets.UTF_8)));
    TestPublisher<DataBuffer> body = TestPublisher.create();
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.getHeaders().setContentType(MediaType.APPLICATION_PDF);
    response.setBody(body.flux());
    BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
    StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
        body.assertWasSubscribed();
        body.emit(buffer);
    }).expectErrorSatisfies(throwable -> {
        boolean condition = throwable instanceof UnsupportedMediaTypeException;
        assertThat(condition).isTrue();
        assertThatExceptionOfType(IllegalReferenceCountException.class).isThrownBy(buffer::release);
        body.assertCancelled();
    }).verify();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) TestPublisher(reactor.test.publisher.TestPublisher) FilePart(org.springframework.http.codec.multipart.FilePart) FormFieldPart(org.springframework.http.codec.multipart.FormFieldPart) JsonView(com.fasterxml.jackson.annotation.JsonView) StepVerifier(reactor.test.StepVerifier) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) HashMap(java.util.HashMap) JSON_VIEW_HINT(org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Part(org.springframework.http.codec.multipart.Part) Map(java.util.Map) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) DefaultPartHttpMessageReader(org.springframework.http.codec.multipart.DefaultPartHttpMessageReader) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MultipartHttpMessageReader(org.springframework.http.codec.multipart.MultipartHttpMessageReader) Optional(java.util.Optional) Collections(java.util.Collections) Mono(reactor.core.publisher.Mono) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with MockClientHttpResponse

use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.

the class BodyExtractorsTests method toMonoVoidAsClientWithEmptyBody.

@Test
public void toMonoVoidAsClientWithEmptyBody() {
    TestPublisher<DataBuffer> body = TestPublisher.create();
    BodyExtractor<Mono<Void>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(Void.class);
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.setBody(body.flux());
    StepVerifier.create(extractor.extract(response, this.context)).then(() -> {
        body.assertWasSubscribed();
        body.complete();
    }).verifyComplete();
}
Also used : Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 5 with MockClientHttpResponse

use of org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse in project spring-framework by spring-projects.

the class ResourceHttpMessageReaderTests method readResourceAsMono.

@Test
void readResourceAsMono() throws IOException {
    String filename = "test.txt";
    String body = "Test resource content";
    ContentDisposition contentDisposition = ContentDisposition.attachment().name("file").filename(filename).build();
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
    response.getHeaders().setContentDisposition(contentDisposition);
    response.setBody(Mono.just(stringBuffer(body)));
    Resource resource = reader.readMono(ResolvableType.forClass(ByteArrayResource.class), response, Collections.emptyMap()).block();
    assertThat(resource).isNotNull();
    assertThat(resource.getFilename()).isEqualTo(filename);
    assertThat(resource.getInputStream()).hasContent(body);
}
Also used : ContentDisposition(org.springframework.http.ContentDisposition) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)5 MockClientHttpResponse (org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse)5 DataBuffer (org.springframework.core.io.buffer.DataBuffer)4 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)3 NettyDataBuffer (org.springframework.core.io.buffer.NettyDataBuffer)3 ReactiveHttpInputMessage (org.springframework.http.ReactiveHttpInputMessage)3 Mono (reactor.core.publisher.Mono)3 StandardCharsets (java.nio.charset.StandardCharsets)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)2 HttpStatus (org.springframework.http.HttpStatus)2 JsonView (com.fasterxml.jackson.annotation.JsonView)1 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)1 IllegalReferenceCountException (io.netty.util.IllegalReferenceCountException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1