use of jakarta.servlet.http.Cookie in project spring-framework by spring-projects.
the class CookieLocaleResolverTests method testSetLocaleContextToNullWithDefault.
@Test
public void testSetLocaleContextToNullWithDefault() {
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);
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("");
}
use of jakarta.servlet.http.Cookie in project spring-framework by spring-projects.
the class CookieLocaleResolverTests method testSetAndResolveLocaleContext.
@Test
public void testSetAndResolveLocaleContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(request, response, 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(request);
assertThat(loc.getLocale().getLanguage()).isEqualTo("nl");
boolean condition = loc instanceof TimeZoneAwareLocaleContext;
assertThat(condition).isTrue();
assertThat(((TimeZoneAwareLocaleContext) loc).getTimeZone()).isNull();
}
use of jakarta.servlet.http.Cookie in project spring-framework by spring-projects.
the class CookieLocaleResolverTests method testSetAndResolveLocaleContextWithTimeZone.
@Test
public void testSetAndResolveLocaleContextWithTimeZone() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setLocaleContext(request, response, 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(request);
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 jakarta.servlet.http.Cookie in project spring-framework by spring-projects.
the class DefaultServerRequestBuilderTests method from.
@Test
void from() throws ServletException, IOException {
MockHttpServletRequest request = PathPatternsTestUtils.initRequest("POST", "https://example.com", true);
request.addHeader("foo", "bar");
request.setCookies(new MockCookie("foo", "bar"));
request.setAttribute("foo", "bar");
request.addParameter("foo", "bar");
request.setRemoteHost("127.0.0.1");
request.setRemotePort(80);
ServerRequest other = ServerRequest.create(request, messageConverters);
ServerRequest result = ServerRequest.from(other).method(HttpMethod.HEAD).header("baz", "qux").headers(httpHeaders -> httpHeaders.set("quux", "quuz")).cookie("baz", "qux").cookies(cookies -> cookies.set("quux", new Cookie("quux", "quuz"))).attribute("baz", "qux").attributes(attributes -> attributes.put("quux", "quuz")).param("baz", "qux").params(params -> params.set("quux", "quuz")).body("baz").build();
assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("bar");
assertThat(result.headers().asHttpHeaders().getFirst("baz")).isEqualTo("qux");
assertThat(result.headers().asHttpHeaders().getFirst("quux")).isEqualTo("quuz");
assertThat(result.cookies().getFirst("foo").getValue()).isEqualTo("bar");
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("qux");
assertThat(result.cookies().getFirst("quux").getValue()).isEqualTo("quuz");
assertThat(result.attributes().get("foo")).isEqualTo("bar");
assertThat(result.attributes().get("baz")).isEqualTo("qux");
assertThat(result.attributes().get("quux")).isEqualTo("quuz");
assertThat(result.params().getFirst("foo")).isEqualTo("bar");
assertThat(result.params().getFirst("baz")).isEqualTo("qux");
assertThat(result.params().getFirst("quux")).isEqualTo("quuz");
assertThat(result.remoteAddress()).contains(new InetSocketAddress("127.0.0.1", 80));
String body = result.body(String.class);
assertThat(body).isEqualTo("baz");
}
use of jakarta.servlet.http.Cookie in project spring-framework by spring-projects.
the class DefaultServerResponseBuilderTests method cookies.
@Test
public void cookies() {
MultiValueMap<String, Cookie> newCookies = new LinkedMultiValueMap<>();
newCookies.add("name", new Cookie("name", "value"));
ServerResponse response = ServerResponse.ok().cookies(cookies -> cookies.addAll(newCookies)).build();
assertThat(response.cookies()).isEqualTo(newCookies);
}
Aggregations