use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.
the class ContextPathCompositeHandlerTests method matchWithNativeContextPath.
@Test
public void matchWithNativeContextPath() {
MockServerHttpRequest request = MockServerHttpRequest.get("/yet/another/path").contextPath(// contextPath in underlying request
"/yet").build();
TestHttpHandler handler = new TestHttpHandler();
Map<String, HttpHandler> map = Collections.singletonMap("/another/path", handler);
new ContextPathCompositeHandler(map).handle(request, new MockServerHttpResponse());
assertThat(handler.wasInvoked()).isTrue();
assertThat(handler.getRequest().getPath().contextPath().value()).isEqualTo("/yet/another/path");
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.
the class BodyExtractorsTests method toMonoWithEmptyBodyAndNoContentType.
// SPR-15758
@Test
public void toMonoWithEmptyBodyAndNoContentType() {
BodyExtractor<Mono<Map<String, String>>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(new ParameterizedTypeReference<Map<String, String>>() {
});
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(Flux.empty());
Mono<Map<String, String>> result = extractor.extract(request, this.context);
StepVerifier.create(result).expectComplete().verify();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.
the class BodyExtractorsTests method toFluxWithHints.
@Test
public void toFluxWithHints() {
BodyExtractor<Flux<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toFlux(User.class);
this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
String text = "[{\"username\":\"foo\",\"password\":\"bar\"},{\"username\":\"bar\",\"password\":\"baz\"}]";
byte[] bytes = text.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_JSON).body(body);
Flux<User> result = extractor.extract(request, this.context);
StepVerifier.create(result).consumeNextWith(user -> {
assertThat(user.getUsername()).isEqualTo("foo");
assertThat(user.getPassword()).isNull();
}).consumeNextWith(user -> {
assertThat(user.getUsername()).isEqualTo("bar");
assertThat(user.getPassword()).isNull();
}).expectComplete().verify();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.
the class BodyExtractorsTests method toFluxUnacceptable.
@Test
public void toFluxUnacceptable() {
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("/").contentType(MediaType.APPLICATION_JSON).body(body);
BodyExtractor.Context emptyContext = new BodyExtractor.Context() {
@Override
public List<HttpMessageReader<?>> messageReaders() {
return Collections.emptyList();
}
@Override
public Optional<ServerHttpResponse> serverResponse() {
return Optional.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.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.
the class BodyExtractorsTests method toMonoWithHints.
@Test
public void toMonoWithHints() {
BodyExtractor<Mono<User>, ReactiveHttpInputMessage> extractor = BodyExtractors.toMono(User.class);
this.hints.put(JSON_VIEW_HINT, SafeToDeserialize.class);
byte[] bytes = "{\"username\":\"foo\",\"password\":\"bar\"}".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_JSON).body(body);
Mono<User> result = extractor.extract(request, this.context);
StepVerifier.create(result).consumeNextWith(user -> {
assertThat(user.getUsername()).isEqualTo("foo");
assertThat(user.getPassword()).isNull();
}).expectComplete().verify();
}
Aggregations