-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
MockClientHttpResponse should not return null body [SPR-16367] #20914
Comments
Rossen Stoyanchev commented I don't follow. MockRestResponseCreators creates a DefaultResponseCreator which initializes the body to byte[0]. Then it passes that into MockHttpInputMessage which has an Assert.notNull check on the body passed in. |
Arthur Gavlyukovskiy commented withStatus(HttpStatus.TOO_MANY_REQUESTS) uses new DefaultResponseCreator(HttpStatus) and DefaultResponseCreator#content stays null. Then in DefaultResponseCreator#createResponse it goes to the branch where this.contentResource is null: response = new MockClientHttpResponse(this.content, this.statusCode); here this.content is null and then constructor of MockHttpInputMessage public MockHttpInputMessage(byte[] contents) {
this.body = (contents != null ? new ByteArrayInputStream(contents) : null);
} sets body to null if contents was null. As workaround I use withStatus(HttpStatus.TOO_MANY_REQUESTS).body("") But to match documentation it should be empty by default or, which is better, make changes in constructor to something like public MockHttpInputMessage(byte[] contents) {
this.body = new ByteArrayInputStream(contents != null ? contents : new byte[0]);
} |
Arthur Gavlyukovskiy commented I see it was fixed in f813712 for Spring 5.x. But changes are not in 4.3.x branch: |
Rossen Stoyanchev commented Okay that makes sense now. We can make this adjustment in 4.3.x. |
Arthur Gavlyukovskiy commented Thanks for a quick fix. BTW about copyright, it is 2018 already :) |
Rossen Stoyanchev commented Yes it is :) |
Arthur Gavlyukovskiy opened SPR-16367 and commented
According to HttpInputMessage documentation body can never be null, although this is not true when you create mock response using MockRestResponseCreators without body, e.g. withBadRequest() or withSuccess().
Looks like changes should be done in MockHttpInputMessage to set ByteArrayInputStream with empty byte array if passed content is null. As well null check before closing stream in MockClientHttpResponse may be removed.
Affects: 4.3.12
The text was updated successfully, but these errors were encountered: