Search in sources :

Example 86 with MockServletContext

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));
}
Also used : ServletContextResource(org.springframework.web.context.support.ServletContextResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) ServletContextResource(org.springframework.web.context.support.ServletContextResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 87 with MockServletContext

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());
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 88 with MockServletContext

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());
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 89 with MockServletContext

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());
}
Also used : PageContext(javax.servlet.jsp.PageContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 90 with MockServletContext

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);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.test.MockFilterChain) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)152 Test (org.junit.Test)120 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)56 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)50 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 ServletContext (javax.servlet.ServletContext)22 ServletContextEvent (javax.servlet.ServletContextEvent)21 HashMap (java.util.HashMap)18 Before (org.junit.Before)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)16 TestBean (org.springframework.tests.sample.beans.TestBean)15 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)13 View (org.springframework.web.servlet.View)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebApplicationContext (org.springframework.web.context.WebApplicationContext)12 Resource (org.springframework.core.io.Resource)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9