Search in sources :

Example 76 with MockHttpServletRequest

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

the class CookieLocaleResolverTests method testResolveLocaleWithoutCookie.

@Test
public void testResolveLocaleWithoutCookie() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    Locale loc = resolver.resolveLocale(request);
    assertEquals(request.getLocale(), loc);
}
Also used : Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 77 with MockHttpServletRequest

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

the class CookieLocaleResolverTests method testResolveLocaleWithoutCookieAndDefaultLocale.

@Test
public void testResolveLocaleWithoutCookieAndDefaultLocale() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setDefaultLocale(Locale.GERMAN);
    Locale loc = resolver.resolveLocale(request);
    assertEquals(Locale.GERMAN, loc);
}
Also used : Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 78 with MockHttpServletRequest

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

the class CookieLocaleResolverTests method testSetLocaleToNull.

@Test
public void testSetLocaleToNull() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
    request.setCookies(cookie);
    MockHttpServletResponse response = new MockHttpServletResponse();
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setLocale(request, response, null);
    Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
    assertEquals(Locale.TAIWAN, locale);
    Cookie[] cookies = response.getCookies();
    assertEquals(1, cookies.length);
    Cookie localeCookie = cookies[0];
    assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
    assertEquals("", localeCookie.getValue());
}
Also used : Cookie(javax.servlet.http.Cookie) Locale(java.util.Locale) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 79 with MockHttpServletRequest

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

the class CookieLocaleResolverTests method testResolveLocaleContextWithoutCookie.

@Test
public void testResolveLocaleContextWithoutCookie() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    LocaleContext loc = resolver.resolveLocaleContext(request);
    assertEquals(request.getLocale(), loc.getLocale());
    assertTrue(loc instanceof TimeZoneAwareLocaleContext);
    assertNull(((TimeZoneAwareLocaleContext) loc).getTimeZone());
}
Also used : SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 80 with MockHttpServletRequest

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

the class CookieLocaleResolverTests method testSetLocaleContextToNullWithDefault.

@Test
public void testSetLocaleContextToNullWithDefault() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.TAIWAN);
    Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
    request.setCookies(cookie);
    MockHttpServletResponse response = new MockHttpServletResponse();
    CookieLocaleResolver resolver = new CookieLocaleResolver();
    resolver.setDefaultLocale(Locale.CANADA_FRENCH);
    resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1"));
    resolver.setLocaleContext(request, response, null);
    Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
    assertEquals(Locale.CANADA_FRENCH, locale);
    TimeZone timeZone = (TimeZone) request.getAttribute(CookieLocaleResolver.TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
    assertEquals(TimeZone.getTimeZone("GMT+1"), timeZone);
    Cookie[] cookies = response.getCookies();
    assertEquals(1, cookies.length);
    Cookie localeCookie = cookies[0];
    assertEquals(CookieLocaleResolver.DEFAULT_COOKIE_NAME, localeCookie.getName());
    assertEquals("", localeCookie.getValue());
}
Also used : Cookie(javax.servlet.http.Cookie) Locale(java.util.Locale) TimeZone(java.util.TimeZone) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

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