Search in sources :

Example 6 with ResponseCookie

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

the class DefaultClientResponseTests method cookies.

@Test
public void cookies() {
    ResponseCookie cookie = ResponseCookie.from("foo", "bar").build();
    MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
    cookies.add("foo", cookie);
    given(mockResponse.getCookies()).willReturn(cookies);
    assertThat(defaultClientResponse.cookies()).isSameAs(cookies);
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) ResponseCookie(cn.taketoday.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 7 with ResponseCookie

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

the class MockServerHttpResponseTests method cookieHeaderSet.

@Test
void cookieHeaderSet() throws Exception {
    ResponseCookie foo11 = ResponseCookie.from("foo1", "bar1").build();
    ResponseCookie foo12 = ResponseCookie.from("foo1", "bar2").build();
    ResponseCookie foo21 = ResponseCookie.from("foo2", "baz1").build();
    ResponseCookie foo22 = ResponseCookie.from("foo2", "baz2").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    response.addCookie(foo11);
    response.addCookie(foo12);
    response.addCookie(foo21);
    response.addCookie(foo22);
    response.applyCookies();
    assertThat(response.getHeaders().get(HttpHeaders.SET_COOKIE)).isEqualTo(Arrays.asList("foo1=bar1", "foo1=bar2", "foo2=baz1", "foo2=baz2"));
}
Also used : ResponseCookie(cn.taketoday.http.ResponseCookie) MockServerHttpResponse(cn.taketoday.mock.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 8 with ResponseCookie

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

the class JettyClientHttpResponse method getCookies.

@Override
public MultiValueMap<String, ResponseCookie> getCookies() {
    var result = MultiValueMap.<String, ResponseCookie>fromLinkedHashMap();
    List<String> cookieHeader = getHeaders().get(HttpHeaders.SET_COOKIE);
    if (cookieHeader != null) {
        for (String header : cookieHeader) {
            List<HttpCookie> httpCookies = HttpCookie.parse(header);
            for (HttpCookie cookie : httpCookies) {
                result.add(cookie.getName(), ResponseCookie.fromClientResponse(cookie.getName(), cookie.getValue()).domain(cookie.getDomain()).path(cookie.getPath()).maxAge(cookie.getMaxAge()).secure(cookie.getSecure()).httpOnly(cookie.isHttpOnly()).sameSite(parseSameSite(header)).build());
            }
        }
    }
    return MultiValueMap.unmodifiable(result);
}
Also used : ResponseCookie(cn.taketoday.http.ResponseCookie) HttpCookie(java.net.HttpCookie)

Example 9 with ResponseCookie

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

the class UndertowServerHttpResponse method applyCookies.

@SuppressWarnings("deprecation")
@Override
protected void applyCookies() {
    for (Map.Entry<String, List<ResponseCookie>> entry : getCookies().entrySet()) {
        String name = entry.getKey();
        for (ResponseCookie httpCookie : entry.getValue()) {
            CookieImpl cookie = new CookieImpl(name, httpCookie.getValue());
            if (!httpCookie.getMaxAge().isNegative()) {
                cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds());
            }
            if (httpCookie.getDomain() != null) {
                cookie.setDomain(httpCookie.getDomain());
            }
            if (httpCookie.getPath() != null) {
                cookie.setPath(httpCookie.getPath());
            }
            cookie.setSecure(httpCookie.isSecure());
            cookie.setHttpOnly(httpCookie.isHttpOnly());
            cookie.setSameSiteMode(httpCookie.getSameSite());
            // getResponseCookies() is deprecated in Undertow 2.2
            this.exchange.getResponseCookies().putIfAbsent(name, cookie);
        }
    }
}
Also used : CookieImpl(io.undertow.server.handlers.CookieImpl) List(java.util.List) Map(java.util.Map) ResponseCookie(cn.taketoday.http.ResponseCookie)

Example 10 with ResponseCookie

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

the class UndertowServerHttpResponse method applyCookies.

@SuppressWarnings("deprecation")
@Override
protected void applyCookies() {
    for (Map.Entry<String, List<ResponseCookie>> entry : getCookies().entrySet()) {
        String name = entry.getKey();
        for (ResponseCookie httpCookie : entry.getValue()) {
            CookieImpl cookie = new CookieImpl(name, httpCookie.getValue());
            if (!httpCookie.getMaxAge().isNegative()) {
                cookie.setMaxAge((int) httpCookie.getMaxAge().getSeconds());
            }
            if (httpCookie.getDomain() != null) {
                cookie.setDomain(httpCookie.getDomain());
            }
            if (httpCookie.getPath() != null) {
                cookie.setPath(httpCookie.getPath());
            }
            cookie.setSecure(httpCookie.isSecure());
            cookie.setHttpOnly(httpCookie.isHttpOnly());
            cookie.setSameSiteMode(httpCookie.getSameSite());
            // getResponseCookies() is deprecated in Undertow 2.2
            this.exchange.getResponseCookies().putIfAbsent(name, cookie);
        }
    }
}
Also used : CookieImpl(io.undertow.server.handlers.CookieImpl) List(java.util.List) Map(java.util.Map) ResponseCookie(cn.taketoday.http.ResponseCookie)

Aggregations

ResponseCookie (cn.taketoday.http.ResponseCookie)12 Test (org.junit.jupiter.api.Test)8 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)2 MockServerHttpResponse (cn.taketoday.mock.http.server.reactive.MockServerHttpResponse)2 CookieImpl (io.undertow.server.handlers.CookieImpl)2 HttpCookie (java.net.HttpCookie)2 List (java.util.List)2 Map (java.util.Map)2