Improve handling of the Content-Length header
Previously, some MockMvc-specific logic would add a Content-Length header to every request that had content. This led to the curl request snippet containing a -H option for the Content-Length header. This is unnecessary as curl will automatically generate a Content-Length header based on the data that's being sent to the server. A secondary problem was the inconsistent automatic addition of a Content-Length header; the header was not automatically added to responses. This commit remove the MockMvc-specific logic in favour of some new logic in the core project to automatically add a Content-Length header to both requests and responses. The curl request snippet has been updated to supress the header in favour of curl's automatic generation. Closes gh-111
This commit is contained in:
@@ -157,7 +157,8 @@ public class MockMvcOperationRequestFactoryTests {
|
||||
OperationRequestPart part = request.getParts().iterator().next();
|
||||
assertThat(part.getName(), is(equalTo("file")));
|
||||
assertThat(part.getSubmittedFileName(), is(nullValue()));
|
||||
assertThat(part.getHeaders().isEmpty(), is(true));
|
||||
assertThat(part.getHeaders().size(), is(1));
|
||||
assertThat(part.getHeaders().getContentLength(), is(4L));
|
||||
assertThat(part.getContent(), is(equalTo(new byte[] { 1, 2, 3, 4 })));
|
||||
}
|
||||
|
||||
|
||||
@@ -79,6 +79,7 @@ import static org.springframework.restdocs.test.SnippetMatchers.codeBlock;
|
||||
import static org.springframework.restdocs.test.SnippetMatchers.httpRequest;
|
||||
import static org.springframework.restdocs.test.SnippetMatchers.httpResponse;
|
||||
import static org.springframework.restdocs.test.SnippetMatchers.snippet;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
@@ -120,6 +121,21 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
"http-request.adoc", "http-response.adoc", "curl-request.adoc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void curlSnippetWithContent() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
.apply(documentationConfiguration(this.restDocumentation)).build();
|
||||
|
||||
mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content"))
|
||||
.andExpect(status().isOk()).andDo(document("curl-snippet-with-content"));
|
||||
assertThat(new File(
|
||||
"build/generated-snippets/curl-snippet-with-content/curl-request.adoc"),
|
||||
is(snippet().withContents(
|
||||
codeBlock("bash").content(
|
||||
"$ curl " + "'http://localhost:8080/' -i -X POST "
|
||||
+ "-H 'Accept: application/json' -d 'content'"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void linksSnippet() throws Exception {
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
|
||||
@@ -298,24 +314,27 @@ public class MockMvcRestDocumentationIntegrationTests {
|
||||
removeHeaders("a"),
|
||||
replacePattern(pattern, "\"<<beta>>\""))));
|
||||
|
||||
String original = "{\"a\":\"alpha\",\"links\":[{\"rel\":\"rel\","
|
||||
+ "\"href\":\"href\"}]}";
|
||||
assertThat(
|
||||
new File("build/generated-snippets/original-response/http-response.adoc"),
|
||||
is(snippet().withContents(
|
||||
httpResponse(HttpStatus.OK)
|
||||
.header("a", "alpha")
|
||||
.header("Content-Type", "application/json")
|
||||
.content(
|
||||
"{\"a\":\"alpha\",\"links\":[{\"rel\":\"rel\","
|
||||
+ "\"href\":\"href\"}]}"))));
|
||||
.header(HttpHeaders.CONTENT_LENGTH,
|
||||
original.getBytes().length).content(original))));
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" : "
|
||||
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/preprocessed-response/http-response.adoc"),
|
||||
is(snippet().withContents(
|
||||
httpResponse(HttpStatus.OK).header("Content-Type",
|
||||
"application/json").content(
|
||||
String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" :"
|
||||
+ " [ {%n \"rel\" : \"rel\",%n \"href\" :"
|
||||
+ " \"...\"%n } ]%n}")))));
|
||||
httpResponse(HttpStatus.OK)
|
||||
.header("Content-Type", "application/json")
|
||||
.header(HttpHeaders.CONTENT_LENGTH,
|
||||
prettyPrinted.getBytes().length)
|
||||
.content(prettyPrinted))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -27,7 +27,6 @@ import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -94,17 +93,6 @@ public class RestDocumentationConfigurerTests {
|
||||
assertThat(this.request.getHeader("Content-Length"), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentLengthHeaderIsSetWhenRequestHasContent() {
|
||||
RequestPostProcessor postProcessor = new RestDocumentationMockMvcConfigurer(
|
||||
this.restDocumentation).beforeMockMvcCreated(null, null);
|
||||
byte[] content = "Hello, world".getBytes();
|
||||
this.request.setContent(content);
|
||||
postProcessor.postProcessRequest(this.request);
|
||||
assertThat(this.request.getHeader("Content-Length"),
|
||||
is(equalTo(Integer.toString(content.length))));
|
||||
}
|
||||
|
||||
private void assertUriConfiguration(String scheme, String host, int port) {
|
||||
assertEquals(scheme, this.request.getScheme());
|
||||
assertEquals(host, this.request.getServerName());
|
||||
|
||||
Reference in New Issue
Block a user