Skip to content

Fix MockHttpServletRequest.setCookies to produce single Cookie header #23074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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 @@ -41,6 +41,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
Expand All @@ -58,6 +59,7 @@

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.LinkedCaseInsensitiveMap;
Expand Down Expand Up @@ -973,12 +975,18 @@ public String getAuthType() {

public void setCookies(@Nullable Cookie... cookies) {
this.cookies = (ObjectUtils.isEmpty(cookies) ? null : cookies);
this.headers.remove(HttpHeaders.COOKIE);
if (this.cookies != null) {
Arrays.stream(this.cookies)
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue()))
.forEach(value -> doAddHeaderValue(HttpHeaders.COOKIE, value, false));
if (this.cookies == null) {
removeHeader(HttpHeaders.COOKIE);
}
else {
doAddHeaderValue(HttpHeaders.COOKIE, encodeCookies(this.cookies), true);
}
}

private static String encodeCookies(@NonNull Cookie... cookies) {
return Arrays.stream(cookies)
.map(c -> c.getName() + '=' + (c.getValue() == null ? "" : c.getValue()))
.collect(Collectors.joining("; "));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,20 @@ public void cookies() {
Cookie cookie2 = new Cookie("baz", "qux");
request.setCookies(cookie1, cookie2);

Cookie[] cookies = request.getCookies();
List<String> cookieHeaders = Collections.list(request.getHeaders("Cookie"));

assertThat(cookies.length).isEqualTo(2);
assertThat(cookies[0].getName()).isEqualTo("foo");
assertThat(cookies[0].getValue()).isEqualTo("bar");
assertThat(cookies[1].getName()).isEqualTo("baz");
assertThat(cookies[1].getValue()).isEqualTo("qux");
assertThat(cookieHeaders).isEqualTo(Arrays.asList("foo=bar", "baz=qux"));
assertThat(request.getCookies())
.describedAs("Raw cookies stored as is")
.hasSize(2)
.satisfies(subject -> {
assertThat(subject[0].getName()).isEqualTo("foo");
assertThat(subject[0].getValue()).isEqualTo("bar");
assertThat(subject[1].getName()).isEqualTo("baz");
assertThat(subject[1].getValue()).isEqualTo("qux");
});

assertThat(Collections.list(request.getHeaders(HttpHeaders.COOKIE)))
.describedAs("Cookies -> Header conversion works as expected per RFC6265")
.hasSize(1)
.hasOnlyOneElementSatisfying(header -> assertThat(header).isEqualTo("foo=bar; baz=qux"));
}

@Test
Expand Down