Search in sources :

Example 16 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-infrastructure by TAKETODAY.

the class CookieIntegrationTests method basicTest.

@ParameterizedHttpServerTest
public void basicTest(HttpServer httpServer) throws Exception {
    startServer(httpServer);
    URI url = new URI("http://localhost:" + port);
    String header = "SID=31d4d96e407aad42; lang=en-US";
    ResponseEntity<Void> response = new RestTemplate().exchange(RequestEntity.get(url).header("Cookie", header).build(), Void.class);
    Map<String, List<HttpCookie>> requestCookies = this.cookieHandler.requestCookies;
    assertThat(requestCookies.size()).isEqualTo(2);
    List<HttpCookie> list = requestCookies.get("SID");
    assertThat(list.size()).isEqualTo(1);
    assertThat(list.iterator().next().getValue()).isEqualTo("31d4d96e407aad42");
    list = requestCookies.get("lang");
    assertThat(list.size()).isEqualTo(1);
    assertThat(list.iterator().next().getValue()).isEqualTo("en-US");
    List<String> headerValues = response.getHeaders().get("Set-Cookie");
    assertThat(headerValues.size()).isEqualTo(2);
    List<String> cookie0 = splitCookie(headerValues.get(0));
    assertThat(cookie0.remove("SID=31d4d96e407aad42")).as("SID").isTrue();
    assertThat(cookie0.stream().map(String::toLowerCase)).containsExactlyInAnyOrder("path=/", "secure", "httponly");
    List<String> cookie1 = splitCookie(headerValues.get(1));
    assertThat(cookie1.remove("lang=en-US")).as("lang").isTrue();
    assertThat(cookie1.stream().map(String::toLowerCase)).containsExactlyInAnyOrder("path=/", "domain=example.com");
}
Also used : RestTemplate(cn.taketoday.web.client.RestTemplate) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) HttpCookie(cn.taketoday.http.HttpCookie)

Example 17 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-infrastructure by TAKETODAY.

the class MockServerHttpRequestTests method cookieHeaderSet.

@Test
void cookieHeaderSet() {
    HttpCookie foo11 = new HttpCookie("foo1", "bar1");
    HttpCookie foo12 = new HttpCookie("foo1", "bar2");
    HttpCookie foo21 = new HttpCookie("foo2", "baz1");
    HttpCookie foo22 = new HttpCookie("foo2", "baz2");
    MockServerHttpRequest request = MockServerHttpRequest.get("/").cookie(foo11, foo12, foo21, foo22).build();
    assertThat(request.getCookies().get("foo1")).isEqualTo(Arrays.asList(foo11, foo12));
    assertThat(request.getCookies().get("foo2")).isEqualTo(Arrays.asList(foo21, foo22));
    assertThat(request.getHeaders().get(HttpHeaders.COOKIE)).isEqualTo(Arrays.asList("foo1=bar1", "foo1=bar2", "foo2=baz1", "foo2=baz2"));
}
Also used : MockServerHttpRequest(cn.taketoday.mock.http.server.reactive.MockServerHttpRequest) HttpCookie(cn.taketoday.http.HttpCookie) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.

the class LightRequestContext method doGetCookies.

@Override
protected HttpCookie[] doGetCookies() {
    final HttpHeaders headers = request.getHeaders();
    final List<String> allCookie = headers.get(HttpHeaders.COOKIE);
    if (CollectionUtils.isEmpty(allCookie)) {
        return EMPTY_COOKIES;
    }
    // TODO doGetCookies
    return new HttpCookie[0];
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) HttpCookie(cn.taketoday.http.HttpCookie)

Example 19 with HttpCookie

use of cn.taketoday.http.HttpCookie in project today-framework by TAKETODAY.

the class CookieTokenResolver method getToken.

@Override
public String getToken(RequestContext context) {
    final String cookieName = this.cookieName;
    final HttpCookie cookie = context.getCookie(cookieName);
    if (cookie == null) {
        final ArrayList<HttpCookie> httpCookies = context.responseCookies();
        for (final HttpCookie httpCookie : httpCookies) {
            if (cookieName.equals(httpCookie.getName())) {
                return httpCookie.getValue();
            }
        }
        return null;
    }
    return cookie.getValue();
}
Also used : HttpCookie(cn.taketoday.http.HttpCookie)

Aggregations

HttpCookie (cn.taketoday.http.HttpCookie)19 List (java.util.List)4 Map (java.util.Map)4 Test (org.junit.jupiter.api.Test)4 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)2 DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)2 MultiValueMap (cn.taketoday.core.MultiValueMap)2 MockServerHttpRequest (cn.taketoday.mock.http.server.reactive.MockServerHttpRequest)2 MockRequestContext (cn.taketoday.web.MockRequestContext)2 RestTemplate (cn.taketoday.web.client.RestTemplate)2 Cookie (io.netty.handler.codec.http.cookie.Cookie)2 Cookie (jakarta.servlet.http.Cookie)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 Set (java.util.Set)2 TimeZone (java.util.TimeZone)2 CookieStore (org.apache.hc.client5.http.cookie.CookieStore)2 BasicClientCookie (org.apache.hc.client5.http.impl.cookie.BasicClientCookie)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2