Search in sources :

Example 1 with ResponseCookie

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

the class ServerHttpResponseTests method beforeCommitActionWithSetComplete.

@Test
void beforeCommitActionWithSetComplete() {
    ResponseCookie cookie = ResponseCookie.from("ID", "123").build();
    TestServerHttpResponse response = new TestServerHttpResponse();
    response.beforeCommit(() -> {
        response.getCookies().add(cookie.getName(), cookie);
        return Mono.empty();
    });
    response.setComplete().block();
    assertThat(response.statusCodeWritten).isTrue();
    assertThat(response.headersWritten).isTrue();
    assertThat(response.cookiesWritten).isTrue();
    assertThat(response.body.isEmpty()).isTrue();
    assertThat(response.getCookies().getFirst("ID")).isSameAs(cookie);
}
Also used : ResponseCookie(cn.taketoday.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 2 with ResponseCookie

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

the class ServerHttpResponseTests method beforeCommitWithComplete.

@Test
void beforeCommitWithComplete() {
    ResponseCookie cookie = ResponseCookie.from("ID", "123").build();
    TestServerHttpResponse response = new TestServerHttpResponse();
    response.beforeCommit(() -> Mono.fromRunnable(() -> response.getCookies().add(cookie.getName(), cookie)));
    response.writeWith(Flux.just(wrap("a"), wrap("b"), wrap("c"))).block();
    assertThat(response.statusCodeWritten).isTrue();
    assertThat(response.headersWritten).isTrue();
    assertThat(response.cookiesWritten).isTrue();
    assertThat(response.getCookies().getFirst("ID")).isSameAs(cookie);
    assertThat(response.body.size()).isEqualTo(3);
    assertThat(new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("a");
    assertThat(new String(response.body.get(1).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("b");
    assertThat(new String(response.body.get(2).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("c");
}
Also used : ResponseCookie(cn.taketoday.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 3 with ResponseCookie

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

the class JettyClientHttpResponse method getCookies.

@Override
public MultiValueMap<String, ResponseCookie> getCookies() {
    DefaultMultiValueMap<String, ResponseCookie> result = MultiValueMap.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 4 with ResponseCookie

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

the class ServerHttpResponseTests method beforeCommitActionWithSetComplete.

@Test
void beforeCommitActionWithSetComplete() {
    ResponseCookie cookie = ResponseCookie.from("ID", "123").build();
    TestServerHttpResponse response = new TestServerHttpResponse();
    response.beforeCommit(() -> {
        response.getCookies().add(cookie.getName(), cookie);
        return Mono.empty();
    });
    response.setComplete().block();
    assertThat(response.statusCodeWritten).isTrue();
    assertThat(response.headersWritten).isTrue();
    assertThat(response.cookiesWritten).isTrue();
    assertThat(response.body.isEmpty()).isTrue();
    assertThat(response.getCookies().getFirst("ID")).isSameAs(cookie);
}
Also used : ResponseCookie(cn.taketoday.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 5 with ResponseCookie

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

the class ServerHttpResponseTests method beforeCommitWithComplete.

@Test
void beforeCommitWithComplete() {
    ResponseCookie cookie = ResponseCookie.from("ID", "123").build();
    TestServerHttpResponse response = new TestServerHttpResponse();
    response.beforeCommit(() -> Mono.fromRunnable(() -> response.getCookies().add(cookie.getName(), cookie)));
    response.writeWith(Flux.just(wrap("a"), wrap("b"), wrap("c"))).block();
    assertThat(response.statusCodeWritten).isTrue();
    assertThat(response.headersWritten).isTrue();
    assertThat(response.cookiesWritten).isTrue();
    assertThat(response.getCookies().getFirst("ID")).isSameAs(cookie);
    assertThat(response.body.size()).isEqualTo(3);
    assertThat(new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("a");
    assertThat(new String(response.body.get(1).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("b");
    assertThat(new String(response.body.get(2).asByteBuffer().array(), StandardCharsets.UTF_8)).isEqualTo("c");
}
Also used : ResponseCookie(cn.taketoday.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

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