Search in sources :

Example 21 with MockServerHttpRequest

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

the class BodyExtractorsTests method toMono.

@Test
public void toMono() throws Exception {
    BodyExtractor<Mono<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(String.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    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.mock.http.server.reactive.test.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) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 22 with MockServerHttpRequest

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

the class BodyExtractorsTests method toMonoWithHints.

@Test
public void toMonoWithHints() throws Exception {
    BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
    this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("{\"username\":\"foo\",\"password\":\"bar\"}".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    Mono<User> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(user -> {
        assertEquals("foo", user.getUsername());
        assertNull(user.getPassword());
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.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) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 23 with MockServerHttpRequest

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

the class BodyExtractorsTests method toFluxWithHints.

@Test
public void toFluxWithHints() throws Exception {
    BodyExtractor<Flux<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(User.class);
    this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("[{\"username\":\"foo\",\"password\":\"bar\"},{\"username\":\"bar\",\"password\":\"baz\"}]".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    Flux<User> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(user -> {
        assertEquals("foo", user.getUsername());
        assertNull(user.getPassword());
    }).consumeNextWith(user -> {
        assertEquals("bar", user.getUsername());
        assertNull(user.getPassword());
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) JsonView(com.fasterxml.jackson.annotation.JsonView) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) AbstractJackson2Codec(org.springframework.http.codec.json.AbstractJackson2Codec) Map(java.util.Map) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) Before(org.junit.Before) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert(org.junit.Assert) Collections(java.util.Collections) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 24 with MockServerHttpRequest

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

the class BodyExtractorsTests method toFluxUnacceptable.

@Test
public void toFluxUnacceptable() throws Exception {
    BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_JSON).body(body);
    BodyExtractor.Context emptyContext = new BodyExtractor.Context() {

        @Override
        public Supplier<Stream<HttpMessageReader<?>>> messageReaders() {
            return Stream::empty;
        }

        @Override
        public Map<String, Object> hints() {
            return Collections.emptyMap();
        }
    };
    Flux<String> result = extractor.extract(request, emptyContext);
    StepVerifier.create(result).expectError(UnsupportedMediaTypeException.class).verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Stream(java.util.stream.Stream) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 25 with MockServerHttpRequest

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

the class BodyExtractorsTests method toFlux.

@Test
public void toFlux() throws Exception {
    BodyExtractor<Flux<String>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(String.class);
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    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.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)34 Test (org.junit.Test)33 Mono (reactor.core.publisher.Mono)14 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)12 StepVerifier (reactor.test.StepVerifier)11 HttpHandler (org.springframework.http.server.reactive.HttpHandler)9 Flux (reactor.core.publisher.Flux)8 Assert (org.junit.Assert)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)7 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)7 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)7 Mockito (org.mockito.Mockito)6 HttpHeaders (org.springframework.http.HttpHeaders)6 HttpStatus (org.springframework.http.HttpStatus)6 ReactiveHttpInputMessage (org.springframework.http.ReactiveHttpInputMessage)6 ResponseStatusException (org.springframework.web.server.ResponseStatusException)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 Collections (java.util.Collections)5 MediaType (org.springframework.http.MediaType)5 Jackson2JsonDecoder (org.springframework.http.codec.json.Jackson2JsonDecoder)5