-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18182 from ntrp/main
- Loading branch information
Showing
6 changed files
with
369 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
.../vertx-web/deployment/src/test/java/io/quarkus/vertx/web/mutiny/NdjsonMultiRouteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package io.quarkus.vertx.web.mutiny; | ||
|
||
import static io.restassured.RestAssured.when; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
import java.io.IOException; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.vertx.web.ReactiveRoutes; | ||
import io.quarkus.vertx.web.Route; | ||
import io.smallrye.mutiny.Multi; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class NdjsonMultiRouteTest { | ||
|
||
public static final String CONTENT_TYPE_NDJSON = "application/x-ndjson"; | ||
public static final String CONTENT_TYPE_STREAM_JSON = "application/stream+json"; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(SimpleBean.class)); | ||
|
||
@Test | ||
public void testNdjsonMultiRoute() { | ||
when().get("/hello").then().statusCode(200) | ||
.body(is("\"Hello world!\"\n")) | ||
.header(HttpHeaders.CONTENT_TYPE.toString(), CONTENT_TYPE_NDJSON); | ||
|
||
when().get("/hellos").then().statusCode(200) | ||
.body(containsString( | ||
// @formatter:off | ||
"\"hello\"\n" | ||
+ "\"world\"\n" | ||
+ "\"!\"\n")) | ||
// @formatter:on | ||
.header(HttpHeaders.CONTENT_TYPE.toString(), CONTENT_TYPE_NDJSON); | ||
|
||
when().get("/no-hello").then().statusCode(200).body(hasLength(0)) | ||
.header(HttpHeaders.CONTENT_TYPE.toString(), CONTENT_TYPE_NDJSON); | ||
|
||
// We get the item followed by the exception | ||
when().get("/hello-and-fail").then().statusCode(200) | ||
.body(containsString("\"Hello\"")) | ||
.body(containsString("boom")); | ||
|
||
when().get("/void").then().statusCode(204).body(hasLength(0)); | ||
|
||
when().get("/people").then().statusCode(200) | ||
.body(is( | ||
// @formatter:off | ||
"{\"name\":\"superman\",\"id\":1}\n" + | ||
"{\"name\":\"batman\",\"id\":2}\n" + | ||
"{\"name\":\"spiderman\",\"id\":3}\n" | ||
)) | ||
// @formatter:on | ||
.header(HttpHeaders.CONTENT_TYPE.toString(), CONTENT_TYPE_NDJSON); | ||
|
||
when().get("/people-content-type").then().statusCode(200) | ||
.body(is( | ||
// @formatter:off | ||
"{\"name\":\"superman\",\"id\":1}\n" + | ||
"{\"name\":\"batman\",\"id\":2}\n" + | ||
"{\"name\":\"spiderman\",\"id\":3}\n")) | ||
// @formatter:on | ||
.header(HttpHeaders.CONTENT_TYPE.toString(), is(CONTENT_TYPE_NDJSON + ";charset=utf-8")); | ||
|
||
when().get("/people-content-type-stream-json").then().statusCode(200) | ||
.body(is( | ||
// @formatter:off | ||
"{\"name\":\"superman\",\"id\":1}\n" + | ||
"{\"name\":\"batman\",\"id\":2}\n" + | ||
"{\"name\":\"spiderman\",\"id\":3}\n")) | ||
// @formatter:on | ||
.header(HttpHeaders.CONTENT_TYPE.toString(), CONTENT_TYPE_STREAM_JSON); | ||
|
||
when().get("/failure").then().statusCode(500).body(containsString("boom")); | ||
when().get("/null").then().statusCode(500).body(containsString("null")); | ||
when().get("/sync-failure").then().statusCode(500).body(containsString("null")); | ||
} | ||
|
||
static class SimpleBean { | ||
|
||
@Route(path = "hello") | ||
Multi<String> hello(RoutingContext context) { | ||
return ReactiveRoutes.asJsonStream(Multi.createFrom().item("Hello world!")); | ||
} | ||
|
||
@Route(path = "hellos") | ||
Multi<String> hellos(RoutingContext context) { | ||
return ReactiveRoutes.asJsonStream(Multi.createFrom().items("hello", "world", "!")); | ||
} | ||
|
||
@Route(path = "no-hello") | ||
Multi<String> noHello(RoutingContext context) { | ||
return ReactiveRoutes.asJsonStream(Multi.createFrom().empty()); | ||
} | ||
|
||
@Route(path = "hello-and-fail") | ||
Multi<String> helloAndFail(RoutingContext context) { | ||
return ReactiveRoutes.asJsonStream(Multi.createBy().concatenating().streams( | ||
Multi.createFrom().item("Hello"), | ||
Multi.createFrom().failure(() -> new IOException("boom")))); | ||
} | ||
|
||
@Route(path = "void") | ||
Multi<Void> multiVoid(RoutingContext context) { | ||
return ReactiveRoutes.asJsonStream(Multi.createFrom().range(0, 200) | ||
.onItem().ignore()); | ||
} | ||
|
||
@Route(path = "/people") | ||
Multi<Person> people(RoutingContext context) { | ||
return ReactiveRoutes.asJsonStream(Multi.createFrom().items( | ||
new Person("superman", 1), | ||
new Person("batman", 2), | ||
new Person("spiderman", 3))); | ||
} | ||
|
||
@Route(path = "/people-content-type") | ||
Multi<Person> peopleWithContentType(RoutingContext context) { | ||
context.response().putHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE_NDJSON + ";charset=utf-8"); | ||
return ReactiveRoutes.asJsonStream(Multi.createFrom().items( | ||
new Person("superman", 1), | ||
new Person("batman", 2), | ||
new Person("spiderman", 3))); | ||
} | ||
|
||
@Route(path = "/people-content-type-stream-json", produces = { CONTENT_TYPE_STREAM_JSON }) | ||
Multi<Person> peopleWithContentTypeStreamJson(RoutingContext context) { | ||
return ReactiveRoutes.asJsonStream(Multi.createFrom().items( | ||
new Person("superman", 1), | ||
new Person("batman", 2), | ||
new Person("spiderman", 3))); | ||
} | ||
|
||
@Route(path = "/failure") | ||
Multi<Person> fail(RoutingContext context) { | ||
return ReactiveRoutes.asJsonStream(Multi.createFrom().failure(new IOException("boom"))); | ||
} | ||
|
||
@Route(path = "/sync-failure") | ||
Multi<Person> failSync(RoutingContext context) { | ||
throw new IllegalStateException("boom"); | ||
} | ||
|
||
@Route(path = "/null") | ||
Multi<String> uniNull(RoutingContext context) { | ||
return null; | ||
} | ||
} | ||
|
||
static class Person { | ||
public String name; | ||
public int id; | ||
|
||
public Person(String name, int id) { | ||
this.name = name; | ||
this.id = id; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...ions/vertx-web/runtime/src/main/java/io/quarkus/vertx/web/runtime/MultiNdjsonSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.quarkus.vertx.web.runtime; | ||
|
||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.MultiMap; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.core.json.Json; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
@SuppressWarnings("ReactiveStreamsSubscriberImplementation") | ||
public class MultiNdjsonSupport { | ||
|
||
private MultiNdjsonSupport() { | ||
// Avoid direct instantiation. | ||
} | ||
|
||
private static void initialize(HttpServerResponse response, RoutingContext rc) { | ||
if (response.bytesWritten() == 0) { | ||
MultiMap headers = response.headers(); | ||
if (headers.get(HttpHeaders.CONTENT_TYPE) == null) { | ||
if (rc.getAcceptableContentType() == null) { | ||
headers.set(HttpHeaders.CONTENT_TYPE, "application/x-ndjson"); | ||
} else { | ||
headers.set(HttpHeaders.CONTENT_TYPE, rc.getAcceptableContentType()); | ||
} | ||
} | ||
response.setChunked(true); | ||
} | ||
} | ||
|
||
public static void subscribeString(Multi<String> multi, RoutingContext rc) { | ||
write(multi.map(s -> Buffer.buffer("\"" + s + "\"\n")), rc); | ||
} | ||
|
||
public static void subscribeObject(Multi<Object> multi, RoutingContext rc) { | ||
write(multi.map(o -> Buffer.buffer(Json.encode(o) + "\n")), rc); | ||
} | ||
|
||
private static void onWriteDone(Subscription subscription, AsyncResult<Void> ar, RoutingContext rc) { | ||
if (ar.failed()) { | ||
rc.fail(ar.cause()); | ||
} else { | ||
subscription.request(1); | ||
} | ||
} | ||
|
||
public static void write(Multi<Buffer> multi, RoutingContext rc) { | ||
HttpServerResponse response = rc.response(); | ||
multi.subscribe().withSubscriber(new Subscriber<Buffer>() { | ||
Subscription upstream; | ||
|
||
@Override | ||
public void onSubscribe(Subscription subscription) { | ||
this.upstream = subscription; | ||
this.upstream.request(1); | ||
} | ||
|
||
@Override | ||
public void onNext(Buffer item) { | ||
initialize(response, rc); | ||
response.write(item, ar -> onWriteDone(upstream, ar, rc)); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable throwable) { | ||
rc.fail(throwable); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
endOfStream(response, rc); | ||
} | ||
}); | ||
} | ||
|
||
private static void endOfStream(HttpServerResponse response, RoutingContext rc) { | ||
if (response.bytesWritten() == 0) { // No item | ||
MultiMap headers = response.headers(); | ||
if (headers.get(HttpHeaders.CONTENT_TYPE) == null) { | ||
if (rc.getAcceptableContentType() == null) { | ||
headers.set(HttpHeaders.CONTENT_TYPE, "application/x-ndjson"); | ||
} else { | ||
headers.set(HttpHeaders.CONTENT_TYPE, rc.getAcceptableContentType()); | ||
} | ||
} | ||
} | ||
response.end(); | ||
} | ||
|
||
public static boolean isNdjson(Multi<?> multi) { | ||
return multi instanceof NdjsonMulti; | ||
} | ||
} |
Oops, something went wrong.