use of org.springframework.core.io.buffer.DefaultDataBuffer 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();
}
use of org.springframework.core.io.buffer.DefaultDataBuffer 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();
}
use of org.springframework.core.io.buffer.DefaultDataBuffer 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();
}
use of org.springframework.core.io.buffer.DefaultDataBuffer 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();
}
use of org.springframework.core.io.buffer.DefaultDataBuffer 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();
}
Aggregations