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

Support custom HTTP status in client-side REST testing support #28105

Closed

Conversation

AzZureman
Copy link
Contributor

@AzZureman AzZureman commented Feb 25, 2022

TL; DR;

Add supporting for custom http statuses in mocked responses
I copied logic from org.springframework.http.ResponseEntity

Foreword

In my company we use "custom not found status" in our REST API's to prevent mixing network error (404 "not found" for unknown routes) with rest responses (when entity with given id not found).
There are good example for rest controller tests.
Suppose we have a rest endpoint for car entity: GET /car/{id}.
Let's try to write test for case when entity with given id not found. Something like that:

mockMvc.perform(get("/car/{id}/", nonExistsId.toString()))
        .andExpect(status().is(404));

And if our code in rest controller have bug, then test will fail with something like "500 internal server error"
But if we also make mistake in test in request's path, like that:

mockMvc.perform(get("/carS/{id}/", nonExistsId.toString()))
        .andExpect(status().is(404));

then test will pass and we don't catch bugs in cases when entity with given id is non-exists.
So, for this reason we used "454" as http status for "not found" case.

Problem

In our clients code we also write tests and want to cover this case with custom http status, but we cant because MockRestResponseCreators and MockClientHttpResponse not support custom http codes:

  mockRestServiceServer.expect(requestTo(serverURI)
          .andExpect(method(HttpMethod.GET))
          .andRespond(withStatus(454)); // <-- will not work cause support only HttpStatus 

@pivotal-cla
Copy link

@AzZureman Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 25, 2022
@pivotal-cla
Copy link

@AzZureman Thank you for signing the Contributor License Agreement!

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 25, 2022
@rstoyanchev rstoyanchev added this to the 5.3.17 milestone Feb 25, 2022
@rstoyanchev rstoyanchev changed the title Support custom http statuses in mocked responses Support custom HTTP status in client-side REST testing support Feb 25, 2022
@AzZureman
Copy link
Contributor Author

Can i do something to move PR forward?

@sbrannen sbrannen added the in: test Issues in the test module label Mar 15, 2022
@rstoyanchev rstoyanchev self-assigned this Mar 16, 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: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants