Search in sources :

Example 1 with ServerWebExchange

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

the class ViewResolutionResultHandler method addBindingResult.

private void addBindingResult(BindingContext context, ServerWebExchange exchange) {
    Map<String, Object> model = context.getModel().asMap();
    model.keySet().stream().filter(name -> isBindingCandidate(name, model.get(name))).filter(name -> !model.containsKey(BindingResult.MODEL_KEY_PREFIX + name)).forEach(name -> {
        WebExchangeDataBinder binder = context.createDataBinder(exchange, model.get(name), name);
        model.put(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
    });
}
Also used : HttpRequestPathHelper(org.springframework.web.server.support.HttpRequestPathHelper) Ordered(org.springframework.core.Ordered) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerResultHandlerSupport(org.springframework.web.reactive.result.HandlerResultHandlerSupport) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Model(org.springframework.ui.Model) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Locale(java.util.Locale) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) HandlerResultHandler(org.springframework.web.reactive.HandlerResultHandler) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ReactiveAdapter(org.springframework.core.ReactiveAdapter) ClassUtils(org.springframework.util.ClassUtils) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) Collectors(java.util.stream.Collectors) HandlerResult(org.springframework.web.reactive.HandlerResult) Flux(reactor.core.publisher.Flux) List(java.util.List) Collections(java.util.Collections) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder)

Example 2 with ServerWebExchange

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

the class PathExtensionContentTypeResolverTests method resolveMediaTypesFromMapping.

@Test
public void resolveMediaTypesFromMapping() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/test.html").toExchange();
    PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver();
    List<MediaType> mediaTypes = resolver.resolveMediaTypes(exchange);
    assertEquals(Collections.singletonList(new MediaType("text", "html")), mediaTypes);
    Map<String, MediaType> mapping = Collections.singletonMap("HTML", MediaType.APPLICATION_XHTML_XML);
    resolver = new PathExtensionContentTypeResolver(mapping);
    mediaTypes = resolver.resolveMediaTypes(exchange);
    assertEquals(Collections.singletonList(new MediaType("application", "xhtml+xml")), mediaTypes);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 3 with ServerWebExchange

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

the class CompositeContentTypeResolverBuilderTests method setDefaultContentType.

// SPR-10513
@Test
public void setDefaultContentType() throws Exception {
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().defaultContentType(MediaType.APPLICATION_JSON).build();
    ServerWebExchange exchange = MockServerHttpRequest.get("/").accept(MediaType.ALL).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 4 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 5 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)

Aggregations

ServerWebExchange (org.springframework.web.server.ServerWebExchange)298 Test (org.junit.jupiter.api.Test)180 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)124 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)81 Mono (reactor.core.publisher.Mono)64 Test (org.junit.Test)63 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)49 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)39 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 MediaType (org.springframework.http.MediaType)30 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)30 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)29 List (java.util.List)27 BeforeEach (org.junit.jupiter.api.BeforeEach)27 StepVerifier (reactor.test.StepVerifier)27 URI (java.net.URI)26 Map (java.util.Map)26 BindingContext (org.springframework.web.reactive.BindingContext)26 Collections (java.util.Collections)25 MethodParameter (org.springframework.core.MethodParameter)24