-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathHttp2Test.java
More file actions
238 lines (220 loc) · 7.73 KB
/
Copy pathHttp2Test.java
File metadata and controls
238 lines (220 loc) · 7.73 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.test;
import static io.jooby.test.TestUtil._19kb;
import static okhttp3.RequestBody.create;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Phaser;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http.MetaData;
import org.eclipse.jetty.http2.api.Session;
import org.eclipse.jetty.http2.api.Stream;
import org.eclipse.jetty.http2.api.server.ServerSessionListener;
import org.eclipse.jetty.http2.client.HTTP2Client;
import org.eclipse.jetty.http2.frames.HeadersFrame;
import org.eclipse.jetty.util.FuturePromise;
import org.eclipse.jetty.util.Promise;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import com.google.common.collect.ImmutableMap;
import io.jooby.*;
import io.jooby.jackson.Jackson2Module;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;
import okhttp3.*;
import okhttp3.MediaType;
public class Http2Test {
@ServerTest
public void h2body(ServerTestRunner runner) {
runner
.options(new ServerOptions().setHttp2(true))
.define(
app -> {
app.install(new Jackson2Module());
app.post(
"/h2/multipart",
ctx -> {
try (var f = ctx.file("f")) {
return ctx.getScheme()
+ ":"
+ ctx.getProtocol()
+ ":"
+ new String(f.bytes(), StandardCharsets.UTF_8);
}
});
app.post(
"/h2/body",
ctx -> {
return ctx.getScheme() + ":" + ctx.getProtocol() + ":" + ctx.body(Map.class);
});
})
.ready(
(http, https) -> {
https.post(
"/h2/multipart",
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"f", "19kb.txt", create(_19kb, MediaType.parse("text/plain")))
.build(),
rsp -> {
assertEquals("https:HTTP/2.0:" + _19kb, rsp.body().string());
});
https.post(
"/h2/body",
create("{\"foo\": \"bar\"}", MediaType.parse("application/json")),
rsp -> {
assertEquals("https:HTTP/2.0:" + "{foo=bar}", rsp.body().string());
});
});
}
@ServerTest
public void http2(ServerTestRunner runner) {
runner
.options(new ServerOptions().setHttp2(true).setSecurePort(8443))
.define(
app -> {
app.get(
"/",
ctx ->
ImmutableMap.of(
"secure",
ctx.isSecure(),
"protocol",
ctx.getProtocol(),
"scheme",
ctx.getScheme()));
})
.ready(
(http, https) -> {
https.get(
"/",
rsp -> {
assertEquals(
"{secure=true, protocol=HTTP/2.0, scheme=https}", rsp.body().string());
});
http.get(
"/",
rsp -> {
assertEquals(
"{secure=false, protocol=HTTP/1.1, scheme=http}", rsp.body().string());
});
});
}
@ServerTest
public void http2c(ServerTestRunner runner) {
runner
.options(new ServerOptions().setHttp2(true))
.define(
app -> {
app.get(
"/",
ctx ->
ImmutableMap.of(
"secure",
ctx.isSecure(),
"protocol",
ctx.getProtocol(),
"scheme",
ctx.getScheme()));
})
.ready(
http -> {
http.get(
"/",
rsp -> {
assertEquals(
"{secure=false, protocol=HTTP/1.1, scheme=http}", rsp.body().string());
});
hc2(
http,
"/",
rsp -> {
assertEquals(
"{secure=false, protocol=HTTP/2.0, scheme=http}", rsp.body().string());
});
});
}
private void hc2(WebClient http, String path, SneakyThrows.Consumer<Response> consumer)
throws ExecutionException, InterruptedException {
HttpFields requestFields = HttpFields.build();
HttpURI uri = HttpURI.from("http://localhost:" + http.getPort() + path);
MetaData.Request metaData = new MetaData.Request("GET", uri, HttpVersion.HTTP_2, requestFields);
HeadersFrame frame = new HeadersFrame(metaData, null, true);
final Phaser phaser = new Phaser(2);
FuturePromise<org.eclipse.jetty.http2.api.Session> sessionPromise = new FuturePromise<>();
h2c.connect(
null,
new InetSocketAddress("localhost", http.getPort()),
new ServerSessionListener() {
@Override
public void onAccept(Session session) {}
},
sessionPromise);
Session session = sessionPromise.get();
Response.Builder builder = new Response.Builder();
session.newStream(
frame,
Promise.noop(),
new Stream.Listener() {
@Override
public void onHeaders(final Stream stream, final HeadersFrame frame) {
MetaData.Response md = (MetaData.Response) frame.getMetaData();
StatusCode statusCode = StatusCode.valueOf(md.getStatus());
builder.code(statusCode.value()).message(statusCode.reason());
md.forEach(
f -> builder.addHeader(f.getName().toLowerCase(), f.getValue().toLowerCase()));
if (frame.isEndStream()) {
phaser.arrive();
} else {
stream.demand();
}
}
@Override
public void onDataAvailable(Stream stream) {
while (true) {
Stream.Data data = stream.readData();
if (data == null) {
stream.demand();
return;
}
var buf = data.frame().getByteBuffer();
byte[] bytes = new byte[buf.remaining()];
buf.get(bytes);
ResponseBody responseBody = ResponseBody.create(bytes, MediaType.parse("text/plain"));
builder.body(responseBody);
if (frame.isEndStream()) {
phaser.arrive();
}
data.release();
if (data.frame().isEndStream()) return;
}
}
});
phaser.awaitAdvanceInterruptibly(phaser.arrive());
consumer.accept(
builder
.request(new Request.Builder().url(uri.toString()).method("GET", null).build())
.protocol(Protocol.HTTP_2)
.build());
}
private static HTTP2Client h2c;
@BeforeAll
public static void init() throws Exception {
h2c = new HTTP2Client();
h2c.start();
}
@AfterAll
public static void close() throws Exception {
h2c.stop();
}
}