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

Add Consumer<B> response body asssertions to WebTestClient [SPR-15421] #19982

Closed
spring-projects-issues opened this issue Apr 7, 2017 · 3 comments

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Apr 7, 2017

Logan Mauzaize opened SPR-15421 and commented

ExchangeResult can be improved by adding an assertBodyWithDiagnostics(Consumer<T> bodyAssertions) method which can consume body whatever it is flux or entity-based.

It also requires ExchangeResult to be generic such as sub-classes.

As an alternative, sub-classes can have corresponding method regarding specific requirements for reactive tests.


Affects: 5.0 M5

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 7, 2017

Rossen Stoyanchev commented

First of all thank you for giving this a try.

Regarding ExchangeResult it does not have a decoded body. From its Javadoc:

* <p>Note that a decoded response body is not exposed at this level since the
* body may not have been decoded and consumed yet. Sub-types
* {@link EntityExchangeResult} and {@link FluxExchangeResult} provide access
* to a decoded response entity and a decoded (but not consumed) response body
* respectively.

Immediately after .exchange() you can assert status + headers but the body is only available after one of the expectBody options are used indicating how to decode the body. So we can add assertBody methods under:

client.get().uri("/path").exchange().expectBody(Pojo.class).value().assertBodyWith(pojo -> {});
client.get().uri("/path").exchange().expectBody(Pojo.class).list().assertBodyWith(List<Pojo> -> {});
client.get().uri("/path").exchange().expectBody().map(String.class, Person.class).assertBodyWith(Map<String, Pojo> -> {});

But not:

client.get().uri("/path").exchange().assertBodyWith(??? -> {
    // body not read and not decoded yet...
});

Does that work?

Loading

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 8, 2017

Logan Mauzaize commented

It will be perfect ! After looking at the code, I saw exchange expection API rely on ExchangeResult.assertWithDiagnostics. So if the new method does, it's fine for me.

I have relied on the two only available subclasses of ExchangeResult which offer both access to response body.

Note: Is there any issue to add "use" in Spring API documentation (Javadoc) ?

Loading

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 10, 2017

Rossen Stoyanchev commented

Okay setting for RC1. Feel free to create a ticket for the Javadoc update.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants