Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebTestClient cannot assert custom HTTP status code #29283

Closed
robert-gdv opened this issue Oct 7, 2022 · 1 comment
Closed

WebTestClient cannot assert custom HTTP status code #29283

robert-gdv opened this issue Oct 7, 2022 · 1 comment
Assignees
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@robert-gdv
Copy link

robert-gdv commented Oct 7, 2022

Affects: spring-test:5.3.23

I wanted to test how the service reacts on unknown HTTP response codes like 222. So I created a service that responds with HTTP status 222 and tried to test it:

@SpringBootTest
class ResponseTest {

  @Autowired
  org.springframework.test.web.reactive.server.WebTestClient client;

  @Test
  void test222() {
    client.method(HttpMethod.GET).uri("/").exchange().expectStatus().isEqualTo(222);
  }
}

The result was an Exception with Stack Trace:

java.lang.IllegalArgumentException: No matching constant for [222]
	at org.springframework.http.HttpStatus.valueOf(HttpStatus.java:547)
	at org.springframework.http.client.reactive.ReactorClientHttpResponse.getStatusCode(ReactorClientHttpResponse.java:139)
	at org.springframework.http.client.reactive.ClientHttpResponseDecorator.getStatusCode(ClientHttpResponseDecorator.java:60)
	at org.springframework.test.web.reactive.server.ExchangeResult.getStatus(ExchangeResult.java:176)
	at org.springframework.test.web.reactive.server.ExchangeResult.toString(ExchangeResult.java:251)
	at java.base/java.lang.String.valueOf(String.java:4218)
	at java.base/java.lang.StringBuilder.append(StringBuilder.java:173)
	at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:236)
	at org.springframework.test.web.reactive.server.DefaultWebTestClient$DefaultBodySpec.isEqualTo(DefaultWebTestClient.java:554)
	at de.gdvdl.tgic.tgicwebauth.routes.WebapiTest.test222(ResponseTest:xyz)

The issue seems to be at org.springframework.test.web.reactive.server.ExchangeResult.toString(), which assumes that getStatus() is always available:

"< " + getStatus() + " " + getStatus().getReasonPhrase() + "\n" +

So the line could be something like this:

"< " + getRawStatusCode() + " " + tryToGet(() -> getStatus().getReasonPhrase()) + "\n" +

with

String tryToGet(Supplier<String> func) {
  try {
    return func.get()
  } catch (RuntimeException e) {
    return "";
  }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 7, 2022
@sbrannen sbrannen added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 8, 2022
@sbrannen sbrannen self-assigned this Oct 8, 2022
@sbrannen sbrannen added this to the 5.3.24 milestone Oct 8, 2022
@sbrannen sbrannen changed the title WebTestClient can't expect unknown response codes WebTestClient cannot expect custom HTTP status code Oct 8, 2022
@sbrannen
Copy link
Member

sbrannen commented Oct 8, 2022

Hi @robert-gdv,

Congratulations on creating your first bug report for the Spring Framework! 👍

And thanks for bringing this to our attention.

This has been fixed in 5.3.x for inclusion in 5.3.24.

@sbrannen sbrannen changed the title WebTestClient cannot expect custom HTTP status code WebTestClient cannot assert custom HTTP status code Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants