Search in sources :

Example 1 with ContentNegotiationManagerFactoryBean

use of cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean in project today-infrastructure by TAKETODAY.

the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    this.request.addHeader("Accept", "application/json,text/plain,*/*");
    request.setRequestURI("foo.html");
    handler.handleRequest(requestContext);
    assertThat(this.response.getContentType()).isEqualTo("text/html");
}
Also used : ContentNegotiationManagerFactoryBean(cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) Resource(cn.taketoday.core.io.Resource) UrlBasedResource(cn.taketoday.core.io.UrlBasedResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 2 with ContentNegotiationManagerFactoryBean

use of cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean in project today-infrastructure by TAKETODAY.

the class ReactiveTypeHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
    factoryBean.afterPropertiesSet();
    ContentNegotiationManager manager = factoryBean.getObject();
    ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
    this.handler = new ReactiveTypeHandler(adapterRegistry, new SyncTaskExecutor(), manager);
    resetRequest();
}
Also used : ContentNegotiationManagerFactoryBean(cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean) SyncTaskExecutor(cn.taketoday.core.task.SyncTaskExecutor) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) ReactiveAdapterRegistry(cn.taketoday.core.ReactiveAdapterRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ContentNegotiationManagerFactoryBean

use of cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean in project today-framework by TAKETODAY.

the class ReactiveTypeHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
    factoryBean.afterPropertiesSet();
    ContentNegotiationManager manager = factoryBean.getObject();
    ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
    this.handler = new ReactiveTypeHandler(adapterRegistry, new SyncTaskExecutor(), manager);
    resetRequest();
}
Also used : ContentNegotiationManagerFactoryBean(cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean) SyncTaskExecutor(cn.taketoday.core.task.SyncTaskExecutor) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) ReactiveAdapterRegistry(cn.taketoday.core.ReactiveAdapterRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ContentNegotiationManagerFactoryBean

use of cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean in project today-framework by TAKETODAY.

the class ResourceHttpRequestHandlerTests method getResourceWithRegisteredMediaType.

// SPR-13658
@Test
public void getResourceWithRegisteredMediaType() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.addMediaType("bar", new MediaType("foo", "bar"));
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    request.setRequestURI("foo.bar");
    handler.handleRequest(requestContext);
    assertThat(this.response.getContentType()).isEqualTo("foo/bar");
    assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
}
Also used : ContentNegotiationManagerFactoryBean(cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) Resource(cn.taketoday.core.io.Resource) UrlBasedResource(cn.taketoday.core.io.UrlBasedResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) FileSystemResource(cn.taketoday.core.io.FileSystemResource) MediaType(cn.taketoday.http.MediaType) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 5 with ContentNegotiationManagerFactoryBean

use of cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method addContentDispositionHeaderToErrorResponse.

@Test
public void addContentDispositionHeaderToErrorResponse() throws Throwable {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.addMediaType("pdf", new MediaType("application", "pdf"));
    factory.afterPropertiesSet();
    var processor = new RequestResponseBodyMethodProcessor(Collections.singletonList(new StringHttpMessageConverter()), factory.getObject());
    this.servletRequest.setRequestURI("/hello.dataless");
    this.servletResponse.setStatus(400);
    processor.handleReturnValue(request, handlerMethod, "body");
    String header = servletResponse.getHeader("Content-Disposition");
    assertThat(header).isEqualTo("inline;filename=f.txt");
}
Also used : ContentNegotiationManagerFactoryBean(cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean) MediaType(cn.taketoday.http.MediaType) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Aggregations

ContentNegotiationManagerFactoryBean (cn.taketoday.web.accept.ContentNegotiationManagerFactoryBean)8 ContentNegotiationManager (cn.taketoday.web.accept.ContentNegotiationManager)6 Test (org.junit.jupiter.api.Test)6 ClassPathResource (cn.taketoday.core.io.ClassPathResource)4 FileSystemResource (cn.taketoday.core.io.FileSystemResource)4 Resource (cn.taketoday.core.io.Resource)4 UrlBasedResource (cn.taketoday.core.io.UrlBasedResource)4 MediaType (cn.taketoday.http.MediaType)4 ReactiveAdapterRegistry (cn.taketoday.core.ReactiveAdapterRegistry)2 SyncTaskExecutor (cn.taketoday.core.task.SyncTaskExecutor)2 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2