Expose sendError message as default body

Closes gh-26720
This commit is contained in:
Juergen Hoeller
2024-01-23 18:31:26 +01:00
parent 358555929d
commit 531ac89e7e
2 changed files with 49 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -32,10 +32,10 @@ import org.springframework.mock.http.client.MockClientHttpResponse;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request;
/** /**
* A {@link ClientHttpRequestFactory} for requests executed via {@link MockMvc}. * A {@link ClientHttpRequestFactory} for requests executed via {@link MockMvc}.
* *
@@ -74,6 +74,14 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
HttpStatusCode status = HttpStatusCode.valueOf(servletResponse.getStatus()); HttpStatusCode status = HttpStatusCode.valueOf(servletResponse.getStatus());
byte[] body = servletResponse.getContentAsByteArray(); byte[] body = servletResponse.getContentAsByteArray();
if (body.length == 0) {
String error = servletResponse.getErrorMessage();
if (StringUtils.hasLength(error)) {
// sendError message as default body
body = error.getBytes(StandardCharsets.UTF_8);
}
}
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status); MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
clientResponse.getHeaders().putAll(getResponseHeaders(servletResponse)); clientResponse.getHeaders().putAll(getResponseHeaders(servletResponse));
return clientResponse; return clientResponse;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.test.web.client.samples; package org.springframework.test.web.client.samples;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@@ -33,13 +34,14 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/** /**
* Tests that use a {@link RestTemplate} configured with a * Tests that use a {@link RestTemplate} configured with a
@@ -48,6 +50,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* the TestContext framework. * the TestContext framework.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Juergen Hoeller
*/ */
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@WebAppConfiguration @WebAppConfiguration
@@ -57,36 +60,62 @@ public class MockMvcClientHttpRequestFactoryTests {
@Autowired @Autowired
private WebApplicationContext wac; private WebApplicationContext wac;
private MockMvc mockMvc; private RestTemplate template;
@BeforeEach @BeforeEach
public void setup() { public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build(); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
this.template = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
} }
@Test @Test
public void test() { public void withResult() {
RestTemplate template = new RestTemplate(new MockMvcClientHttpRequestFactory(this.mockMvc)); assertThat(template.getForObject("/foo", String.class)).isEqualTo("bar");
String result = template.getForObject("/foo", String.class); }
assertThat(result).isEqualTo("bar");
@Test
public void withError() {
assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> template.getForEntity("/error", String.class))
.withMessageContaining("400")
.withMessageContaining("some bad request");
}
@Test
public void withErrorAndBody() {
assertThatExceptionOfType(HttpClientErrorException.class)
.isThrownBy(() -> template.getForEntity("/errorbody", String.class))
.withMessageContaining("400")
.withMessageContaining("some really bad request");
} }
@EnableWebMvc @EnableWebMvc
@Configuration @Configuration
@ComponentScan(basePackageClasses=MockMvcClientHttpRequestFactoryTests.class) @ComponentScan(basePackageClasses = MockMvcClientHttpRequestFactoryTests.class)
static class MyWebConfig implements WebMvcConfigurer { static class MyWebConfig implements WebMvcConfigurer {
} }
@Controller @Controller
static class MyController { static class MyController {
@RequestMapping(value="/foo", method=RequestMethod.GET) @RequestMapping(value = "/foo", method = RequestMethod.GET)
@ResponseBody @ResponseBody
public String handle() { public String handle() {
return "bar"; return "bar";
} }
@RequestMapping(value = "/error", method = RequestMethod.GET)
public void handleError(HttpServletResponse response) throws Exception {
response.sendError(400, "some bad request");
}
@RequestMapping(value = "/errorbody", method = RequestMethod.GET)
public void handleErrorWithBody(HttpServletResponse response) throws Exception {
response.sendError(400, "some bad request");
response.getWriter().write("some really bad request");
}
} }
} }