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 pathFeatureTest.java
More file actions
108 lines (91 loc) · 2.83 KB
/
Copy pathFeatureTest.java
File metadata and controls
108 lines (91 loc) · 2.83 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 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 FeatureTest extends App {
{
before(GET, "/filters", ctx -> {
ctx.header("one", 1);
});
before(GET, "/filters", ctx -> {
ctx.header("two", 2);
});
after(GET, "/filters", ctx -> {
ctx.header("three", 3);
});
after(GET, "/filters", ctx -> {
ctx.header("four", 4);
});
get("/", ctx -> "Hello world!");
get("/chunk", ctx -> {
return ctx.write("Hello ")
.write("chunks")
.write("!")
.end();
});
get("/writer", ctx -> ctx.writer(writer -> {
writer.write("Hello ");
writer.write("writer");
writer.write("!");
}));
get("/filters", ctx -> "Hello filters!");
}
@Test
public void chunks(WebClient client) throws IOException {
client.get("/chunk", rsp -> {
assertEquals("chunked", rsp.header("transfer-encoding"));
assertEquals(null, rsp.header("content-length"));
assertEquals("Hello chunks!", rsp.body().string());
});
}
@Test
public void writer(WebClient client) throws IOException {
client.get("/writer", rsp -> {
assertEquals("chunked", rsp.header("transfer-encoding"));
assertEquals(null, rsp.header("content-length"));
assertEquals("Hello writer!", rsp.body().string());
});
}
@Test
public void filters(WebClient client) throws IOException {
client.get("/filters", rsp -> {
assertEquals(null, rsp.header("transfer-encoding"));
assertEquals("14", rsp.header("content-length"));
assertEquals("1", rsp.header("one"));
assertEquals("2", rsp.header("two"));
assertEquals("3", rsp.header("three"));
assertEquals("4", rsp.header("four"));
assertEquals("Hello filters!", rsp.body().string());
});
}
@Test
public void helloWorld(WebClient client) throws IOException {
client.get("/", rsp -> {
assertEquals(null, rsp.header("transfer-encoding"));
assertEquals("12", rsp.header("content-length"));
assertEquals("Hello world!", rsp.body().string());
});
}
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;
}
};
}
}