Search in sources :

Example 51 with ServerWebExchange

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

the class RequestMappingInfoHandlerMappingTests method handleMatchUriTemplateVariablesDecode.

// SPR-9098
@Test
public void handleMatchUriTemplateVariablesDecode() throws Exception {
    RequestMappingInfo key = paths("/{group}/{identifier}").build();
    URI url = URI.create("/group/a%2Fb");
    ServerWebExchange exchange = MockServerHttpRequest.method(HttpMethod.GET, url).toExchange();
    HttpRequestPathHelper pathHelper = new HttpRequestPathHelper();
    pathHelper.setUrlDecode(false);
    String lookupPath = pathHelper.getLookupPathForRequest(exchange);
    this.handlerMapping.setPathHelper(pathHelper);
    this.handlerMapping.handleMatch(key, lookupPath, exchange);
    String name = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
    @SuppressWarnings("unchecked") Map<String, String> uriVariables = (Map<String, String>) exchange.getAttributes().get(name);
    assertNotNull(uriVariables);
    assertEquals("group", uriVariables.get("group"));
    assertEquals("a/b", uriVariables.get("identifier"));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpRequestPathHelper(org.springframework.web.server.support.HttpRequestPathHelper) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.Test)

Example 52 with ServerWebExchange

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

the class RequestMappingInfoHandlerMappingTests method testHttpOptions.

private void testHttpOptions(String requestURI, String allowHeader) throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.options(requestURI).toExchange();
    HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
    BindingContext bindingContext = new BindingContext();
    InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
    Mono<HandlerResult> mono = invocable.invoke(exchange, bindingContext);
    HandlerResult result = mono.block();
    assertNotNull(result);
    Optional<Object> value = result.getReturnValue();
    assertTrue(value.isPresent());
    assertEquals(HttpHeaders.class, value.get().getClass());
    assertEquals(allowHeader, ((HttpHeaders) value.get()).getFirst("Allow"));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 53 with ServerWebExchange

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

the class RequestMappingInfoHandlerMappingTests method getHandlerTestRequestParamMismatch.

// SPR-12854
@Test
public void getHandlerTestRequestParamMismatch() throws Exception {
    ServerWebExchange exchange = get("/params").toExchange();
    Mono<Object> mono = this.handlerMapping.getHandler(exchange);
    assertError(mono, ServerWebInputException.class, ex -> {
        assertThat(ex.getReason(), containsString("[foo=bar]"));
        assertThat(ex.getReason(), containsString("[bar=baz]"));
    });
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 54 with ServerWebExchange

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

the class HttpEntityArgumentResolverTests method httpEntityWithMonoBody.

@Test
public void httpEntityWithMonoBody() throws Exception {
    ServerWebExchange exchange = postExchange("line1");
    ResolvableType type = httpEntityType(Mono.class, String.class);
    HttpEntity<Mono<String>> httpEntity = resolveValue(exchange, type);
    assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders());
    assertEquals("line1", httpEntity.getBody().block());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 55 with ServerWebExchange

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

the class HttpEntityArgumentResolverTests method requestEntity.

@Test
public void requestEntity() throws Exception {
    ServerWebExchange exchange = postExchange("line1");
    ResolvableType type = forClassWithGenerics(RequestEntity.class, String.class);
    RequestEntity<String> requestEntity = resolveValue(exchange, type);
    assertEquals(exchange.getRequest().getMethod(), requestEntity.getMethod());
    assertEquals(exchange.getRequest().getURI(), requestEntity.getUrl());
    assertEquals(exchange.getRequest().getHeaders(), requestEntity.getHeaders());
    assertEquals("line1", requestEntity.getBody());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ResolvableType(org.springframework.core.ResolvableType) 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