use of cn.taketoday.web.mock.MockHttpServletResponse in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolverTests method testSetAndResolveLocaleContext.
@Test
public void testSetAndResolveLocaleContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockServletRequestContext requestContext = new MockServletRequestContext(request, response);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(requestContext, new SimpleLocaleContext(new Locale("nl", "")));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(requestContext);
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull();
}
use of cn.taketoday.web.mock.MockHttpServletResponse in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolverTests method testSetAndResolveLocaleWithCountryAsLegacyJava.
@Test
public void testSetAndResolveLocaleWithCountryAsLegacyJava() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockServletRequestContext requestContext = new MockServletRequestContext(request, response);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLanguageTagCompliant(false);
resolver.setLocale(requestContext, new Locale("de", "AT"));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
assertThat(cookie).isNotNull();
assertThat(cookie.getName()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
assertThat(cookie.getDomain()).isNull();
assertThat(cookie.getPath()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_PATH);
assertThat(cookie.getSecure()).isFalse();
assertThat(cookie.getValue()).isEqualTo("de_AT");
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
Locale loc = resolver.resolveLocale(requestContext);
assertThat(loc.getLanguage()).isEqualTo("de");
assertThat(loc.getCountry()).isEqualTo("AT");
}
use of cn.taketoday.web.mock.MockHttpServletResponse in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolverTests method testCustomCookie.
@Test
public void testCustomCookie() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockServletRequestContext requestContext = new MockServletRequestContext(request, response);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setCookieName("LanguageKoek");
resolver.setCookieDomain(".springframework.org");
resolver.setCookiePath("/mypath");
resolver.setCookieMaxAge(10000);
resolver.setCookieSecure(true);
resolver.setLocale(requestContext, new Locale("nl", ""));
Cookie cookie = response.getCookie("LanguageKoek");
assertThat(cookie).isNotNull();
assertThat(cookie.getName()).isEqualTo("LanguageKoek");
assertThat(cookie.getDomain()).isEqualTo(".springframework.org");
assertThat(cookie.getPath()).isEqualTo("/mypath");
assertThat(cookie.getMaxAge()).isEqualTo(10000);
assertThat(cookie.getSecure()).isTrue();
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
resolver.setCookieName("LanguageKoek");
Locale loc = resolver.resolveLocale(requestContext);
assertThat(loc.getLanguage()).isEqualTo("nl");
}
use of cn.taketoday.web.mock.MockHttpServletResponse in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolverTests method testSetAndResolveLocaleContextWithTimeZone.
@Test
public void testSetAndResolveLocaleContextWithTimeZone() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockServletRequestContext requestContext = new MockServletRequestContext(request, response);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(requestContext, new SimpleTimeZoneAwareLocaleContext(new Locale("nl", ""), TimeZone.getTimeZone("GMT+1")));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
request = new MockHttpServletRequest();
request.setCookies(cookie);
resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(requestContext);
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1"));
}
use of cn.taketoday.web.mock.MockHttpServletResponse in project today-infrastructure by TAKETODAY.
the class SessionLocaleResolverTests method testResolveLocale.
@Test
public void testResolveLocale() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
RequestContext context = new ServletRequestContext(webApplicationContext, request, response);
RequestContextUtils.getRequiredSession(context).setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.GERMAN);
SessionLocaleResolver resolver = new SessionLocaleResolver();
assertThat(resolver.resolveLocale(context)).isEqualTo(Locale.GERMAN);
}
Aggregations