From 5d9b7ec575e8aad87914f5f2def559c4fb2b35cd Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 17 Oct 2024 21:16:36 +0900 Subject: [PATCH] Use single Cookie header in HttpRequestSnippet See https://www.rfc-editor.org/rfc/rfc6265#section-5.4 See gh-947 --- .../springframework/restdocs/http/HttpRequestSnippet.java | 6 +++++- .../restdocs/http/HttpRequestSnippetTests.java | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java index ef761720..403b83be 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java @@ -103,8 +103,12 @@ private List> getHeaders(OperationRequest request) { } } + List cookies = new ArrayList<>(); for (RequestCookie cookie : request.getCookies()) { - headers.add(header(HttpHeaders.COOKIE, String.format("%s=%s", cookie.getName(), cookie.getValue()))); + cookies.add(String.format("%s=%s", cookie.getName(), cookie.getValue())); + } + if (!cookies.isEmpty()) { + headers.add(header(HttpHeaders.COOKIE, String.join("; ", cookies))); } if (requiresFormEncodingContentTypeHeader(request)) { diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java index 41cccdd1..447e00c0 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java @@ -82,8 +82,7 @@ public void getRequestWithCookies() throws IOException { .build()); assertThat(this.generatedSnippets.httpRequest()) .is(httpRequest(RequestMethod.GET, "/foo").header(HttpHeaders.HOST, "localhost") - .header(HttpHeaders.COOKIE, "name1=value1") - .header(HttpHeaders.COOKIE, "name2=value2")); + .header(HttpHeaders.COOKIE, "name1=value1; name2=value2")); } @Test