Search in sources :

Example 61 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project grpc-java by grpc.

the class GrpcHttp2InboundHeadersTest method basicCorrectness.

@Test
public void basicCorrectness() {
    Http2Headers headers = new GrpcHttp2RequestHeaders(1);
    headers.add(of(":method"), of("POST"));
    headers.add(of("content-type"), of("application/grpc+proto"));
    headers.add(of(":path"), of("/google.pubsub.v2.PublisherService/CreateTopic"));
    headers.add(of(":scheme"), of("https"));
    headers.add(of("te"), of("trailers"));
    headers.add(of(":authority"), of("pubsub.googleapis.com"));
    headers.add(of("foo"), of("bar"));
    assertEquals(7, headers.size());
    // Number of headers without the pseudo headers and 'te' header.
    assertEquals(2, ((GrpcHttp2InboundHeaders) headers).numHeaders());
    assertEquals(of("application/grpc+proto"), headers.get(of("content-type")));
    assertEquals(of("/google.pubsub.v2.PublisherService/CreateTopic"), headers.path());
    assertEquals(of("https"), headers.scheme());
    assertEquals(of("POST"), headers.method());
    assertEquals(of("pubsub.googleapis.com"), headers.authority());
    assertEquals(of("trailers"), headers.get(of("te")));
    assertEquals(of("bar"), headers.get(of("foo")));
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) GrpcHttp2RequestHeaders(io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2RequestHeaders) Test(org.junit.Test)

Example 62 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project grpc-java by grpc.

the class GrpcHttp2InboundHeadersTest method binaryHeadersShouldBeBase64Decoded.

@Test
public void binaryHeadersShouldBeBase64Decoded() {
    Http2Headers headers = new GrpcHttp2RequestHeaders(1);
    byte[] data = new byte[100];
    new Random().nextBytes(data);
    headers.add(of("foo-bin"), of(BASE64_ENCODING_OMIT_PADDING.encode(data)));
    assertEquals(1, headers.size());
    byte[][] namesAndValues = ((GrpcHttp2InboundHeaders) headers).namesAndValues();
    assertEquals(of("foo-bin"), new AsciiString(namesAndValues[0]));
    assertNotSame(data, namesAndValues[1]);
    assertArrayEquals(data, namesAndValues[1]);
}
Also used : Random(java.util.Random) Http2Headers(io.netty.handler.codec.http2.Http2Headers) GrpcHttp2RequestHeaders(io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2RequestHeaders) GrpcHttp2InboundHeaders(io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2InboundHeaders) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 63 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project grpc-java by grpc.

the class NettyServerHandlerTest method headersWithAuthorityAndHostUsesAuthority.

@Test
public void headersWithAuthorityAndHostUsesAuthority() throws Exception {
    manualSetUp();
    Http2Headers headers = new DefaultHttp2Headers().method(HTTP_METHOD).authority("example.com").set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC).add(AsciiString.of("host"), AsciiString.of("bad.com")).path(new AsciiString("/foo/bar"));
    ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
    channelRead(headersFrame);
    Metadata.Key<String> hostKey = Metadata.Key.of("host", Metadata.ASCII_STRING_MARSHALLER);
    ArgumentCaptor<NettyServerStream> streamCaptor = ArgumentCaptor.forClass(NettyServerStream.class);
    ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
    verify(transportListener).streamCreated(streamCaptor.capture(), eq("foo/bar"), metadataCaptor.capture());
    Truth.assertThat(streamCaptor.getValue().getAuthority()).isEqualTo("example.com");
    Truth.assertThat(metadataCaptor.getValue().get(hostKey)).isNull();
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) Metadata(io.grpc.Metadata) AsciiString(io.netty.util.AsciiString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 64 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project grpc-java by grpc.

the class NettyServerHandlerTest method headersWithInvalidContentTypeShouldFail.

@Test
public void headersWithInvalidContentTypeShouldFail() throws Exception {
    manualSetUp();
    Http2Headers headers = new DefaultHttp2Headers().method(HTTP_METHOD).set(CONTENT_TYPE_HEADER, new AsciiString("application/bad", UTF_8)).set(TE_HEADER, TE_TRAILERS).path(new AsciiString("/foo/bar"));
    ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
    channelRead(headersFrame);
    Http2Headers responseHeaders = new DefaultHttp2Headers().set(InternalStatus.CODE_KEY.name(), String.valueOf(Code.INTERNAL.value())).set(InternalStatus.MESSAGE_KEY.name(), "Content-Type 'application/bad' is not supported").status("" + 415).set(CONTENT_TYPE_HEADER, "text/plain; charset=utf-8");
    verifyWrite().writeHeaders(eq(ctx()), eq(STREAM_ID), eq(responseHeaders), eq(0), eq(false), any(ChannelPromise.class));
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 65 with Http2Headers

use of io.netty.handler.codec.http2.Http2Headers in project grpc-java by grpc.

the class NettyServerHandlerTest method headersWithMissingPathShouldFail.

@Test
public void headersWithMissingPathShouldFail() throws Exception {
    manualSetUp();
    Http2Headers headers = new DefaultHttp2Headers().method(HTTP_METHOD).set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC);
    ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
    channelRead(headersFrame);
    Http2Headers responseHeaders = new DefaultHttp2Headers().set(InternalStatus.CODE_KEY.name(), String.valueOf(Code.UNIMPLEMENTED.value())).set(InternalStatus.MESSAGE_KEY.name(), "Expected path but is missing").status("" + 404).set(CONTENT_TYPE_HEADER, "text/plain; charset=utf-8");
    verifyWrite().writeHeaders(eq(ctx()), eq(STREAM_ID), eq(responseHeaders), eq(0), eq(false), any(ChannelPromise.class));
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

Http2Headers (io.netty.handler.codec.http2.Http2Headers)126 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)111 ByteBuf (io.netty.buffer.ByteBuf)103 Test (org.junit.Test)97 ChannelFuture (io.netty.channel.ChannelFuture)76 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)76 AsciiString (io.netty.util.AsciiString)58 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)58 AtomicReference (java.util.concurrent.atomic.AtomicReference)56 ByteArrayOutputStream (java.io.ByteArrayOutputStream)54 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)54 Channel (io.netty.channel.Channel)53 Collections (java.util.Collections)53 ChannelInitializer (io.netty.channel.ChannelInitializer)52 ChannelPipeline (io.netty.channel.ChannelPipeline)52 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)52 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)52 ApplicationProtocolNames (io.netty.handler.ssl.ApplicationProtocolNames)52 ApplicationProtocolNegotiationHandler (io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)52 SslHandler (io.netty.handler.ssl.SslHandler)52