Search in sources :

Example 26 with MockHttpServletResponse

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();
}
Also used : Locale(java.util.Locale) Cookie(jakarta.servlet.http.Cookie) SimpleLocaleContext(cn.taketoday.core.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(cn.taketoday.core.i18n.TimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(cn.taketoday.core.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(cn.taketoday.core.i18n.SimpleLocaleContext) LocaleContext(cn.taketoday.core.i18n.LocaleContext) 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 27 with MockHttpServletResponse

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");
}
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 28 with MockHttpServletResponse

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");
}
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 29 with MockHttpServletResponse

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"));
}
Also used : Locale(java.util.Locale) 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 30 with MockHttpServletResponse

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

Aggregations

MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)75 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)61 Test (org.junit.jupiter.api.Test)51 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)22 MockServletRequestContext (cn.taketoday.web.servlet.MockServletRequestContext)17 BeforeEach (org.junit.jupiter.api.BeforeEach)16 RequestContext (cn.taketoday.web.RequestContext)14 HashMap (java.util.HashMap)14 Locale (java.util.Locale)13 MockServletContext (cn.taketoday.web.mock.MockServletContext)12 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)12 Cookie (jakarta.servlet.http.Cookie)11 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)9 HttpMethod (cn.taketoday.http.HttpMethod)8 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)7 FilterChain (jakarta.servlet.FilterChain)7 ServletException (jakarta.servlet.ServletException)7 IOException (java.io.IOException)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 HttpHeaders (cn.taketoday.http.HttpHeaders)6