Search in sources :

Example 26 with AsciiString

use of io.netty.util.AsciiString 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(BaseEncoding.base64().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.GrpcHttp2HeadersDecoder.GrpcHttp2RequestHeaders) GrpcHttp2InboundHeaders(io.grpc.netty.GrpcHttp2HeadersDecoder.GrpcHttp2InboundHeaders) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 27 with AsciiString

use of io.netty.util.AsciiString in project grpc-java by grpc.

the class NettyServerHandlerTest method headersWithInvalidContentTypeShouldFail.

@Test
public void headersWithInvalidContentTypeShouldFail() throws Exception {
    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);
    verifyWrite().writeRstStream(eq(ctx()), eq(STREAM_ID), eq(Http2Error.REFUSED_STREAM.code()), 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 28 with AsciiString

use of io.netty.util.AsciiString in project grpc-java by grpc.

the class NettyServerStreamTest method emptyFramerShouldSendNoPayload.

@Test
public void emptyFramerShouldSendNoPayload() {
    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("0")));
    ArgumentCaptor<SendResponseHeadersCommand> cmdCap = ArgumentCaptor.forClass(SendResponseHeadersCommand.class);
    stream().close(Status.OK, new Metadata());
    verify(writeQueue).enqueue(cmdCap.capture(), eq(true));
    SendResponseHeadersCommand cmd = cmdCap.getValue();
    assertThat(cmd.stream()).isSameAs(stream.transportState());
    assertThat(ImmutableListMultimap.copyOf(cmd.headers())).containsExactlyEntriesIn(expectedHeaders);
    assertThat(cmd.endOfStream()).isTrue();
}
Also used : DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 29 with AsciiString

use of io.netty.util.AsciiString in project grpc-java by grpc.

the class NettyServerStreamTest method closeAfterClientHalfCloseShouldSucceed.

@Test
public void closeAfterClientHalfCloseShouldSucceed() 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("0")));
    // Client half-closes. Listener gets halfClosed()
    stream().transportState().inboundDataReceived(new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT), true);
    verify(serverListener).halfClosed();
    // Server closes. Status sent
    stream().close(Status.OK, trailers);
    verifyNoMoreInteractions(serverListener);
    ArgumentCaptor<SendResponseHeadersCommand> cmdCap = ArgumentCaptor.forClass(SendResponseHeadersCommand.class);
    verify(writeQueue).enqueue(cmdCap.capture(), eq(true));
    SendResponseHeadersCommand cmd = cmdCap.getValue();
    assertThat(cmd.stream()).isSameAs(stream.transportState());
    assertThat(ImmutableListMultimap.copyOf(cmd.headers())).containsExactlyEntriesIn(expectedHeaders);
    assertThat(cmd.endOfStream()).isTrue();
    // Sending and receiving complete. Listener gets closed()
    stream().transportState().complete();
    verify(serverListener).closed(Status.OK);
    verifyNoMoreInteractions(serverListener);
}
Also used : EmptyByteBuf(io.netty.buffer.EmptyByteBuf) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 30 with AsciiString

use of io.netty.util.AsciiString in project grpc-java by grpc.

the class NettyServerStreamTest method closeBeforeClientHalfCloseShouldSucceed.

@Test
public void closeBeforeClientHalfCloseShouldSucceed() 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("0")));
    stream().close(Status.OK, new Metadata());
    ArgumentCaptor<SendResponseHeadersCommand> sendHeadersCap = ArgumentCaptor.forClass(SendResponseHeadersCommand.class);
    verify(writeQueue).enqueue(sendHeadersCap.capture(), eq(true));
    SendResponseHeadersCommand sendHeaders = sendHeadersCap.getValue();
    assertThat(sendHeaders.stream()).isSameAs(stream.transportState());
    assertThat(ImmutableListMultimap.copyOf(sendHeaders.headers())).containsExactlyEntriesIn(expectedHeaders);
    assertThat(sendHeaders.endOfStream()).isTrue();
    verifyZeroInteractions(serverListener);
    // Sending complete. Listener gets closed()
    stream().transportState().complete();
    verify(serverListener).closed(Status.OK);
    verifyZeroInteractions(serverListener);
}
Also used : DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Aggregations

AsciiString (io.netty.util.AsciiString)73 Test (org.junit.Test)43 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)27 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)25 ByteBuf (io.netty.buffer.ByteBuf)18 ChannelPromise (io.netty.channel.ChannelPromise)15 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)12 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)12 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)12 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)11 Http2Headers (io.netty.handler.codec.http2.Http2Headers)9 Metadata (io.grpc.Metadata)8 ChannelFuture (io.netty.channel.ChannelFuture)4 Status (io.grpc.Status)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Setup (org.openjdk.jmh.annotations.Setup)3 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2