This repository was archived by the owner on Mar 3, 2026. It is now read-only.
forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetachTest.java
More file actions
71 lines (61 loc) · 1.89 KB
/
Copy pathDetachTest.java
File metadata and controls
71 lines (61 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package apps;
import io.jooby.App;
import io.jooby.AppTest;
import io.jooby.Context;
import io.jooby.Route;
import io.jooby.WebClient;
import io.reactivex.Flowable;
import io.reactivex.schedulers.Schedulers;
import org.jooby.funzy.Throwing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import javax.annotation.Nonnull;
import java.io.IOException;
@AppTest
public class DetachTest extends App {
{
get("/rx", detach(ctx -> {
Flowable.fromCallable(() -> "Hello rx!")
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.single())
.subscribe(ctx::send);
}));
get("/rx/flow", rx(ctx ->
Flowable.fromCallable(() -> "Hello rx/flow!")
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.single())
));
}
@Test
public void rxJava(WebClient client) throws IOException {
client.get("/rx", rsp -> {
assertEquals("9", rsp.header("content-length"));
assertEquals("Hello rx!", rsp.body().string());
});
}
@Test
public void rxJavaFlow(WebClient client) throws IOException {
client.get("/rx/flow", rsp -> {
assertEquals("14", rsp.header("content-length"));
assertEquals("Hello rx/flow!", rsp.body().string());
});
}
public static <T> Throwing.Consumer2<Context, Flowable<T>> rx2() {
return (ctx, flow) -> {
flow.subscribe(it -> ctx.send(it.toString()));
};
}
public static <T> Route.Handler rx(Throwing.Function<Context, Flowable<T>> handler) {
return new Route.Handler() {
@Override public void handle(@Nonnull Context ctx, @Nonnull Route.Pipeline chain)
throws Throwable {
ctx.detach();
handler.apply(ctx)
.subscribe(success -> ctx.send(success.toString()));
}
@Override public Object handle(@Nonnull Context ctx) throws Throwable {
return ctx;
}
};
}
}