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"));
}
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());
}
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());
}
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());
}
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");
}
}
}
}
Aggregations