Search in sources :

Example 86 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class PathExtensionContentTypeResolverTests method getMediaTypeFilenameWithEncodedURI.

// SPR-9390
@Test
public void getMediaTypeFilenameWithEncodedURI() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/quo%20vadis%3f.html").toExchange();
    PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver();
    List<MediaType> result = resolver.resolveMediaTypes(exchange);
    assertEquals("Invalid content type", Collections.singletonList(new MediaType("text", "html")), result);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 87 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class PathExtensionContentTypeResolverTests method resolveMediaTypesFromJaf.

@Test
public void resolveMediaTypesFromJaf() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("test.xls").toExchange();
    PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver();
    List<MediaType> mediaTypes = resolver.resolveMediaTypes(exchange);
    assertEquals(Collections.singletonList(new MediaType("application", "vnd.ms-excel")), mediaTypes);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 88 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class PathExtensionContentTypeResolverTests method resolveMediaTypesIgnoreUnknownExtension.

// SPR-10170
@Test
public void resolveMediaTypesIgnoreUnknownExtension() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("test.xyz").toExchange();
    PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver();
    List<MediaType> mediaTypes = resolver.resolveMediaTypes(exchange);
    assertEquals(Collections.<MediaType>emptyList(), mediaTypes);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 89 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class DefaultServerResponseBuilderTests method build.

@Test
public void build() throws Exception {
    Mono<ServerResponse> result = ServerResponse.status(HttpStatus.CREATED).header("MyKey", "MyValue").build();
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    when(exchange.getResponse()).thenReturn(response);
    HandlerStrategies strategies = mock(HandlerStrategies.class);
    result.then(res -> res.writeTo(exchange, strategies)).block();
    assertEquals(HttpStatus.CREATED, response.getStatusCode());
    assertEquals("MyValue", response.getHeaders().getFirst("MyKey"));
    StepVerifier.create(response.getBody()).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) HttpHeaders(org.springframework.http.HttpHeaders) ZonedDateTime(java.time.ZonedDateTime) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) CacheControl(org.springframework.http.CacheControl) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) URI(java.net.URI) Collections(java.util.Collections) EnumSet(java.util.EnumSet) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 90 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class RouterFunctionsTests method toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo.

@Test
public void toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo() 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) {
            return Mono.error(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)

Aggregations

ServerWebExchange (org.springframework.web.server.ServerWebExchange)158 Test (org.junit.Test)138 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)53 Mono (reactor.core.publisher.Mono)22 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)20 MediaType (org.springframework.http.MediaType)18 BindingContext (org.springframework.web.reactive.BindingContext)17 MethodParameter (org.springframework.core.MethodParameter)15 List (java.util.List)14 ResolvableType (org.springframework.core.ResolvableType)14 Map (java.util.Map)12 Collections (java.util.Collections)11 HttpStatus (org.springframework.http.HttpStatus)11 StepVerifier (reactor.test.StepVerifier)11 Assert.assertEquals (org.junit.Assert.assertEquals)10 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)10 ResponseStatusException (org.springframework.web.server.ResponseStatusException)9 ServerWebInputException (org.springframework.web.server.ServerWebInputException)9 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)8 NotAcceptableStatusException (org.springframework.web.server.NotAcceptableStatusException)8