Support custom HTTP status in client-side REST testing support #28105
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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:
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: