Search in sources :

Example 51 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest 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 52 with MockHttpServletRequest

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

the class CglibProxyControllerTests method typeLevel.

@Test
public void typeLevel() throws Exception {
    initServlet(TypeLevelImpl.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    assertEquals("doIt", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 53 with MockHttpServletRequest

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

the class CglibProxyControllerTests method typeAndMethodLevel.

@Test
public void typeAndMethodLevel() throws Exception {
    initServlet(TypeAndMethodLevelImpl.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/bookings");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    assertEquals("doIt", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 54 with MockHttpServletRequest

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

the class CglibProxyControllerTests method methodLevel.

@Test
public void methodLevel() throws Exception {
    initServlet(MethodLevelImpl.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    assertEquals("doIt", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 55 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest 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)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)646 Test (org.junit.Test)540 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)330 Before (org.junit.Before)78 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)77 MockServletContext (org.springframework.mock.web.test.MockServletContext)56 TestBean (org.springframework.tests.sample.beans.TestBean)43 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)30 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)29 ITestBean (org.springframework.tests.sample.beans.ITestBean)28 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)28 ServletException (javax.servlet.ServletException)27 HashMap (java.util.HashMap)26 FilterChain (javax.servlet.FilterChain)26 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)23 Cookie (javax.servlet.http.Cookie)22