Search in sources :

Example 46 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class DefaultServerResponseBuilderTests method build.

@Test
public void build() {
    ResponseCookie cookie = ResponseCookie.from("name", "value").build();
    Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED).header("MyKey", "MyValue").cookie(cookie).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    result.flatMap(res -> res.writeTo(exchange, EMPTY_CONTEXT)).block();
    MockServerHttpResponse response = exchange.getResponse();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
    assertThat(response.getHeaders().getFirst("MyKey")).isEqualTo("MyValue");
    assertThat(response.getCookies().getFirst("name").getValue()).isEqualTo("value");
    StepVerifier.create(response.getBody()).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) CacheControl(org.springframework.http.CacheControl) URI(java.net.URI) ResponseCookie(org.springframework.http.ResponseCookie) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ResponseCookie(org.springframework.http.ResponseCookie) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 47 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class DefaultEntityResponseBuilderTests method notModifiedEtag.

@Test
public void notModifiedEtag() {
    String etag = "\"foo\"";
    EntityResponse<String> responseMono = EntityResponse.fromObject("bar").eTag(etag).build().block();
    MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").header(HttpHeaders.IF_NONE_MATCH, etag).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();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 48 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class WebHttpHandlerBuilderTests method configWithoutFilters.

@Test
void configWithoutFilters() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(NoFilterConfig.class);
    context.refresh();
    HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    httpHandler.handle(request, response).block(ofMillis(5000));
    assertThat(response.getBodyAsString().block(ofMillis(5000))).isEqualTo("handled");
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 49 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class WebHttpHandlerBuilderTests method orderedWebExceptionHandlerBeans.

// SPR-15074
@Test
void orderedWebExceptionHandlerBeans() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(OrderedExceptionHandlerBeanConfig.class);
    context.refresh();
    HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    httpHandler.handle(request, response).block(ofMillis(5000));
    assertThat(response.getBodyAsString().block(ofMillis(5000))).isEqualTo("ExceptionHandlerB");
}
Also used : HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 50 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class WebHttpHandlerBuilderTests method httpHandlerDecoratorFactoryBeans.

@Test
void httpHandlerDecoratorFactoryBeans() {
    HttpHandler handler = WebHttpHandlerBuilder.applicationContext(new AnnotationConfigApplicationContext(HttpHandlerDecoratorFactoryBeansConfig.class)).build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    handler.handle(MockServerHttpRequest.get("/").build(), response).block();
    Function<String, Long> headerValue = name -> Long.valueOf(response.getHeaders().getFirst(name));
    assertThat(headerValue.apply("decoratorA")).isLessThan(headerValue.apply("decoratorB"));
    assertThat(headerValue.apply("decoratorC")).isLessThan(headerValue.apply("decoratorB"));
}
Also used : HttpHandlerDecoratorFactory(org.springframework.http.server.reactive.HttpHandlerDecoratorFactory) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebHandler(org.springframework.web.server.WebHandler) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebFilter(org.springframework.web.server.WebFilter) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Order(org.springframework.core.annotation.Order) HttpHeaders(org.springframework.http.HttpHeaders) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Duration.ofMillis(java.time.Duration.ofMillis) HttpHandler(org.springframework.http.server.reactive.HttpHandler) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)64 Test (org.junit.jupiter.api.Test)55 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)28 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)24 HttpHandler (org.springframework.http.server.reactive.HttpHandler)14 HttpHeaders (org.springframework.http.HttpHeaders)13 Flux (reactor.core.publisher.Flux)13 Mono (reactor.core.publisher.Mono)13 Collections (java.util.Collections)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 DataBuffer (org.springframework.core.io.buffer.DataBuffer)11 MultiValueMap (org.springframework.util.MultiValueMap)11 StandardCharsets (java.nio.charset.StandardCharsets)10 HttpStatus (org.springframework.http.HttpStatus)10 ResponseCookie (org.springframework.http.ResponseCookie)10 ServerWebExchange (org.springframework.web.server.ServerWebExchange)10 WebFilter (org.springframework.web.server.WebFilter)10 StepVerifier (reactor.test.StepVerifier)10