use of io.grpc.okhttp.internal.framed.Header in project grpc-java by grpc.
the class OkHttpClientTransportTest method overrideDefaultUserAgent.
@Test
public void overrideDefaultUserAgent() throws Exception {
startTransport(3, null, true, DEFAULT_MAX_MESSAGE_SIZE, "fakeUserAgent");
MockStreamListener listener = new MockStreamListener();
OkHttpClientStream stream = clientTransport.newStream(method, new Metadata());
stream.start(listener);
List<Header> expectedHeaders = Arrays.asList(SCHEME_HEADER, METHOD_HEADER, new Header(Header.TARGET_AUTHORITY, "notarealauthority:80"), new Header(Header.TARGET_PATH, "/" + method.getFullMethodName()), new Header(GrpcUtil.USER_AGENT_KEY.name(), GrpcUtil.getGrpcUserAgent("okhttp", "fakeUserAgent")), 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();
}
use of io.grpc.okhttp.internal.framed.Header in project grpc-java by grpc.
the class OkHttpClientTransportTest method invalidInboundTrailersPropagateToMetadata.
@Test
public void invalidInboundTrailersPropagateToMetadata() throws Exception {
initTransport();
MockStreamListener listener = new MockStreamListener();
OkHttpClientStream stream = clientTransport.newStream(method, new Metadata());
stream.start(listener);
stream.request(1);
assertContainStream(3);
// Headers block with EOS without correct content type or status
frameHandler().headers(true, true, 3, 0, Arrays.asList(new Header("random", "4")), HeadersMode.HTTP_20_HEADERS);
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();
}
use of io.grpc.okhttp.internal.framed.Header in project grpc-java by grpc.
the class Headers method createRequestHeaders.
/**
* Serializes the given headers and creates a list of OkHttp {@link Header}s to be used when
* creating a stream. Since this serializes the headers, this method should be called in the
* application thread context.
*/
public static List<Header> createRequestHeaders(Metadata headers, String defaultPath, String authority, String userAgent) {
Preconditions.checkNotNull(headers, "headers");
Preconditions.checkNotNull(defaultPath, "defaultPath");
Preconditions.checkNotNull(authority, "authority");
// 7 is the number of explicit add calls below.
List<Header> okhttpHeaders = new ArrayList<Header>(7 + headers.headerCount());
// Set GRPC-specific headers.
okhttpHeaders.add(SCHEME_HEADER);
okhttpHeaders.add(METHOD_HEADER);
okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority));
String path = defaultPath;
okhttpHeaders.add(new Header(Header.TARGET_PATH, path));
okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent));
// All non-pseudo headers must come after pseudo headers.
okhttpHeaders.add(CONTENT_TYPE_HEADER);
okhttpHeaders.add(TE_HEADER);
// Now add any application-provided headers.
byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers);
for (int i = 0; i < serializedHeaders.length; i += 2) {
ByteString key = ByteString.of(serializedHeaders[i]);
String keyString = key.utf8();
if (isApplicationHeader(keyString)) {
ByteString value = ByteString.of(serializedHeaders[i + 1]);
okhttpHeaders.add(new Header(key, value));
}
}
return okhttpHeaders;
}
Aggregations