Search in sources :

Example 16 with MockServerHttpResponse

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

the class WebHttpHandlerBuilderTests method httpHandlerDecorator.

@Test
void httpHandlerDecorator() {
    BiFunction<ServerHttpRequest, String, ServerHttpRequest> mutator = (req, value) -> req.mutate().headers(headers -> headers.add("My-Header", value)).build();
    AtomicBoolean success = new AtomicBoolean();
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> {
        HttpHeaders headers = exchange.getRequest().getHeaders();
        assertThat(headers.get("My-Header")).containsExactlyInAnyOrder("1", "2", "3");
        success.set(true);
        return Mono.empty();
    }).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "1"), res)).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "2"), res)).httpHandlerDecorator(handler -> (req, res) -> handler.handle(mutator.apply(req, "3"), res)).build();
    httpHandler.handle(MockServerHttpRequest.get("/").build(), new MockServerHttpResponse()).block();
    assertThat(success.get()).isTrue();
}
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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpHeaders(org.springframework.http.HttpHeaders) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) 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 17 with MockServerHttpResponse

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

the class WebHttpHandlerBuilderTests method orderedWebFilterBeans.

// SPR-15074
@Test
void orderedWebFilterBeans() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(OrderedWebFilterBeanConfig.class);
    context.refresh();
    HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
    boolean condition = httpHandler instanceof HttpWebHandlerAdapter;
    assertThat(condition).isTrue();
    assertThat(((HttpWebHandlerAdapter) httpHandler).getApplicationContext()).isSameAs(context);
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    httpHandler.handle(request, response).block(ofMillis(5000));
    assertThat(response.getBodyAsString().block(ofMillis(5000))).isEqualTo("FilterB::FilterA");
}
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 18 with MockServerHttpResponse

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

the class FilteringWebHandlerTests method handleErrorFromFilter.

@Test
public void handleErrorFromFilter() throws Exception {
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    TestExceptionHandler exceptionHandler = new TestExceptionHandler();
    WebHttpHandlerBuilder.webHandler(new StubWebHandler()).filter(new ExceptionFilter()).exceptionHandler(exceptionHandler).build().handle(request, response).block();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    assertThat(exceptionHandler.ex).isNotNull();
    assertThat(exceptionHandler.ex.getMessage()).isEqualTo("boo");
}
Also used : 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 19 with MockServerHttpResponse

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

the class DefaultWebSessionManagerTests method setUp.

@BeforeEach
void setUp() throws Exception {
    given(this.createSession.save()).willReturn(Mono.empty());
    given(this.createSession.getId()).willReturn("create-session-id");
    given(this.updateSession.getId()).willReturn("update-session-id");
    given(this.sessionStore.createWebSession()).willReturn(Mono.just(this.createSession));
    given(this.sessionStore.retrieveSession(this.updateSession.getId())).willReturn(Mono.just(this.updateSession));
    this.sessionManager = new DefaultWebSessionManager();
    this.sessionManager.setSessionIdResolver(this.sessionIdResolver);
    this.sessionManager.setSessionStore(this.sessionStore);
    MockServerHttpRequest request = MockServerHttpRequest.get("/path").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    this.exchange = new DefaultServerWebExchange(request, response, this.sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
}
Also used : AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 20 with MockServerHttpResponse

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

the class ContextPathCompositeHandlerTests method testHandle.

private ServerHttpResponse testHandle(String pathToHandle, Map<String, HttpHandler> handlerMap) {
    ServerHttpRequest request = MockServerHttpRequest.get(pathToHandle).build();
    ServerHttpResponse response = new MockServerHttpResponse();
    new ContextPathCompositeHandler(handlerMap).handle(request, response).block(Duration.ofSeconds(5));
    return response;
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)

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