Skip to content

Commit 82e0278

Browse files
authored
Add tests to validate we correctly handling trailers of inner messages (#120)
Motivation: We didn't have tests to validate that we correctly serialize / deserialize trailers of the inner messages. Modifications: Add unit test for handling of trailers Result: More tests
1 parent 2ad5dec commit 82e0278

1 file changed

Lines changed: 84 additions & 42 deletions

File tree

codec-ohttp/src/test/java/io/netty/incubator/codec/ohttp/OHttpCodecsTest.java

Lines changed: 84 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
*/
1616
package io.netty.incubator.codec.ohttp;
1717

18+
import io.netty.handler.codec.http.DefaultFullHttpRequest;
19+
import io.netty.handler.codec.http.DefaultHttpHeaders;
20+
import io.netty.handler.codec.http.HttpHeaders;
21+
import io.netty.handler.codec.http.LastHttpContent;
1822
import io.netty.incubator.codec.bhttp.BinaryHttpRequest;
1923
import io.netty.incubator.codec.bhttp.DefaultBinaryHttpRequest;
2024
import io.netty.incubator.codec.bhttp.DefaultBinaryHttpResponse;
2125
import io.netty.incubator.codec.bhttp.DefaultFullBinaryHttpRequest;
2226
import io.netty.incubator.codec.bhttp.DefaultFullBinaryHttpResponse;
2327
import io.netty.incubator.codec.bhttp.FullBinaryHttpRequest;
28+
import io.netty.incubator.codec.bhttp.FullBinaryHttpResponse;
2429
import io.netty.incubator.codec.hpke.AEAD;
2530
import io.netty.incubator.codec.hpke.AsymmetricCipherKeyPair;
2631
import io.netty.incubator.codec.hpke.AsymmetricKeyParameter;
@@ -75,26 +80,29 @@ private static final class OHttpVersionArgumentsProvider implements ArgumentsPro
7580
@Override
7681
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
7782
List<Arguments> arguments = new ArrayList<>();
78-
arguments.add(Arguments.of(OHttpVersionDraft.INSTANCE, BouncyCastleOHttpCryptoProvider.INSTANCE,
79-
BouncyCastleOHttpCryptoProvider.INSTANCE));
80-
arguments.add(Arguments.of(OHttpVersionChunkDraft.INSTANCE, BouncyCastleOHttpCryptoProvider.INSTANCE,
81-
BouncyCastleOHttpCryptoProvider.INSTANCE));
82-
83-
if (BoringSSLHPKE.isAvailable()) {
84-
arguments.add(Arguments.of(OHttpVersionDraft.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE,
85-
BoringSSLOHttpCryptoProvider.INSTANCE));
86-
arguments.add(Arguments.of(OHttpVersionChunkDraft.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE,
87-
BoringSSLOHttpCryptoProvider.INSTANCE));
88-
89-
arguments.add(Arguments.of(OHttpVersionDraft.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE,
90-
BouncyCastleOHttpCryptoProvider.INSTANCE));
91-
arguments.add(Arguments.of(OHttpVersionChunkDraft.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE,
92-
BouncyCastleOHttpCryptoProvider.INSTANCE));
93-
83+
for (int i = 0; i < 2; i++) {
84+
boolean trailers = i == 0;
9485
arguments.add(Arguments.of(OHttpVersionDraft.INSTANCE, BouncyCastleOHttpCryptoProvider.INSTANCE,
95-
BoringSSLOHttpCryptoProvider.INSTANCE));
86+
BouncyCastleOHttpCryptoProvider.INSTANCE, trailers));
9687
arguments.add(Arguments.of(OHttpVersionChunkDraft.INSTANCE, BouncyCastleOHttpCryptoProvider.INSTANCE,
97-
BoringSSLOHttpCryptoProvider.INSTANCE));
88+
BouncyCastleOHttpCryptoProvider.INSTANCE, trailers));
89+
90+
if (BoringSSLHPKE.isAvailable()) {
91+
arguments.add(Arguments.of(OHttpVersionDraft.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE,
92+
BoringSSLOHttpCryptoProvider.INSTANCE, trailers));
93+
arguments.add(Arguments.of(OHttpVersionChunkDraft.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE,
94+
BoringSSLOHttpCryptoProvider.INSTANCE, trailers));
95+
96+
arguments.add(Arguments.of(OHttpVersionDraft.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE,
97+
BouncyCastleOHttpCryptoProvider.INSTANCE, trailers));
98+
arguments.add(Arguments.of(OHttpVersionChunkDraft.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE,
99+
BouncyCastleOHttpCryptoProvider.INSTANCE, trailers));
100+
101+
arguments.add(Arguments.of(OHttpVersionDraft.INSTANCE, BouncyCastleOHttpCryptoProvider.INSTANCE,
102+
BoringSSLOHttpCryptoProvider.INSTANCE, trailers));
103+
arguments.add(Arguments.of(OHttpVersionChunkDraft.INSTANCE,
104+
BouncyCastleOHttpCryptoProvider.INSTANCE, BoringSSLOHttpCryptoProvider.INSTANCE, trailers));
105+
}
98106
}
99107
return arguments.stream();
100108
}
@@ -195,6 +203,11 @@ public static void testTransferFlow(EmbeddedChannel sender,
195203
assertEquals(expected, received);
196204
if (expected instanceof HttpContent) {
197205
assertEquals(((HttpContent) expected).content(), ((HttpContent) received).content());
206+
207+
if (expected instanceof LastHttpContent) {
208+
assertEquals(((LastHttpContent) expected).trailingHeaders(),
209+
((LastHttpContent) received).trailingHeaders());
210+
}
198211
}
199212
ReferenceCountUtil.release(expected);
200213
ReferenceCountUtil.release(received);
@@ -207,40 +220,56 @@ public static ByteBuf strToBuf(String str) {
207220
return Unpooled.directBuffer().writeBytes(str.getBytes(StandardCharsets.US_ASCII));
208221
}
209222

223+
private static HttpHeaders newTrailers(boolean useTrailers) {
224+
HttpHeaders trailers = new DefaultHttpHeaders();
225+
if (useTrailers) {
226+
trailers.add("x-trailer", "value");
227+
}
228+
return trailers;
229+
}
230+
210231
@ParameterizedTest
211232
@ArgumentsSource(value = OHttpVersionArgumentsProvider.class)
212-
void testContent(OHttpVersion version, OHttpCryptoProvider clientProvider, OHttpCryptoProvider serverProvider)
233+
void testContent(OHttpVersion version, OHttpCryptoProvider clientProvider,
234+
OHttpCryptoProvider serverProvider, boolean useTrailers)
213235
throws Exception {
214236
ChannelPair channels = createChannelPair(version, clientProvider, serverProvider);
215237
EmbeddedChannel client = channels.client();
216238
EmbeddedChannel server = channels.server();
217239

240+
HttpHeaders trailers = newTrailers(useTrailers);
241+
FullBinaryHttpRequest request = new DefaultFullBinaryHttpRequest(
242+
HttpVersion.HTTP_1_1,
243+
HttpMethod.POST,
244+
"https",
245+
"foo.bar",
246+
"/test",
247+
strToBuf("THIS IS MY BODY"));
248+
request.trailingHeaders().set(trailers);
249+
218250
testTransferFlow(client, server, false,
219-
Collections.singletonList(new DefaultFullBinaryHttpRequest(
220-
HttpVersion.HTTP_1_1,
221-
HttpMethod.POST,
222-
"https",
223-
"foo.bar",
224-
"/test",
225-
strToBuf("THIS IS MY BODY"))),
251+
Collections.singletonList(request),
226252
Arrays.asList(new DefaultHttpRequest(
227253
HttpVersion.HTTP_1_1,
228254
HttpMethod.POST,
229255
"/test"),
230256
new DefaultHttpContent(strToBuf("THIS IS MY BODY")),
231-
new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER))
257+
new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, trailers))
232258
);
233259

