mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-21 21:39:05 +00:00
Provide access to raw content in RestTestClient
Closes gh-35399
This commit is contained in:
+23
-2
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user