Search in sources :

Example 36 with ServerWebExchange

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

the class ResourceWebHandlerTests method ignoreInvalidEscapeSequence.

@Test
public void ignoreInvalidEscapeSequence() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    setPathWithinHandlerMapping(exchange, "/%foo%/bar.txt");
    this.handler.handle(exchange).block(TIMEOUT);
    assertEquals(HttpStatus.NOT_FOUND, exchange.getResponse().getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 37 with ServerWebExchange

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

the class MessageReaderArgumentResolverTests method emptyBody.

// More extensive "empty body" tests in RequestBody- and HttpEntityArgumentResolverTests
// SPR-9942
@Test
// SPR-9942
@SuppressWarnings("unchecked")
public void emptyBody() throws Exception {
    ServerWebExchange exchange = post("/path").contentType(MediaType.APPLICATION_JSON).toExchange();
    ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
    MethodParameter param = this.testMethod.arg(type);
    Mono<TestBean> result = (Mono<TestBean>) this.resolver.readBody(param, true, this.bindingContext, exchange).block();
    StepVerifier.create(result).expectError(ServerWebInputException.class).verify();
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerWebInputException(org.springframework.web.server.ServerWebInputException) Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 38 with ServerWebExchange

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

the class MessageReaderArgumentResolverTests method missingContentType.

@Test
public void missingContentType() throws Exception {
    ServerWebExchange exchange = post("/path").body("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}").toExchange();
    ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
    MethodParameter param = this.testMethod.arg(type);
    Mono<Object> result = this.resolver.readBody(param, true, this.bindingContext, exchange);
    StepVerifier.create(result).expectError(UnsupportedMediaTypeStatusException.class).verify();
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) UnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException) ResolvableType(org.springframework.core.ResolvableType) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 39 with ServerWebExchange

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

the class ModelAttributeMethodArgumentResolverTests method testValidationError.

private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor) throws URISyntaxException {
    ServerWebExchange exchange = postForm("age=invalid");
    Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, exchange);
    mono = valueMonoExtractor.apply(mono);
    StepVerifier.create(mono).consumeErrorWith(ex -> {
        assertTrue(ex instanceof WebExchangeBindException);
        WebExchangeBindException bindException = (WebExchangeBindException) ex;
        assertEquals(1, bindException.getErrorCount());
        assertTrue(bindException.hasFieldErrors("age"));
    }).verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Assert.assertSame(org.junit.Assert.assertSame) Single(rx.Single) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Duration(java.time.Duration) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Before(org.junit.Before) Validated(org.springframework.validation.annotation.Validated) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Assert.assertNotNull(org.junit.Assert.assertNotNull) MediaType(org.springframework.http.MediaType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ResolvableMethod(org.springframework.web.method.ResolvableMethod) RxReactiveStreams(rx.RxReactiveStreams) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException)

Example 40 with ServerWebExchange

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

the class CookieValueMethodArgumentResolverTests method resolveCookieStringArgument.

@Test
public void resolveCookieStringArgument() {
    HttpCookie cookie = new HttpCookie("name", "foo");
    ServerWebExchange exchange = MockServerHttpRequest.get("/").cookie(cookie.getName(), cookie).toExchange();
    Mono<Object> mono = this.resolver.resolveArgument(this.cookieStringParameter, this.bindingContext, exchange);
    assertEquals("Invalid result", cookie.getValue(), mono.block());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HttpCookie(org.springframework.http.HttpCookie) 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