260+
FullBinaryHttpResponse response = new DefaultFullBinaryHttpResponse(
261+
HttpVersion.HTTP_1_1,
262+
HttpResponseStatus.OK,
263+
strToBuf("RESPONSE"));
264+
response.trailingHeaders().set(trailers);
265+
234266
testTransferFlow(server, client, false,
235-
Collections.singletonList(new DefaultFullBinaryHttpResponse(
236-
HttpVersion.HTTP_1_1,
237-
HttpResponseStatus.OK,
238-
strToBuf("RESPONSE"))),
267+
Collections.singletonList(response),
239268
Arrays.asList(new DefaultHttpResponse(
240269
HttpVersion.HTTP_1_1,
241270
HttpResponseStatus.OK),
242271
new DefaultHttpContent(strToBuf("RESPONSE")),
243-
new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER))
272+
new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, trailers))
244273
);
245274

246275
client.finishAndReleaseAll();
@@ -250,13 +279,15 @@ void testContent(OHttpVersion version, OHttpCryptoProvider clientProvider, OHttp
250279
@ParameterizedTest
251280
@ArgumentsSource(value = OHttpVersionArgumentsProvider.class)
252281
void testContentChunked(OHttpVersion version, OHttpCryptoProvider clientProvider,
253-
OHttpCryptoProvider serverProvider) throws Exception {
282+
OHttpCryptoProvider serverProvider, boolean useTrailers)
283+
throws Exception {
254284

255285
assumeTrue(version != OHttpVersionDraft.INSTANCE);
256286

257287
ChannelPair channels = createChannelPair(version, clientProvider, serverProvider);
258288
EmbeddedChannel client = channels.client();
259289
EmbeddedChannel server = channels.server();
290+
HttpHeaders trailers = newTrailers(useTrailers);
260291

261292
testTransferFlow(client, server, false,
262293
Arrays.asList(newRequestWithHeaders("test", true),
@@ -291,39 +322,50 @@ void testContentChunked(OHttpVersion version, OHttpCryptoProvider clientProvider
291322
Collections.singletonList(new DefaultHttpContent(strToBuf("555")))
292323
);
293324

294-
testTransferFlow(server, client, true,
325+
testTransferFlow(server, client, false,
295326
Collections.singletonList(new DefaultHttpContent(strToBuf("666"))),
296327
Collections.singletonList(new DefaultHttpContent(strToBuf("666")))
297328
);
298-
329+
testTransferFlow(server, client, true,
330+
Collections.singletonList(new DefaultLastHttpContent(strToBuf("666"), trailers)),
331+
Arrays.asList(new DefaultHttpContent(strToBuf("666")),
332+
new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, trailers))
333+
);
299334
client.finishAndReleaseAll();
300335
server.finishAndReleaseAll();
301336
}
302337

303338
@ParameterizedTest
304339
@ArgumentsSource(value = OHttpVersionArgumentsProvider.class)
305340
void testCodec(OHttpVersion version, OHttpCryptoProvider clientProvider,
306-
OHttpCryptoProvider serverProvider) throws Exception {
341+
OHttpCryptoProvider serverProvider, boolean useTrailers) throws Exception {
307342

308343
ChannelPair channels = createChannelPair(version, clientProvider, serverProvider);
309344
EmbeddedChannel client = channels.client();
310345
EmbeddedChannel server = channels.server();
311346

347+
HttpHeaders trailers = newTrailers(useTrailers);
348+
349+
FullBinaryHttpRequest request = newFullRequestWithHeaders("/test", strToBuf("request body"));
350+
request.trailingHeaders().set(trailers);
312351
testTransferFlow(client, server, false,
313-
Collections.singletonList(newFullRequestWithHeaders("/test", strToBuf("request body"))),
352+
Collections.singletonList(request),
314353
Arrays.asList(newRequestWithHeaders("/test", false),
315354
new DefaultHttpContent(strToBuf("request body")),
316-
new DefaultLastHttpContent()));
355+
new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, trailers)));
356+
357+
FullBinaryHttpResponse response = new DefaultFullBinaryHttpResponse(
358+
HttpVersion.HTTP_1_1,
359+
HttpResponseStatus.OK, strToBuf("response body"));
360+
response.trailingHeaders().set(trailers);
317361

318362
testTransferFlow(server, client, false,
319-
Collections.singletonList(new DefaultFullBinaryHttpResponse(
320-
HttpVersion.HTTP_1_1,
321-
HttpResponseStatus.OK, strToBuf("response body"))),
363+
Collections.singletonList(response),
322364
Arrays.asList(new DefaultBinaryHttpResponse(
323365
HttpVersion.HTTP_1_1,
324366
HttpResponseStatus.OK),
325367
new DefaultHttpContent(strToBuf("response body")),
326-
new DefaultLastHttpContent())
368+
new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, trailers))
327369
);
328370
client.finishAndReleaseAll();
329371
server.finishAndReleaseAll();

0 commit comments

Comments
 (0)