Search in sources :

Example 1 with MockServletRequestContext

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);
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockServletContext(cn.taketoday.web.mock.MockServletContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with MockServletRequestContext

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());
}
Also used : Locale(java.util.Locale) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) Test(org.junit.jupiter.api.Test)

Example 3 with MockServletRequestContext

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");
}
Also used : Locale(java.util.Locale) Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) Test(org.junit.jupiter.api.Test)

Example 4 with MockServletRequestContext

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"));
}
Also used : Cookie(jakarta.servlet.http.Cookie) TimeZoneAwareLocaleContext(cn.taketoday.core.i18n.TimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(cn.taketoday.core.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(cn.taketoday.core.i18n.SimpleLocaleContext) LocaleContext(cn.taketoday.core.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(cn.taketoday.core.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(cn.taketoday.core.i18n.TimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(cn.taketoday.core.i18n.SimpleTimeZoneAwareLocaleContext) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) Test(org.junit.jupiter.api.Test)

Example 5 with MockServletRequestContext

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("");
}
Also used : Cookie(jakarta.servlet.http.Cookie) Locale(java.util.Locale) TimeZone(java.util.TimeZone) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletRequestContext (cn.taketoday.web.servlet.MockServletRequestContext)90 Test (org.junit.jupiter.api.Test)74 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)43 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)43 Cookie (jakarta.servlet.http.Cookie)40 Locale (java.util.Locale)30 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)22 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)22 LocaleContext (cn.taketoday.core.i18n.LocaleContext)20 SimpleLocaleContext (cn.taketoday.core.i18n.SimpleLocaleContext)20 SimpleTimeZoneAwareLocaleContext (cn.taketoday.core.i18n.SimpleTimeZoneAwareLocaleContext)20 TimeZoneAwareLocaleContext (cn.taketoday.core.i18n.TimeZoneAwareLocaleContext)20 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)16 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)16 BeforeEach (org.junit.jupiter.api.BeforeEach)10 AnnotationConfigServletWebApplicationContext (cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebApplicationContext)6 ServletException (jakarta.servlet.ServletException)4 TimeZone (java.util.TimeZone)4 RequestPath (cn.taketoday.http.server.RequestPath)2 StaticWebApplicationContext (cn.taketoday.web.context.support.StaticWebApplicationContext)2