use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class PathResourceResolverTests method checkServletContextResource.
// SPR-12432
@Test
public void checkServletContextResource() throws Exception {
Resource classpathLocation = new ClassPathResource("test/", PathResourceResolver.class);
MockServletContext context = new MockServletContext();
ServletContextResource servletContextLocation = new ServletContextResource(context, "/webjars/");
ServletContextResource resource = new ServletContextResource(context, "/webjars/webjar-foo/1.0/foo.js");
assertFalse(this.resolver.checkResource(resource, classpathLocation));
assertTrue(this.resolver.checkResource(resource, servletContextLocation));
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ResourceHttpRequestHandlerTests method getResourceWithRegisteredMediaType.
// SPR-13658
@Test
public void getResourceWithRegisteredMediaType() throws Exception {
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
factory.addMediaType("css", new MediaType("foo", "bar"));
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.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
handler.handleRequest(this.request, this.response);
assertEquals("foo/bar", this.response.getContentType());
assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ResourceHttpRequestHandlerTests method initAllowedLocationsWithExplicitConfiguration.
@Test
public void initAllowedLocationsWithExplicitConfiguration() throws Exception {
ClassPathResource location1 = new ClassPathResource("test/", getClass());
ClassPathResource location2 = new ClassPathResource("testalternatepath/", getClass());
PathResourceResolver pathResolver = new PathResourceResolver();
pathResolver.setAllowedLocations(location1);
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
handler.setResourceResolvers(Collections.singletonList(pathResolver));
handler.setServletContext(new MockServletContext());
handler.setLocations(Arrays.asList(location1, location2));
handler.afterPropertiesSet();
Resource[] locations = pathResolver.getAllowedLocations();
assertEquals(1, locations.length);
assertEquals("test/", ((ClassPathResource) locations[0]).getPath());
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class HtmlEscapeTagTests method htmlEscapeTagWithContextParamFalse.
@Test
public void htmlEscapeTagWithContextParamFalse() throws JspException {
PageContext pc = createPageContext();
MockServletContext sc = (MockServletContext) pc.getServletContext();
HtmlEscapeTag tag = new HtmlEscapeTag();
tag.setPageContext(pc);
tag.doStartTag();
sc.addInitParameter(WebUtils.HTML_ESCAPE_CONTEXT_PARAM, "false");
assertTrue("Correct default", !tag.getRequestContext().isDefaultHtmlEscape());
tag.setDefaultHtmlEscape(true);
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
assertTrue("Correctly enabled", tag.getRequestContext().isDefaultHtmlEscape());
tag.setDefaultHtmlEscape(false);
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
assertTrue("Correctly disabled", !tag.getRequestContext().isDefaultHtmlEscape());
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ResourceUrlProviderJavaConfigTests method setup.
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter());
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(WebConfig.class);
context.refresh();
this.request = new MockHttpServletRequest("GET", "/");
this.request.setContextPath("/myapp");
this.response = new MockHttpServletResponse();
Object urlProvider = context.getBean(ResourceUrlProvider.class);
this.request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider);
}
Aggregations