use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.
the class NettyServerStreamTest method closeWithErrorBeforeClientHalfCloseShouldSucceed.
@Test
public void closeWithErrorBeforeClientHalfCloseShouldSucceed() throws Exception {
ListMultimap<CharSequence, CharSequence> expectedHeaders = ImmutableListMultimap.copyOf(new DefaultHttp2Headers().status(new AsciiString("200")).set(new AsciiString("content-type"), new AsciiString("application/grpc")).set(new AsciiString("grpc-status"), new AsciiString("1")));
// Error is sent on wire and ends the stream
stream().close(Status.CANCELLED, trailers);
ArgumentCaptor<SendResponseHeadersCommand> sendHeadersCap = ArgumentCaptor.forClass(SendResponseHeadersCommand.class);
verify(writeQueue).enqueue(sendHeadersCap.capture(), eq(true));
SendResponseHeadersCommand sendHeaders = sendHeadersCap.getValue();
assertThat(sendHeaders.stream()).isSameInstanceAs(stream.transportState());
assertThat(ImmutableListMultimap.copyOf(sendHeaders.headers())).containsExactlyEntriesIn(expectedHeaders);
assertThat(sendHeaders.endOfStream()).isTrue();
verifyNoInteractions(serverListener);
// Sending complete. Listener gets closed()
stream().transportState().complete();
verify(serverListener).closed(Status.OK);
assertNull("no message expected", listenerMessageQueue.poll());
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.
the class NettyServerStreamTest method writeMessageShouldSendResponse.
@Test
public void writeMessageShouldSendResponse() throws Exception {
ListMultimap<CharSequence, CharSequence> expectedHeaders = ImmutableListMultimap.copyOf(new DefaultHttp2Headers().status(Utils.STATUS_OK).set(Utils.CONTENT_TYPE_HEADER, Utils.CONTENT_TYPE_GRPC));
stream.writeHeaders(new Metadata());
ArgumentCaptor<SendResponseHeadersCommand> sendHeadersCap = ArgumentCaptor.forClass(SendResponseHeadersCommand.class);
verify(writeQueue).enqueue(sendHeadersCap.capture(), eq(true));
SendResponseHeadersCommand sendHeaders = sendHeadersCap.getValue();
assertThat(sendHeaders.stream()).isSameInstanceAs(stream.transportState());
assertThat(ImmutableListMultimap.copyOf(sendHeaders.headers())).containsExactlyEntriesIn(expectedHeaders);
assertThat(sendHeaders.endOfStream()).isFalse();
byte[] msg = smallMessage();
stream.writeMessage(new ByteArrayInputStream(msg));
stream.flush();
verify(writeQueue).enqueue(eq(new SendGrpcFrameCommand(stream.transportState(), messageFrame(MESSAGE), false)), eq(true));
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.
the class UtilsTest method convertClientHeaders_sanitizes.
@Test
public void convertClientHeaders_sanitizes() {
Metadata metaData = new Metadata();
// Intentionally being explicit here rather than relying on any pre-defined lists of headers,
// since the goal of this test is to validate the correctness of such lists in the first place.
metaData.put(GrpcUtil.CONTENT_TYPE_KEY, "to-be-removed");
metaData.put(GrpcUtil.USER_AGENT_KEY, "to-be-removed");
metaData.put(GrpcUtil.TE_HEADER, "to-be-removed");
metaData.put(userKey, userValue);
String scheme = "https";
String userAgent = "user-agent";
String method = "POST";
String authority = "authority";
String path = "//testService/test";
Http2Headers output = Utils.convertClientHeaders(metaData, new AsciiString(scheme), new AsciiString(path), new AsciiString(authority), new AsciiString(method), new AsciiString(userAgent));
DefaultHttp2Headers headers = new DefaultHttp2Headers();
for (Map.Entry<CharSequence, CharSequence> entry : output) {
headers.add(entry.getKey(), entry.getValue());
}
// 7 reserved headers, 1 user header
assertEquals(7 + 1, headers.size());
// Check the 3 reserved headers that are non pseudo
// Users can not create pseudo headers keys so no need to check for them here
assertEquals(GrpcUtil.CONTENT_TYPE_GRPC, headers.get(GrpcUtil.CONTENT_TYPE_KEY.name()).toString());
assertEquals(userAgent, headers.get(GrpcUtil.USER_AGENT_KEY.name()).toString());
assertEquals(GrpcUtil.TE_TRAILERS, headers.get(GrpcUtil.TE_HEADER.name()).toString());
// Check the user header is in tact
assertEquals(userValue, headers.get(userKey.name()).toString());
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.
the class UtilsTest method convertServerHeaders_sanitizes.
@Test
// AsciiString.equals
@SuppressWarnings("UndefinedEquals")
public void convertServerHeaders_sanitizes() {
Metadata metaData = new Metadata();
// Intentionally being explicit here rather than relying on any pre-defined lists of headers,
// since the goal of this test is to validate the correctness of such lists in the first place.
metaData.put(GrpcUtil.CONTENT_TYPE_KEY, "to-be-removed");
metaData.put(GrpcUtil.TE_HEADER, "to-be-removed");
metaData.put(GrpcUtil.USER_AGENT_KEY, "to-be-removed");
metaData.put(userKey, userValue);
Http2Headers output = Utils.convertServerHeaders(metaData);
DefaultHttp2Headers headers = new DefaultHttp2Headers();
for (Map.Entry<CharSequence, CharSequence> entry : output) {
headers.add(entry.getKey(), entry.getValue());
}
// 2 reserved headers, 1 user header
assertEquals(2 + 1, headers.size());
assertEquals(Utils.CONTENT_TYPE_GRPC, headers.get(GrpcUtil.CONTENT_TYPE_KEY.name()));
}
use of io.netty.handler.codec.http2.DefaultHttp2Headers in project netty by netty.
the class DataCompressionHttp2Test method zstdEncodingSingleMessage.
@Test
public void zstdEncodingSingleMessage() throws Exception {
final String text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccc";
final ByteBuf data = Unpooled.copiedBuffer(text.getBytes(CharsetUtil.UTF_8.name()));
bootstrapEnv(data.readableBytes());
try {
final Http2Headers headers = new DefaultHttp2Headers().method(POST).path(PATH).set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.ZSTD);
runInChannel(clientChannel, new Http2Runnable() {
@Override
public void run() throws Http2Exception {
clientEncoder.writeHeaders(ctxClient(), 3, headers, 0, false, newPromiseClient());
clientEncoder.writeData(ctxClient(), 3, data.retain(), 0, true, newPromiseClient());
clientHandler.flush(ctxClient());
}
});
awaitServer();
assertEquals(text, serverOut.toString(CharsetUtil.UTF_8.name()));
} finally {
data.release();
}
}
Aggregations