Skip to content

Commit 1ee96bb

Browse files
committed
Make tests tolerate varying presence of Content-Length header
In Framework 5.2, the Content-Length is now provided by MockMvc. In earlier versions it is not. This commit updates the tests to tolerate this difference in behaviour so that the compatibility tests pass again.
1 parent d34a81f commit 1ee96bb

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

spring-restdocs-mockmvc/src/test/java/org/springframework/restdocs/mockmvc/MockMvcRestDocumentationIntegrationTests.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
import java.nio.charset.StandardCharsets;
2626
import java.util.Arrays;
2727
import java.util.HashMap;
28+
import java.util.HashSet;
2829
import java.util.List;
2930
import java.util.Map;
31+
import java.util.Set;
3032
import java.util.regex.Pattern;
3133

3234
import javax.servlet.http.Cookie;
@@ -385,11 +387,17 @@ public void preprocessedRequest() throws Exception {
385387
replacePattern(pattern, "\"<<beta>>\""))))
386388
.andReturn();
387389
HttpRequestCondition originalRequest = httpRequest(TemplateFormats.asciidoctor(), RequestMethod.GET, "/");
390+
Set<String> mvcResultHeaderNames = new HashSet<>();
388391
for (String headerName : IterableEnumeration.of(result.getRequest().getHeaderNames())) {
389392
originalRequest.header(headerName, result.getRequest().getHeader(headerName));
393+
mvcResultHeaderNames.add(headerName);
390394
}
391-
assertThat(new File("build/generated-snippets/original-request/http-request.adoc")).has(content(originalRequest
392-
.header("Host", "localhost:8080").header("Content-Length", "13").content("{\"a\":\"alpha\"}")));
395+
originalRequest.header("Host", "localhost:8080");
396+
if (!mvcResultHeaderNames.contains("Content-Length")) {
397+
originalRequest.header("Content-Length", "13");
398+
}
399+
assertThat(new File("build/generated-snippets/original-request/http-request.adoc"))
400+
.has(content(originalRequest.content("{\"a\":\"alpha\"}")));
393401
HttpRequestCondition preprocessedRequest = httpRequest(TemplateFormats.asciidoctor(), RequestMethod.GET, "/");
394402
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
395403
for (String headerName : IterableEnumeration.of(result.getRequest().getHeaderNames())) {

0 commit comments

Comments
 (0)