Provide access to raw content in RestTestClient

Closes gh-35399
This commit is contained in:
rstoyanchev
2025-09-17 15:50:59 +01:00
parent 23d1b0e881
commit 11dd0d6118
4 changed files with 119 additions and 10 deletions
@@ -36,6 +36,8 @@ import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -317,6 +319,19 @@ class RestTestClientTests {
});
assertThat(result.getResponseBody().get("uri")).isEqualTo("/test");
}
@Test
void testResultContent() {
String body = "body-in";
EntityExchangeResult<String> result = RestTestClientTests.this.client.post().uri("/body")
.body(body)
.exchange()
.expectStatus().isOk()
.expectBody(String.class)
.returnResult();
assertThat(result.getRequestBodyContent()).isEqualTo(body.getBytes(StandardCharsets.UTF_8));
assertThat(result.getResponseBodyContent()).isEqualTo((body + "-out").getBytes(StandardCharsets.UTF_8));
}
}
@@ -325,14 +340,20 @@ class RestTestClientTests {
@RequestMapping(path = {"/test", "/test/*"}, produces = "application/json")
public Map<String, Object> handle(
@RequestHeader HttpHeaders headers,
HttpServletRequest request, HttpServletResponse response) {
@RequestHeader HttpHeaders headers, HttpServletRequest request, HttpServletResponse response) {
response.addCookie(new Cookie("session", "abc"));
return Map.of(
"method", request.getMethod(),
"uri", request.getRequestURI(),
"headers", headers.toSingleValueMap()
);
}
@PostMapping("/body")
public String echoBody(@RequestBody String body) {
return body + "-out";
}
}
}