Search in sources :

Example 6 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest 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()).filters(Collections.singletonList(new ExceptionFilter())).exceptionHandlers(Collections.singletonList(exceptionHandler)).build().handle(request, response).block();
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
    assertNotNull(exceptionHandler.ex);
    assertEquals("boo", exceptionHandler.ex.getMessage());
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 7 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.

the class DefaultWebSessionManagerTests method setUp.

@Before
public void setUp() throws Exception {
    this.manager = new DefaultWebSessionManager();
    this.idResolver = new TestWebSessionIdResolver();
    this.manager.setSessionIdResolver(this.idResolver);
    MockServerHttpRequest request = MockServerHttpRequest.get("/path").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    this.exchange = new DefaultServerWebExchange(request, response, this.manager);
}
Also used : DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Before(org.junit.Before)

Example 8 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.

the class BodyExtractorsTests method toDataBuffers.

@Test
public void toDataBuffers() throws Exception {
    BodyExtractor<Flux<DataBuffer>, ReactiveHttpInputMessage> extractor = BodyExtractors.toDataBuffers();
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
    Flux<DataBuffer> result = extractor.extract(request, this.context);
    StepVerifier.create(result).expectNext(dataBuffer).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Flux(reactor.core.publisher.Flux) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 9 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.

the class RouterFunctionsTests method toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo.

@Test
public void toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
    request -> Mono.<ServerResponse>just(new ServerResponse() {

        @Override
        public HttpStatus statusCode() {
            return HttpStatus.OK;
        }

        @Override
        public HttpHeaders headers() {
            return new HttpHeaders();
        }

        @Override
        public Mono<Void> writeTo(ServerWebExchange exchange, HandlerStrategies strategies) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found");
        }
    });
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
    assertNotNull(result);
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertEquals(HttpStatus.NOT_FOUND, httpResponse.getStatusCode());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) StepVerifier(reactor.test.StepVerifier) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHeaders(org.springframework.http.HttpHeaders) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.Test)

Example 10 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.

the class RouterFunctionsTests method toHttpHandlerHandlerReturnsException.

@Test
public void toHttpHandlerHandlerReturnsException() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new IllegalStateException());
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
    HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
    assertNotNull(result);
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, httpResponse.getStatusCode());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) StepVerifier(reactor.test.StepVerifier) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)34 Test (org.junit.Test)33 Mono (reactor.core.publisher.Mono)14 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)12 StepVerifier (reactor.test.StepVerifier)11 HttpHandler (org.springframework.http.server.reactive.HttpHandler)9 Flux (reactor.core.publisher.Flux)8 Assert (org.junit.Assert)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)7 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)7 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)7 Mockito (org.mockito.Mockito)6 HttpHeaders (org.springframework.http.HttpHeaders)6 HttpStatus (org.springframework.http.HttpStatus)6 ReactiveHttpInputMessage (org.springframework.http.ReactiveHttpInputMessage)6 ResponseStatusException (org.springframework.web.server.ResponseStatusException)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 Collections (java.util.Collections)5 MediaType (org.springframework.http.MediaType)5 Jackson2JsonDecoder (org.springframework.http.codec.json.Jackson2JsonDecoder)5