Search in sources :

Example 46 with ServerWebExchange

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

the class RequestMappingInfoHandlerMappingTests method getHandlerProducibleMediaTypesAttribute.

@Test
public void getHandlerProducibleMediaTypesAttribute() throws Exception {
    ServerWebExchange exchange = get("/content").accept(MediaType.APPLICATION_XML).toExchange();
    this.handlerMapping.getHandler(exchange).block();
    String name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
    assertEquals(Collections.singleton(MediaType.APPLICATION_XML), exchange.getAttributes().get(name));
    exchange = get("/content").accept(MediaType.APPLICATION_JSON).toExchange();
    this.handlerMapping.getHandler(exchange).block();
    assertNull("Negated expression shouldn't be listed as producible type", exchange.getAttributes().get(name));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 47 with ServerWebExchange

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

the class RequestMappingInfoHandlerMappingTests method testMediaTypeNotAcceptable.

private void testMediaTypeNotAcceptable(String url) throws Exception {
    ServerWebExchange exchange = get(url).accept(MediaType.APPLICATION_JSON).toExchange();
    Mono<Object> mono = this.handlerMapping.getHandler(exchange);
    assertError(mono, NotAcceptableStatusException.class, ex -> assertEquals("Invalid supported producible media types", Collections.singletonList(new MediaType("application", "xml")), ex.getSupportedMediaTypes()));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MediaType(org.springframework.http.MediaType)

Example 48 with ServerWebExchange

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

the class RequestMappingInfoHandlerMappingTests method handleMatchBestMatchingPatternAttribute.

@Test
public void handleMatchBestMatchingPatternAttribute() throws Exception {
    RequestMappingInfo key = paths("/{path1}/2", "/**").build();
    ServerWebExchange exchange = get("/1/2").toExchange();
    this.handlerMapping.handleMatch(key, "/1/2", exchange);
    assertEquals("/{path1}/2", exchange.getAttributes().get(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 49 with ServerWebExchange

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

the class RequestMappingInfoHandlerMappingTests method getHandlerEmptyPathMatch.

@Test
public void getHandlerEmptyPathMatch() throws Exception {
    Method expected = on(TestController.class).annot(requestMapping("")).resolveMethod();
    ServerWebExchange exchange = get("").toExchange();
    HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
    assertEquals(expected, hm.getMethod());
    exchange = get("/").toExchange();
    hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
    assertEquals(expected, hm.getMethod());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 50 with ServerWebExchange

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

the class RequestMappingInfoHandlerMappingTests method handleMatchUriTemplateVariables.

@Test
@SuppressWarnings("unchecked")
public void handleMatchUriTemplateVariables() throws Exception {
    String lookupPath = "/1/2";
    ServerWebExchange exchange = get(lookupPath).toExchange();
    RequestMappingInfo key = paths("/{path1}/{path2}").build();
    this.handlerMapping.handleMatch(key, lookupPath, exchange);
    String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
    Map<String, String> uriVariables = (Map<String, String>) exchange.getAttributes().get(name);
    assertNotNull(uriVariables);
    assertEquals("1", uriVariables.get("path1"));
    assertEquals("2", uriVariables.get("path2"));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) 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