Search in sources :

Example 21 with ServerWebExchange

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

the class CompositeContentTypeResolverBuilderTests method setDefaultContentTypeWithStrategy.

// SPR-12286
@Test
public void setDefaultContentTypeWithStrategy() throws Exception {
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().defaultContentTypeResolver(new FixedContentTypeResolver(MediaType.APPLICATION_JSON)).build();
    List<MediaType> expected = Collections.singletonList(MediaType.APPLICATION_JSON);
    ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
    assertEquals(expected, resolver.resolveMediaTypes(exchange));
    exchange = MockServerHttpRequest.get("/").accept(MediaType.ALL).toExchange();
    assertEquals(expected, resolver.resolveMediaTypes(exchange));
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 22 with ServerWebExchange

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

the class CompositeContentTypeResolverBuilderTests method favorParameter.

@Test
public void favorParameter() throws Exception {
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().favorParameter(true).mediaType("json", MediaType.APPLICATION_JSON).build();
    ServerWebExchange exchange = MockServerHttpRequest.get("/flower?format=json").toExchange();
    assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), resolver.resolveMediaTypes(exchange));
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 23 with ServerWebExchange

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

the class PathExtensionContentTypeResolverTests method getMediaTypeFromFilenameNoJaf.

// SPR-10334
@Test
public void getMediaTypeFromFilenameNoJaf() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("test.json").toExchange();
    PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver();
    resolver.setUseJaf(false);
    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 24 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange 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 25 with ServerWebExchange

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

the class CorsUrlHandlerMappingTests method actualRequestWithoutCorsConfigurationProvider.

@Test
public void actualRequestWithoutCorsConfigurationProvider() throws Exception {
    String origin = "http://domain2.com";
    ServerWebExchange exchange = createExchange(HttpMethod.GET, "/welcome.html", origin);
    Object actual = this.handlerMapping.getHandler(exchange).block();
    assertNotNull(actual);
    assertSame(this.welcomeController, actual);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) 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