use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.
// SPR-14577
@Test
@SuppressWarnings("deprecation")
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.setFavorPathExtension(false);
factory.afterPropertiesSet();
ContentNegotiationManager manager = factory.getObject();
List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.setServletContext(new MockServletContext());
handler.setLocations(paths);
handler.setContentNegotiationManager(manager);
handler.afterPropertiesSet();
this.request.addHeader("Accept", "application/json,text/plain,*/*");
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
handler.handleRequest(this.request, this.response);
assertThat(this.response.getContentType()).isEqualTo("text/html");
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ResourceHttpRequestHandlerTests method getResourceWithMediaTypeResolvedThroughServletContext.
// SPR-14368
@Test
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
MockServletContext servletContext = new MockServletContext() {
@Override
public String getMimeType(String filePath) {
return "foo/bar";
}
};
List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.setServletContext(servletContext);
handler.setLocations(paths);
handler.afterPropertiesSet();
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "");
request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
handler.handleRequest(request, this.response);
assertThat(this.response.getContentType()).isEqualTo("foo/bar");
assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ResourceUrlProviderTests method initializeOnCurrentContext.
@Test
@SuppressWarnings("resource")
void initializeOnCurrentContext() {
AnnotationConfigWebApplicationContext parentContext = new AnnotationConfigWebApplicationContext();
parentContext.setServletContext(new MockServletContext());
parentContext.register(ParentHandlerMappingConfiguration.class);
AnnotationConfigWebApplicationContext childContext = new AnnotationConfigWebApplicationContext();
childContext.setParent(parentContext);
childContext.setServletContext(new MockServletContext());
childContext.register(HandlerMappingConfiguration.class);
parentContext.refresh();
childContext.refresh();
ResourceUrlProvider parentUrlProvider = parentContext.getBean(ResourceUrlProvider.class);
assertThat(parentUrlProvider.getHandlerMap()).isEmpty();
assertThat(parentUrlProvider.isAutodetect()).isTrue();
ResourceUrlProvider childUrlProvider = childContext.getBean(ResourceUrlProvider.class);
assertThat(childUrlProvider.getHandlerMap()).containsOnlyKeys("/resources/**");
assertThat(childUrlProvider.isAutodetect()).isFalse();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class RequestScopedControllerAdviceIntegrationTests method loadContextWithRequestScopedControllerAdvice.
// gh-23985
@Test
void loadContextWithRequestScopedControllerAdvice() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(Config.class);
assertThatCode(context::refresh).doesNotThrowAnyException();
List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(context);
assertThat(adviceBeans).hasSize(1);
//
assertThat(adviceBeans.get(0)).returns(RequestScopedControllerAdvice.class, //
ControllerAdviceBean::getBeanType).returns(42, ControllerAdviceBean::getOrder);
context.close();
}
use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.
the class ServletContextSupportTests method testServletContextResourcePatternResolverWithUnboundedPatternPath.
@Test
public void testServletContextResourcePatternResolverWithUnboundedPatternPath() throws IOException {
final Set<String> dirs = new HashSet<>();
dirs.add("/WEB-INF/mydir1/");
dirs.add("/WEB-INF/mydir2/");
final Set<String> paths = new HashSet<>();
paths.add("/WEB-INF/mydir2/context2.xml");
paths.add("/WEB-INF/mydir2/mydir3/");
MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {
@Override
public Set<String> getResourcePaths(String path) {
if ("/WEB-INF/".equals(path)) {
return dirs;
}
if ("/WEB-INF/mydir1/".equals(path)) {
return Collections.singleton("/WEB-INF/mydir1/context1.xml");
}
if ("/WEB-INF/mydir2/".equals(path)) {
return paths;
}
if ("/WEB-INF/mydir2/mydir3/".equals(path)) {
return Collections.singleton("/WEB-INF/mydir2/mydir3/context3.xml");
}
return null;
}
};
ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
Resource[] found = rpr.getResources("/WEB-INF/**/*.xml");
Set<String> foundPaths = new HashSet<>();
for (Resource resource : found) {
foundPaths.add(((ServletContextResource) resource).getPath());
}
assertThat(foundPaths.size()).isEqualTo(3);
assertThat(foundPaths.contains("/WEB-INF/mydir1/context1.xml")).isTrue();
assertThat(foundPaths.contains("/WEB-INF/mydir2/context2.xml")).isTrue();
assertThat(foundPaths.contains("/WEB-INF/mydir2/mydir3/context3.xml")).isTrue();
}
Aggregations