Skip to content

Commit

Permalink
Documentation for HTTP response expectations
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont committed Nov 4, 2024
1 parent 69618c3 commit f8e6dca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,29 @@ The obtained `Single` can be composed and chained naturally with the RxJava API
{@link examples.RxWebClientExamples#flatMap(io.vertx.rxjava3.ext.web.client.WebClient)}
----

The same APIs is available
The same APIs are available:

[source,java]
----
{@link examples.RxWebClientExamples#moreComplex(io.vertx.rxjava3.ext.web.client.WebClient)}
----

The {@link io.vertx.rxjava3.ext.web.client.HttpRequest#rxSendStream(io.reactivex.rxjava3.core.Flowable)} shall
be preferred for sending bodies `Flowable<Buffer>`.
The {@link io.vertx.rxjava3.ext.web.client.HttpRequest#rxSendStream(io.reactivex.rxjava3.core.Flowable)} shall be preferred for sending bodies `Flowable<Buffer>`.

[source,java]
----
{@link examples.RxWebClientExamples#sendFlowable(io.vertx.rxjava3.ext.web.client.WebClient)}
----

Upon subscription, the `body` will be subscribed and its content used for the request.

=== HTTP Response Expectations

Interacting with an HTTP backend often involves verifying HTTP response codes and/or content types.

To streamline the process of verification, use the {@link io.vertx.rxjava3.core.http.HttpResponseExpectation} methods:

[source,java]
----
{@link examples.RxWebClientExamples#httpResponseExpectations(io.vertx.rxjava3.ext.web.client.WebClient)}
----
13 changes: 12 additions & 1 deletion rx-java3/src/main/java/examples/RxWebClientExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Single;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.docgen.Source;
import io.vertx.core.buffer.Buffer;
import io.vertx.rxjava3.core.http.HttpResponseExpectation;
import io.vertx.rxjava3.ext.web.client.HttpResponse;
import io.vertx.rxjava3.ext.web.client.WebClient;
import io.vertx.rxjava3.ext.web.codec.BodyCodec;
Expand Down Expand Up @@ -73,4 +74,14 @@ public void sendFlowable(WebClient client) {
System.out.println(resp.body());
});
}

public void httpResponseExpectations(WebClient client) {
Single<HttpResponse<Buffer>> single = client
.get(8080, "myserver.mycompany.com", "/some-uri")
.rxSend()
// Transforms the single into a failed single if the HTTP response is not successful
.compose(HttpResponseExpectation.status(200))
// Transforms the single into a failed single if the HTTP response content is not JSON
.compose(HttpResponseExpectation.contentType("application/json"));
}
}

0 comments on commit f8e6dca

Please sign in to comment.