Search in sources :

Example 1 with CompositeContentTypeResolver

use of org.springframework.web.reactive.accept.CompositeContentTypeResolver in project spring-framework by spring-projects.

the class ResourceWebHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    CompositeContentTypeResolver contentTypeResolver = new RequestedContentTypeResolverBuilder().favorPathExtension(false).build();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceWebHandler handler = new ResourceWebHandler();
    handler.setLocations(paths);
    handler.setContentTypeResolver(contentTypeResolver);
    handler.afterPropertiesSet();
    handler.afterSingletonsInstantiated();
    MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Accept", "application/json,text/plain,*/*").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.html");
    handler.handle(exchange).block(TIMEOUT);
    assertEquals(MediaType.TEXT_HTML, exchange.getResponse().getHeaders().getContentType());
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) CompositeContentTypeResolver(org.springframework.web.reactive.accept.CompositeContentTypeResolver) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Test(org.junit.Test)

Example 2 with CompositeContentTypeResolver

use of org.springframework.web.reactive.accept.CompositeContentTypeResolver in project spring-framework by spring-projects.

the class ResourceWebHandlerTests method getResourceWithRegisteredMediaType.

// SPR-13658
@Test
public void getResourceWithRegisteredMediaType() throws Exception {
    CompositeContentTypeResolver contentTypeResolver = new RequestedContentTypeResolverBuilder().mediaType("css", new MediaType("foo", "bar")).build();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceWebHandler handler = new ResourceWebHandler();
    handler.setLocations(paths);
    handler.setContentTypeResolver(contentTypeResolver);
    handler.afterPropertiesSet();
    handler.afterSingletonsInstantiated();
    MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.css");
    handler.handle(exchange).block(TIMEOUT);
    assertEquals(MediaType.parseMediaType("foo/bar"), exchange.getResponse().getHeaders().getContentType());
    assertResponseBody(exchange, "h1 { color:red; }");
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) CompositeContentTypeResolver(org.springframework.web.reactive.accept.CompositeContentTypeResolver) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2 UrlResource (org.springframework.core.io.UrlResource)2 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)2 CompositeContentTypeResolver (org.springframework.web.reactive.accept.CompositeContentTypeResolver)2 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)2 MediaType (org.springframework.http.MediaType)1