Search in sources :

Example 96 with Cookie

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

Example 97 with Cookie

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();
}
Also used : Locale(java.util.Locale) Cookie(jakarta.servlet.http.Cookie) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 98 with Cookie

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"));
}
Also used : Locale(java.util.Locale) Cookie(jakarta.servlet.http.Cookie) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleLocaleContext(org.springframework.context.i18n.SimpleLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) LocaleContext(org.springframework.context.i18n.LocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) SimpleTimeZoneAwareLocaleContext(org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext) TimeZoneAwareLocaleContext(org.springframework.context.i18n.TimeZoneAwareLocaleContext) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 99 with Cookie

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");
}
Also used : MockCookie(org.springframework.web.testfixture.servlet.MockCookie) Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) InetSocketAddress(java.net.InetSocketAddress) MockCookie(org.springframework.web.testfixture.servlet.MockCookie) Test(org.junit.jupiter.api.Test)

Example 100 with Cookie

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);
}
Also used : Cookie(jakarta.servlet.http.Cookie) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CompletableFuture(java.util.concurrent.CompletableFuture) CacheControl(org.springframework.http.CacheControl) ArrayList(java.util.ArrayList) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) URI(java.net.URI) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) Cookie(jakarta.servlet.http.Cookie) ChronoUnit(java.time.temporal.ChronoUnit) CompletionStage(java.util.concurrent.CompletionStage) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) DateTimeFormatter(java.time.format.DateTimeFormatter) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

Cookie (jakarta.servlet.http.Cookie)197 Test (org.junit.jupiter.api.Test)137 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)45 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)40 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)30 Locale (java.util.Locale)19 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)19 MvcResult (org.springframework.test.web.servlet.MvcResult)15 Authentication (org.springframework.security.core.Authentication)11 Test (org.junit.Test)10 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)9 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)9 IOException (java.io.IOException)8 LocaleContext (org.springframework.context.i18n.LocaleContext)8 SimpleLocaleContext (org.springframework.context.i18n.SimpleLocaleContext)8 SimpleTimeZoneAwareLocaleContext (org.springframework.context.i18n.SimpleTimeZoneAwareLocaleContext)8 TimeZoneAwareLocaleContext (org.springframework.context.i18n.TimeZoneAwareLocaleContext)8 Map (java.util.Map)6 SavedCookie (org.springframework.security.web.savedrequest.SavedCookie)6 ModelAndView (org.springframework.web.servlet.ModelAndView)6