Search in sources :

Example 81 with ServerWebExchange

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

the class DefaultCorsProcessorTests method actualRequestCredentialsWithOriginWildcard.

@Test
public void actualRequestCredentialsWithOriginWildcard() throws Exception {
    ServerWebExchange exchange = actualRequest();
    this.conf.addAllowedOrigin("*");
    this.conf.setAllowCredentials(true);
    this.processor.processRequest(this.conf, exchange);
    ServerHttpResponse response = exchange.getResponse();
    assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
    assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
    assertNull(response.getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.Test)

Example 82 with ServerWebExchange

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

the class UrlBasedCorsConfigurationSourceTests method empty.

@Test
public void empty() {
    ServerWebExchange exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
    assertNull(this.configSource.getCorsConfiguration(exchange));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 83 with ServerWebExchange

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

the class DispatcherHandlerErrorTests method requestBodyError.

@Test
public void requestBodyError() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.post("/request-body").body(Mono.error(EXCEPTION)).toExchange();
    Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(publisher).consumeErrorWith(error -> {
        assertThat(error, instanceOf(ServerWebInputException.class));
        assertSame(EXCEPTION, error.getCause());
    }).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) ServerWebInputException(org.springframework.web.server.ServerWebInputException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) Before(org.junit.Before) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 84 with ServerWebExchange

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

the class DispatcherHandlerErrorTests method webExceptionHandler.

@Test
public void webExceptionHandler() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/unknown-argument-type").toExchange();
    List<WebExceptionHandler> handlers = Collections.singletonList(new ServerError500ExceptionHandler());
    WebHandler webHandler = new ExceptionHandlingWebHandler(this.dispatcherHandler, handlers);
    webHandler.handle(exchange).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, exchange.getResponse().getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) WebHandler(org.springframework.web.server.WebHandler) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) Test(org.junit.Test)

Example 85 with ServerWebExchange

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

the class DispatcherHandlerErrorTests method controllerThrowsException.

@Test
public void controllerThrowsException() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/raise-exception").toExchange();
    Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(publisher).consumeErrorWith(error -> assertSame(EXCEPTION, error)).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) ServerWebInputException(org.springframework.web.server.ServerWebInputException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) Before(org.junit.Before) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) 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