use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class ContextPathCompositeHandlerTests method notFoundWithCommitAction.
// SPR-17144
@Test
public void notFoundWithCommitAction() {
AtomicBoolean commitInvoked = new AtomicBoolean();
ServerHttpRequest request = MockServerHttpRequest.get("/unknown/path").build();
ServerHttpResponse response = new MockServerHttpResponse();
response.beforeCommit(() -> {
commitInvoked.set(true);
return Mono.empty();
});
Map<String, HttpHandler> map = new HashMap<>();
TestHttpHandler handler = new TestHttpHandler();
map.put("/path", handler);
new ContextPathCompositeHandler(map).handle(request, response).block(Duration.ofSeconds(5));
assertNotInvoked(handler);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(commitInvoked.get()).isTrue();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.
// gh-26232
@Test
void monoResponseShouldNotLeakIfCancelled() {
LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
MockServerHttpResponse response = new MockServerHttpResponse(bufferFactory);
response.setWriteHandler(flux -> {
throw AbortedException.beforeSend();
});
HttpMessageWriter<Object> messageWriter = new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder());
Mono<Void> result = messageWriter.write(Mono.just(Collections.singletonMap("foo", "bar")), ResolvableType.forClass(Mono.class), ResolvableType.forClass(Map.class), null, request, response, Collections.emptyMap());
StepVerifier.create(result).expectError(AbortedException.class).verify();
bufferFactory.checkForLeaks();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class BodyInsertersTests method ofServerSentEventFlux.
@Test
public void ofServerSentEventFlux() {
ServerSentEvent<String> event = ServerSentEvent.builder("foo").build();
Flux<ServerSentEvent<String>> body = Flux.just(event);
BodyInserter<Flux<ServerSentEvent<String>>, ServerHttpResponse> inserter = BodyInserters.fromServerSentEvents(body);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class BodyInsertersTests method ofResource.
@Test
public void ofResource() throws IOException {
Resource resource = new ClassPathResource("response.txt", getClass());
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = BodyInserters.fromResource(resource).insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
byte[] expectedBytes = Files.readAllBytes(resource.getFile().toPath());
StepVerifier.create(response.getBody()).consumeNextWith(dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
DataBufferUtils.release(dataBuffer);
assertThat(resultBytes).isEqualTo(expectedBytes);
}).expectComplete().verify();
}
use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.
the class BodyInsertersTests method ofProducerWithMono.
@Test
public void ofProducerWithMono() {
Mono<User> body = Mono.just(new User("foo", "bar"));
BodyInserter<?, ReactiveHttpOutputMessage> inserter = BodyInserters.fromProducer(body, User.class);
MockServerHttpResponse response = new MockServerHttpResponse();
Mono<Void> result = inserter.insert(response, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\",\"password\":\"bar\"}").expectComplete().verify();
}
Aggregations