Search in sources :

Example 96 with MockServerHttpRequest

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

the class BodyExtractorsTests method toFlux.

@Test
public void toFlux() {
    BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Flux<String> result = extractor.extract(request, this.context);
    StepVerifier.create(result).expectNext("foo").expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Flux(reactor.core.publisher.Flux) 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) Test(org.junit.jupiter.api.Test)

Example 97 with MockServerHttpRequest

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

the class BodyExtractorsTests method toFormData.

@Test
public void toFormData() {
    byte[] bytes = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_FORM_URLENCODED).body(body);
    Mono<MultiValueMap<String, String>> result = BodyExtractors.toFormData().extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(form -> {
        assertThat(form.size()).as("Invalid result").isEqualTo(3);
        assertThat(form.getFirst("name 1")).as("Invalid result").isEqualTo("value 1");
        List<String> values = form.get("name 2");
        assertThat(values.size()).as("Invalid result").isEqualTo(2);
        assertThat(values.get(0)).as("Invalid result").isEqualTo("value 2+1");
        assertThat(values.get(1)).as("Invalid result").isEqualTo("value 2+2");
        assertThat(form.getFirst("name 3")).as("Invalid result").isNull();
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) NettyDataBuffer(org.springframework.core.io.buffer.NettyDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 98 with MockServerHttpRequest

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

the class BodyExtractorsTests method toMono.

@Test
public void toMono() {
    BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
    DefaultDataBufferFactory factory = DefaultDataBufferFactory.sharedInstance;
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Mono<String> result = extractor.extract(request, this.context);
    StepVerifier.create(result).expectNext("foo").expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) 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) Test(org.junit.jupiter.api.Test)

Example 99 with MockServerHttpRequest

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

the class BodyInsertersTests method ofResourceRange.

@Test
public void ofResourceRange() throws IOException {
    final int rangeStart = 10;
    Resource body = new ClassPathResource("response.txt", getClass());
    BodyInserter<Resource, ReactiveHttpOutputMessage> inserter = BodyInserters.fromResource(body);
    MockServerHttpRequest request = MockServerHttpRequest.get("/foo").range(HttpRange.createByteRange(rangeStart)).build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, new BodyInserter.Context() {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return Collections.singletonList(new ResourceHttpMessageWriter());
        }

        @Override
        public Optional<ServerHttpRequest> serverRequest() {
            return Optional.of(request);
        }

        @Override
        public Map<String, Object> hints() {
            return hints;
        }
    });
    StepVerifier.create(result).expectComplete().verify();
    byte[] allBytes = Files.readAllBytes(body.getFile().toPath());
    byte[] expectedBytes = new byte[allBytes.length - rangeStart];
    System.arraycopy(allBytes, rangeStart, expectedBytes, 0, expectedBytes.length);
    StepVerifier.create(response.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo(expectedBytes);
    }).expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) Optional(java.util.Optional) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Example 100 with MockServerHttpRequest

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

the class ResourceWebHandlerTests method partialContentInvalidRangeHeader.

@Test
public void partialContentInvalidRangeHeader() {
    MockServerHttpRequest request = MockServerHttpRequest.get("").header("range", "bytes=foo bar").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    setPathWithinHandlerMapping(exchange, "foo.txt");
    StepVerifier.create(this.handler.handle(exchange)).expectNextCount(0).expectComplete().verify();
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE);
    assertThat(exchange.getResponse().getHeaders().getFirst("Accept-Ranges")).isEqualTo("bytes");
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)182 Test (org.junit.jupiter.api.Test)160 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)101 Mono (reactor.core.publisher.Mono)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)50 Collections (java.util.Collections)48 StepVerifier (reactor.test.StepVerifier)46 HttpHeaders (org.springframework.http.HttpHeaders)39 Flux (reactor.core.publisher.Flux)37 MediaType (org.springframework.http.MediaType)34 ServerWebExchange (org.springframework.web.server.ServerWebExchange)34 MultiValueMap (org.springframework.util.MultiValueMap)33 HttpStatus (org.springframework.http.HttpStatus)31 StandardCharsets (java.nio.charset.StandardCharsets)29 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)29 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)27 ClassPathResource (org.springframework.core.io.ClassPathResource)25 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)24 Optional (java.util.Optional)23