Search in sources :

Example 1 with Header

use of io.grpc.okhttp.internal.framed.Header in project grpc-java by grpc.

the class Utils method convertHeadersToArray.

private static byte[][] convertHeadersToArray(List<Header> http2Headers) {
    byte[][] headerValues = new byte[http2Headers.size() * 2][];
    int i = 0;
    for (Header header : http2Headers) {
        headerValues[i++] = header.name.toByteArray();
        headerValues[i++] = header.value.toByteArray();
    }
    return TransportFrameUtil.toRawSerializedHeaders(headerValues);
}
Also used : Header(io.grpc.okhttp.internal.framed.Header)

Example 2 with Header

use of io.grpc.okhttp.internal.framed.Header in project grpc-java by grpc.

the class OkHttpClientTransportTest method invalidInboundHeadersCancelStream.

@Test
public void invalidInboundHeadersCancelStream() throws Exception {
    initTransport();
    MockStreamListener listener = new MockStreamListener();
    OkHttpClientStream stream = clientTransport.newStream(method, new Metadata());
    stream.start(listener);
    stream.request(1);
    assertContainStream(3);
    // Headers block without correct content type or status
    frameHandler().headers(false, false, 3, 0, Arrays.asList(new Header("random", "4")), HeadersMode.HTTP_20_HEADERS);
    // Now wait to receive 1000 bytes of data so we can have a better error message before
    // cancelling the streaam.
    frameHandler().data(false, 3, createMessageFrame(new String(new char[1000])), 1000);
    verify(frameWriter, timeout(TIME_OUT_MS)).rstStream(eq(3), eq(ErrorCode.CANCEL));
    assertNull(listener.headers);
    assertEquals(Status.INTERNAL.getCode(), listener.status.getCode());
    assertNotNull(listener.trailers);
    assertEquals("4", listener.trailers.get(Metadata.Key.of("random", Metadata.ASCII_STRING_MARSHALLER)));
    shutdownAndVerify();
}
Also used : Header(io.grpc.okhttp.internal.framed.Header) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 3 with Header

use of io.grpc.okhttp.internal.framed.Header in project grpc-java by grpc.

the class OkHttpClientStreamTest method start_headerFieldOrder.

@Test
public void start_headerFieldOrder() {
    Metadata metaData = new Metadata();
    metaData.put(GrpcUtil.USER_AGENT_KEY, "misbehaving-application");
    stream = new OkHttpClientStream(methodDescriptor, metaData, frameWriter, transport, flowController, lock, MAX_MESSAGE_SIZE, "localhost", "good-application", StatsTraceContext.NOOP);
    stream.start(new BaseClientStreamListener());
    stream.start(3);
    verify(frameWriter).synStream(eq(false), eq(false), eq(3), eq(0), headersCaptor.capture());
    assertThat(headersCaptor.getValue()).containsExactly(Headers.SCHEME_HEADER, Headers.METHOD_HEADER, new Header(Header.TARGET_AUTHORITY, "localhost"), new Header(Header.TARGET_PATH, "/" + methodDescriptor.getFullMethodName()), new Header(GrpcUtil.USER_AGENT_KEY.name(), "good-application"), Headers.CONTENT_TYPE_HEADER, Headers.TE_HEADER).inOrder();
}
Also used : Header(io.grpc.okhttp.internal.framed.Header) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 4 with Header

use of io.grpc.okhttp.internal.framed.Header in project grpc-java by grpc.

the class OkHttpClientStreamTest method start_userAgentRemoved.

@Test
public void start_userAgentRemoved() {
    Metadata metaData = new Metadata();
    metaData.put(GrpcUtil.USER_AGENT_KEY, "misbehaving-application");
    stream = new OkHttpClientStream(methodDescriptor, metaData, frameWriter, transport, flowController, lock, MAX_MESSAGE_SIZE, "localhost", "good-application", StatsTraceContext.NOOP);
    stream.start(new BaseClientStreamListener());
    stream.start(3);
    verify(frameWriter).synStream(eq(false), eq(false), eq(3), eq(0), headersCaptor.capture());
    assertThat(headersCaptor.getValue()).contains(new Header(GrpcUtil.USER_AGENT_KEY.name(), "good-application"));
}
Also used : Header(io.grpc.okhttp.internal.framed.Header) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 5 with Header

use of io.grpc.okhttp.internal.framed.Header in project grpc-java by grpc.

the class OkHttpClientTransportTest method addDefaultUserAgent.

@Test
public void addDefaultUserAgent() throws Exception {
    initTransport();
    MockStreamListener listener = new MockStreamListener();
    OkHttpClientStream stream = clientTransport.newStream(method, new Metadata());
    stream.start(listener);
    Header userAgentHeader = new Header(GrpcUtil.USER_AGENT_KEY.name(), GrpcUtil.getGrpcUserAgent("okhttp", null));
    List<Header> expectedHeaders = Arrays.asList(SCHEME_HEADER, METHOD_HEADER, new Header(Header.TARGET_AUTHORITY, "notarealauthority:80"), new Header(Header.TARGET_PATH, "/" + method.getFullMethodName()), userAgentHeader, CONTENT_TYPE_HEADER, TE_HEADER);
    verify(frameWriter, timeout(TIME_OUT_MS)).synStream(eq(false), eq(false), eq(3), eq(0), eq(expectedHeaders));
    getStream(3).cancel(Status.CANCELLED);
    shutdownAndVerify();
}
Also used : Header(io.grpc.okhttp.internal.framed.Header) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Aggregations

Header (io.grpc.okhttp.internal.framed.Header)8 Metadata (io.grpc.Metadata)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)1 ByteString (okio.ByteString)1