Search in sources :

Example 21 with MockHttpServletResponse

use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.

the class WebContentInterceptorTests method http10CachingConfigAndSpecificMapping.

@SuppressWarnings("deprecation")
@Test
public void http10CachingConfigAndSpecificMapping() throws Exception {
    WebContentInterceptor interceptor = new WebContentInterceptor();
    interceptor.setCacheSeconds(0);
    interceptor.setUseExpiresHeader(true);
    interceptor.setAlwaysMustRevalidate(true);
    Properties mappings = new Properties();
    // was **/*.cache.html
    mappings.setProperty("*/*.cache.html", "10");
    interceptor.setCacheMappings(mappings);
    // request.setRequestURI("http://example.org/foo/page.html");
    request.setRequestURI("foo/page.html");
    interceptor.preHandle(request, response, null);
    Iterable<String> expiresHeaders = response.getHeaders("Expires");
    assertThat(expiresHeaders, Matchers.iterableWithSize(1));
    Iterable<String> cacheControlHeaders = response.getHeaders("Cache-Control");
    assertThat(cacheControlHeaders, Matchers.contains("no-cache", "no-store"));
    Iterable<String> pragmaHeaders = response.getHeaders("Pragma");
    assertThat(pragmaHeaders, Matchers.contains("no-cache"));
    // request.setRequestURI("http://example.org/page.cache.html");
    request = new MockHttpServletRequest("GET", "foo/page.cache.html");
    response = new MockHttpServletResponse();
    interceptor.preHandle(request, response, null);
    expiresHeaders = response.getHeaders("Expires");
    assertThat(expiresHeaders, Matchers.iterableWithSize(1));
    cacheControlHeaders = response.getHeaders("Cache-Control");
    assertThat(cacheControlHeaders, Matchers.contains("max-age=10, must-revalidate"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Properties(java.util.Properties) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 22 with MockHttpServletResponse

use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.

the class LocaleResolverTests method doTest.

private void doTest(LocaleResolver localeResolver, boolean shouldSet) {
    // create mocks
    MockServletContext context = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest(context);
    request.addPreferredLocale(Locale.UK);
    MockHttpServletResponse response = new MockHttpServletResponse();
    // check original locale
    Locale locale = localeResolver.resolveLocale(request);
    assertEquals(locale, Locale.UK);
    // set new locale
    try {
        localeResolver.setLocale(request, response, Locale.GERMANY);
        if (!shouldSet)
            fail("should not be able to set Locale");
        // check new locale
        locale = localeResolver.resolveLocale(request);
        assertEquals(locale, Locale.GERMANY);
    } catch (UnsupportedOperationException ex) {
        if (shouldSet) {
            fail("should be able to set Locale");
        }
    }
    // check LocaleContext
    if (localeResolver instanceof LocaleContextResolver) {
        LocaleContextResolver localeContextResolver = (LocaleContextResolver) localeResolver;
        LocaleContext localeContext = localeContextResolver.resolveLocaleContext(request);
        if (shouldSet) {
            assertEquals(localeContext.getLocale(), Locale.GERMANY);
        } else {
            assertEquals(localeContext.getLocale(), Locale.UK);
        }
        assertTrue(localeContext instanceof TimeZoneAwareLocaleContext);
        assertNull(((TimeZoneAwareLocaleContext) localeContext).getTimeZone());
        if (localeContextResolver instanceof AbstractLocaleContextResolver) {
            ((AbstractLocaleContextResolver) localeContextResolver).setDefaultTimeZone(TimeZone.getTimeZone("GMT+1"));
            assertEquals(((TimeZoneAwareLocaleContext) localeContext).getTimeZone(), TimeZone.getTimeZone("GMT+1"));
        }
        try {
            localeContextResolver.setLocaleContext(request, response, new SimpleLocaleContext(Locale.US));
            if (!shouldSet) {
                fail("should not be able to set Locale");
            }
            localeContext = localeContextResolver.resolveLocaleContext(request);
            assertEquals(localeContext.getLocale(), Locale.US);
            if (localeContextResolver instanceof AbstractLocaleContextResolver) {
                assertEquals(((TimeZoneAwareLocaleContext) localeContext).getTimeZone(), TimeZone.getTimeZone("GMT+1"));
            } else {
                assertNull(((TimeZoneAwareLocaleContext) localeContext).getTimeZone());
            }
            localeContextResolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(Locale.GERMANY, TimeZone.getTimeZone("GMT+2")));
            localeContext = localeContextResolver.resolveLocaleContext(request);
            assertEquals(localeContext.getLocale(), Locale.GERMANY);
            assertTrue(localeContext instanceof TimeZoneAwareLocaleContext);
            assertEquals(((TimeZoneAwareLocaleContext) localeContext).getTimeZone(), TimeZone.getTimeZone("GMT+2"));
            localeContextResolver.setLocaleContext(request, response, new SimpleTimeZoneAwareLocaleContext(null, TimeZone.getTimeZone("GMT+3")));
            localeContext = localeContextResolver.resolveLocaleContext(request);
            assertEquals(localeContext.getLocale(), Locale.UK);
            assertTrue(localeContext instanceof TimeZoneAwareLocaleContext);
            assertEquals(((TimeZoneAwareLocaleContext) localeContext).getTimeZone(), TimeZone.getTimeZone("GMT+3"));
            if (localeContextResolver instanceof AbstractLocaleContextResolver) {
                ((AbstractLocaleContextResolver) localeContextResolver).setDefaultLocale(Locale.GERMANY);
                assertEquals(localeContext.getLocale(), Locale.GERMANY);
            }
        } catch (UnsupportedOperationException ex) {
            if (shouldSet) {
                fail("should be able to set Locale");
            }
        }
    }
}
Also used : Locale(java.util.Locale) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) LocaleContextResolver(org.springframework.web.servlet.LocaleContextResolver)

Example 23 with MockHttpServletResponse

use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.

the class HandlerMethodAnnotationDetectionTests method testRequestMappingMethod.

@Test
public void testRequestMappingMethod() throws Exception {
    String datePattern = "MM:dd:yyyy";
    SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
    String dateA = "11:01:2011";
    String dateB = "11:02:2011";
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
    request.setParameter("datePattern", datePattern);
    request.addHeader("header1", dateA);
    request.addHeader("header2", dateB);
    HandlerExecutionChain chain = handlerMapping.getHandler(request);
    assertNotNull(chain);
    ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertEquals("model attr1:", dateFormat.parse(dateA), mav.getModel().get("attr1"));
    assertEquals("model attr2:", dateFormat.parse(dateB), mav.getModel().get("attr2"));
    MockHttpServletResponse response = new MockHttpServletResponse();
    exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
    assertEquals("text/plain;charset=ISO-8859-1", response.getHeader("Content-Type"));
    assertEquals("failure", response.getContentAsString());
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) SimpleDateFormat(java.text.SimpleDateFormat) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 24 with MockHttpServletResponse

use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method ambiguousPathAndRequestMethod.

// SPR-9062
@Test
public void ambiguousPathAndRequestMethod() throws Exception {
    initServletWithControllers(AmbiguousPathAndRequestMethodController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bug/EXISTING");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(200, response.getStatus());
    assertEquals("Pattern", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 25 with MockHttpServletResponse

use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method modelAndViewWithStatus.

@Test
public void modelAndViewWithStatus() throws Exception {
    initServletWithControllers(ModelAndViewController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(422, response.getStatus());
    assertEquals("view", response.getForwardedUrl());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)171 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)162 Test (org.junit.Test)140 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)33 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)28 Before (org.junit.Before)19 MockServletContext (org.springframework.mock.web.test.MockServletContext)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 ModelAndView (org.springframework.web.servlet.ModelAndView)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)10 TestBean (org.springframework.tests.sample.beans.TestBean)9 HashMap (java.util.HashMap)8 FilterChain (javax.servlet.FilterChain)8 ServletException (javax.servlet.ServletException)7 HttpSession (javax.servlet.http.HttpSession)7 IOException (java.io.IOException)6 Map (java.util.Map)6 ServletRequest (javax.servlet.ServletRequest)6 ServletResponse (javax.servlet.ServletResponse)6