Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import jakarta.servlet.http.Cookie;
Expand Down Expand Up @@ -169,7 +170,7 @@ private void addCookies(HttpHeaders headers, AbstractMockHttpServletRequestBuild
List<String> values = headers.get(HttpHeaders.COOKIE);
if (!ObjectUtils.isEmpty(values)) {
values.stream()
.flatMap(header -> StringUtils.commaDelimitedListToSet(header).stream())
.flatMap(header -> Arrays.stream(StringUtils.tokenizeToStringArray(header, ";")))
.map(value -> {
String[] cookieParts = StringUtils.split(value, "=");
Assert.isTrue(cookieParts != null && cookieParts.length == 2, "Invalid cookie: '" + value + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
Expand Down Expand Up @@ -213,6 +214,38 @@ void cookies() {
.expectStatus().isOk()
.expectBody().jsonPath("$.headers.Cookie").isEqualTo("foo=bar");
}

@Test
void multipleCookie() {
RestTestClientTests.this.client.get().uri("/test")
.cookie("foo", "bar")
.cookie("baz", "qux")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.headers.Cookie").isEqualTo("foo=bar; baz=qux")
.jsonPath("$.cookies[0].name").isEqualTo("foo")
.jsonPath("$.cookies[0].value").isEqualTo("bar")
.jsonPath("$.cookies[1].name").isEqualTo("baz")
.jsonPath("$.cookies[1].value").isEqualTo("qux");
}

@Test
void multipleCookies() {
RestTestClientTests.this.client.get().uri("/test")
.cookies(cookies -> {
cookies.add("foo", "bar");
cookies.add("baz", "qux");
})
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.headers.Cookie").isEqualTo("foo=bar; baz=qux")
.jsonPath("$.cookies[0].name").isEqualTo("foo")
.jsonPath("$.cookies[0].value").isEqualTo("bar")
.jsonPath("$.cookies[1].name").isEqualTo("baz")
.jsonPath("$.cookies[1].value").isEqualTo("qux");
}
}


Expand Down Expand Up @@ -345,7 +378,8 @@ public Map<String, Object> handle(
return Map.of(
"method", request.getMethod(),
"uri", request.getRequestURI(),
"headers", headers.toSingleValueMap()
"headers", headers.toSingleValueMap(),
"cookies", ObjectUtils.toObjectArray(request.getCookies())
);
}

Expand Down
Loading