|
21 | 21 | import java.net.URLClassLoader;
|
22 | 22 | import java.util.Arrays;
|
23 | 23 | import java.util.HashMap;
|
| 24 | +import java.util.List; |
24 | 25 | import java.util.Map;
|
25 | 26 | import java.util.regex.Pattern;
|
26 | 27 |
|
|
39 | 40 | import org.springframework.http.ResponseEntity;
|
40 | 41 | import org.springframework.restdocs.JUnitRestDocumentation;
|
41 | 42 | import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationIntegrationTests.TestConfiguration;
|
| 43 | +import org.springframework.restdocs.test.SnippetMatchers.HttpRequestMatcher; |
42 | 44 | import org.springframework.test.context.ContextConfiguration;
|
43 | 45 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
44 | 46 | import org.springframework.test.context.web.WebAppConfiguration;
|
45 | 47 | import org.springframework.test.web.servlet.MockMvc;
|
| 48 | +import org.springframework.test.web.servlet.MvcResult; |
46 | 49 | import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
47 | 50 | import org.springframework.util.FileSystemUtils;
|
48 | 51 | import org.springframework.web.bind.annotation.RequestMapping;
|
|
60 | 63 | import static org.springframework.restdocs.headers.HeaderDocumentation.responseHeaders;
|
61 | 64 | import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
|
62 | 65 | import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
|
| 66 | +import static org.springframework.restdocs.mockmvc.IterableEnumeration.iterable; |
63 | 67 | import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
64 | 68 | import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
|
65 | 69 | import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
|
@@ -317,8 +321,9 @@ public void responseFieldsSnippet() throws Exception {
|
317 | 321 |
|
318 | 322 | mockMvc.perform(get("/").param("foo", "bar").accept(MediaType.APPLICATION_JSON))
|
319 | 323 | .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"), |
322 | 327 | fieldWithPath("links").description("Links to other resources"))));
|
323 | 328 |
|
324 | 329 | assertExpectedSnippetFilesExist(new File("build/generated-snippets/links"),
|
@@ -388,38 +393,44 @@ public void preprocessedRequest() throws Exception {
|
388 | 393 |
|
389 | 394 | Pattern pattern = Pattern.compile("(\"alpha\")");
|
390 | 395 |
|
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\"}")) |
394 | 400 | .andExpect(status().isOk()).andDo(document("original-request"))
|
395 | 401 | .andDo(document("preprocessed-request",
|
396 | 402 | preprocessRequest(prettyPrint(),
|
397 | 403 | removeHeaders("a", HttpHeaders.HOST,
|
398 | 404 | HttpHeaders.CONTENT_LENGTH),
|
399 |
| - replacePattern(pattern, "\"<<beta>>\"")))); |
| 405 | + replacePattern(pattern, "\"<<beta>>\"")))) |
| 406 | + .andReturn(); |
400 | 407 |
|
| 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 | + } |
401 | 413 | assertThat(
|
402 | 414 | 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 | + } |
413 | 428 | String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
|
414 | 429 | assertThat(
|
415 | 430 | new File(
|
416 | 431 | "build/generated-snippets/preprocessed-request/http-request.adoc"),
|
417 | 432 | 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)))); |
423 | 434 | }
|
424 | 435 |
|
425 | 436 | @Test
|
|
0 commit comments