Skip to content

Commit 727f6cc

Browse files
committed
Protect the tests against changes to MockMvc's HTTP header ordering
Closes gh-359
1 parent 7cb91f9 commit 727f6cc

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

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

+32-21
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URLClassLoader;
2222
import java.util.Arrays;
2323
import java.util.HashMap;
24+
import java.util.List;
2425
import java.util.Map;
2526
import java.util.regex.Pattern;
2627

@@ -39,10 +40,12 @@
3940
import org.springframework.http.ResponseEntity;
4041
import org.springframework.restdocs.JUnitRestDocumentation;
4142
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationIntegrationTests.TestConfiguration;
43+
import org.springframework.restdocs.test.SnippetMatchers.HttpRequestMatcher;
4244
import org.springframework.test.context.ContextConfiguration;
4345
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4446
import org.springframework.test.context.web.WebAppConfiguration;
4547
import org.springframework.test.web.servlet.MockMvc;
48+
import org.springframework.test.web.servlet.MvcResult;
4649
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
4750
import org.springframework.util.FileSystemUtils;
4851
import org.springframework.web.bind.annotation.RequestMapping;
@@ -60,6 +63,7 @@
6063
import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
6164
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
6265
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
66+
import static org.springframework.restdocs.mockmvc.IterableEnumeration.iterable;
6367
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
6468
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
6569
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
@@ -317,8 +321,9 @@ public void responseFieldsSnippet() throws Exception {
317321

318322
mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON))
319323
.andExpect(status().isOk())
320-
.andDo(document("links", responseFields(
321-
fieldWithPath("a").description("The description"),
324+
.andDo(document("links",
325+
responseFields(fieldWithPath("a")
326+
.description("The description"),
322327
fieldWithPath("links").description("Links to other resources"))));
323328

324329
assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
@@ -388,38 +393,44 @@ public void preprocessedRequest() throws Exception {
388393

389394
Pattern pattern = Pattern.compile("(\"alpha\")");
390395

391-
mockMvc.perform(get("/").header("a", "alpha").header("b", "bravo")
392-
.contentType(MediaType.APPLICATION_JSON)
393-
.accept(MediaType.APPLICATION_JSON).content("{\"a\":\"alpha\"}"))
396+
MvcResult result = mockMvc
397+
.perform(get("/").header("a", "alpha").header("b", "bravo")
398+
.contentType(MediaType.APPLICATION_JSON)
399+
.accept(MediaType.APPLICATION_JSON).content("{\"a\":\"alpha\"}"))
394400
.andExpect(status().isOk()).andDo(document("original-request"))
395401
.andDo(document("preprocessed-request",
396402
preprocessRequest(prettyPrint(),
397403
removeHeaders("a", HttpHeaders.HOST,
398404
HttpHeaders.CONTENT_LENGTH),
399-
replacePattern(pattern, "\"<<beta>>\""))));
405+
replacePattern(pattern, "\"<<beta>>\""))))
406+
.andReturn();
400407

408+
HttpRequestMatcher originalRequest = httpRequest(asciidoctor(), RequestMethod.GET,
409+
"/");
410+
for (String headerName : iterable(result.getRequest().getHeaderNames())) {
411+
originalRequest.header(headerName, result.getRequest().getHeader(headerName));
412+
}
401413
assertThat(
402414
new File("build/generated-snippets/original-request/http-request.adoc"),
403-
is(snippet(asciidoctor())
404-
.withContents(
405-
httpRequest(asciidoctor(), RequestMethod.GET, "/")
406-
.header("a", "alpha").header("b", "bravo")
407-
.header("Content-Type", "application/json")
408-
.header("Accept",
409-
MediaType.APPLICATION_JSON_VALUE)
410-
.header("Host", "localhost:8080")
411-
.header("Content-Length", "13")
412-
.content("{\"a\":\"alpha\"}"))));
415+
is(snippet(asciidoctor()).withContents(originalRequest
416+
.header("Host", "localhost:8080").header("Content-Length", "13")
417+
.content("{\"a\":\"alpha\"}"))));
418+
HttpRequestMatcher preprocessedRequest = httpRequest(asciidoctor(),
419+
RequestMethod.GET, "/");
420+
List<String> removedHeaders = Arrays.asList("a", HttpHeaders.HOST,
421+
HttpHeaders.CONTENT_LENGTH);
422+
for (String headerName : iterable(result.getRequest().getHeaderNames())) {
423+
if (!removedHeaders.contains(headerName)) {
424+
preprocessedRequest.header(headerName,
425+
result.getRequest().getHeader(headerName));
426+
}
427+
}
413428
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
414429
assertThat(
415430
new File(
416431
"build/generated-snippets/preprocessed-request/http-request.adoc"),
417432
is(snippet(asciidoctor())
418-
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
419-
.header("b", "bravo")
420-
.header("Content-Type", "application/json")
421-
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
422-
.content(prettyPrinted))));
433+
.withContents(preprocessedRequest.content(prettyPrinted))));
423434
}
424435

425436
@Test

0 commit comments

Comments
 (0)