use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.
the class ResourceHandlerFunctionTests method options.
@Test
public void options() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options("http://localhost"));
MockServerHttpResponse mockResponse = exchange.getResponse();
ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults().messageReaders());
Mono<ServerResponse> responseMono = this.handlerFunction.handle(request);
Mono<Void> result = responseMono.flatMap(response -> {
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.headers().getAllow()).isEqualTo(Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS));
return response.writeTo(exchange, context);
});
StepVerifier.create(result).expectComplete().verify();
assertThat(mockResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(mockResponse.getHeaders().getAllow()).isEqualTo(Set.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS));
StepVerifier.create(mockResponse.getBody()).expectComplete().verify();
}
use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.
the class DefaultEntityResponseBuilderTests method notModifiedLastModified.
@Test
public void notModifiedLastModified() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
EntityResponse<String> responseMono = EntityResponse.fromObject("bar").lastModified(oneMinuteBeforeNow).build().block();
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").header(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now)).build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
responseMono.writeTo(exchange, DefaultServerResponseBuilderTests.EMPTY_CONTEXT);
MockServerHttpResponse response = exchange.getResponse();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
StepVerifier.create(response.getBody()).expectError(IllegalStateException.class).verify();
}
use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.
the class DefaultEntityResponseBuilderTests method bodyInserter.
@Test
public void bodyInserter() {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
Mono<EntityResponse<Publisher<String>>> result = EntityResponse.fromPublisher(publisher, String.class).build();
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost"));
ServerResponse.Context context = new ServerResponse.Context() {
@Override
public List<HttpMessageWriter<?>> messageWriters() {
return Collections.singletonList(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
}
@Override
public List<ViewResolver> viewResolvers() {
return Collections.emptyList();
}
};
StepVerifier.create(result).consumeNextWith(response -> {
StepVerifier.create(response.entity()).expectNext(body).expectComplete().verify();
response.writeTo(exchange, context);
}).expectComplete().verify();
assertThat(exchange.getResponse().getBody()).isNotNull();
}
use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.
the class DefaultServerRequestBuilderTests method from.
@Test
public void from() {
MockServerHttpRequest request = MockServerHttpRequest.post("https://example.com").header("foo", "bar").build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
ServerRequest other = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders());
other.attributes().put("attr1", "value1");
Flux<DataBuffer> body = Flux.just("baz").map(s -> s.getBytes(StandardCharsets.UTF_8)).map(DefaultDataBufferFactory.sharedInstance::wrap);
ServerRequest result = ServerRequest.from(other).method(HttpMethod.HEAD).headers(httpHeaders -> httpHeaders.set("foo", "baar")).cookies(cookies -> cookies.set("baz", ResponseCookie.from("baz", "quux").build())).attribute("attr2", "value2").attributes(attributes -> attributes.put("attr3", "value3")).body(body).build();
assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
assertThat(result.headers().asHttpHeaders()).hasSize(1);
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("baar");
assertThat(result.cookies()).hasSize(1);
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("quux");
assertThat(result.attributes()).containsOnlyKeys(ServerWebExchange.LOG_ID_ATTRIBUTE, "attr1", "attr2", "attr3");
assertThat(result.attributes()).contains(entry("attr1", "value1"), entry("attr2", "value2"), entry("attr3", "value3"));
StepVerifier.create(result.bodyToFlux(String.class)).expectNext("baz").verifyComplete();
}
use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.
the class DefaultServerResponseBuilderTests method notModifiedLastModified.
@Test
public void notModifiedLastModified() {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
ServerResponse responseMono = ServerResponse.ok().lastModified(oneMinuteBeforeNow).bodyValue("bar").block();
MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").header(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now)).build();
MockServerWebExchange exchange = MockServerWebExchange.from(request);
responseMono.writeTo(exchange, EMPTY_CONTEXT);
MockServerHttpResponse response = exchange.getResponse();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_MODIFIED);
StepVerifier.create(response.getBody()).expectError(IllegalStateException.class).verify();
}
Aggregations