use of cn.taketoday.web.servlet.MockServletRequestContext in project today-infrastructure by TAKETODAY.
the class ContentNegotiatingViewResolverTests method createViewResolver.
@BeforeEach
public void createViewResolver() {
wac.setServletContext(new MockServletContext());
wac.refresh();
viewResolver = new ContentNegotiatingViewResolver();
viewResolver.setApplicationContext(wac);
request = new MockHttpServletRequest("GET", "/test");
MockHttpServletResponse response = new MockHttpServletResponse();
this.requestContext = new MockServletRequestContext(wac, request, response);
RequestContextHolder.set(requestContext);
}
use of cn.taketoday.web.servlet.MockServletRequestContext in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolverTests method testResolveLocaleWithoutCookie.
@Test
public void testResolveLocaleWithoutCookie() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.TAIWAN);
MockServletRequestContext requestContext = new MockServletRequestContext(request, null);
CookieLocaleResolver resolver = new CookieLocaleResolver();
Locale loc = resolver.resolveLocale(requestContext);
assertThat(loc).isEqualTo(request.getLocale());
}
use of cn.taketoday.web.servlet.MockServletRequestContext in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolverTests method testSetAndResolveLocaleWithCountry.
@Test
public void testSetAndResolveLocaleWithCountry() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockServletRequestContext requestContext = new MockServletRequestContext(request, response);
CookieLocaleResolver resolver = new CookieLocaleResolver();
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.servlet.MockServletRequestContext in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolverTests method testSetAndResolveLocaleContextWithTimeZoneOnly.
@Test
public void testSetAndResolveLocaleContextWithTimeZoneOnly() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockServletRequestContext requestContext = new MockServletRequestContext(request, response);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(requestContext, new SimpleTimeZoneAwareLocaleContext(null, TimeZone.getTimeZone("GMT+1")));
Cookie cookie = response.getCookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
request = new MockHttpServletRequest();
request.addPreferredLocale(Locale.GERMANY);
request.setCookies(cookie);
requestContext = new MockServletRequestContext(request, response);
resolver = new CookieLocaleResolver();
LocaleContext loc = resolver.resolveLocaleContext(requestContext);
assertThat(loc.getLocale()).isEqualTo(Locale.GERMANY);
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isEqualTo(TimeZone.getTimeZone("GMT+1"));
}
use of cn.taketoday.web.servlet.MockServletRequestContext in project today-infrastructure by TAKETODAY.
the class CookieLocaleResolverTests method testSetLocaleContextToNullWithDefault.
@Test
public void testSetLocaleContextToNullWithDefault() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockServletRequestContext requestContext = new MockServletRequestContext(request, response);
request.addPreferredLocale(Locale.TAIWAN);
Cookie cookie = new Cookie(CookieLocaleResolver.DEFAULT_COOKIE_NAME, Locale.UK.toString());
request.setCookies(cookie);
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultLocale(Locale.CANADA_FRENCH);
resolver.setDefaultTimeZone(TimeZone.getTimeZone("GMT+1"));
resolver.setLocaleContext(requestContext, null);
Locale locale = (Locale) request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
assertThat(locale).isEqualTo(Locale.CANADA_FRENCH);
TimeZone timeZone = (TimeZone) request.getAttribute(CookieLocaleResolver.TIME_ZONE_REQUEST_ATTRIBUTE_NAME);
assertThat(timeZone).isEqualTo(TimeZone.getTimeZone("GMT+1"));
Cookie[] cookies = response.getCookies();
assertThat(cookies.length).isEqualTo(1);
Cookie localeCookie = cookies[0];
assertThat(localeCookie.getName()).isEqualTo(CookieLocaleResolver.DEFAULT_COOKIE_NAME);
assertThat(localeCookie.getValue()).isEqualTo("");
}
Aggregations