use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class TrailersOnlyErrorTest method testServiceSingleThrows.
@Test
void testServiceSingleThrows() throws Exception {
final BlockingQueue<Throwable> asyncErrors = new LinkedBlockingDeque<>();
final TesterService service = mockTesterService();
setupServiceSingleThrows(service);
try (ServerContext serverContext = GrpcServers.forAddress(localAddress(0)).listenAndAwait(new Tester.ServiceFactory(service))) {
final GrpcClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = GrpcClients.forAddress(serverHostAndPort(serverContext)).initializeHttp(builder -> builder.appendClientFilter(__ -> true, setupResponseVerifierFilter(asyncErrors)));
try (TesterClient client = clientBuilder.build(new Tester.ClientFactory())) {
verifyException(client.test(TestRequest.newBuilder().build()).toFuture(), UNKNOWN);
assertNoAsyncErrors(asyncErrors);
}
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class TrailersOnlyErrorTest method testRouteThrows.
@Test
void testRouteThrows() throws Exception {
final BlockingQueue<Throwable> asyncErrors = new LinkedBlockingDeque<>();
try (ServerContext serverContext = GrpcServers.forAddress(localAddress(0)).listenAndAwait(new Tester.ServiceFactory(mockTesterService()))) {
final GrpcClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = GrpcClients.forAddress(serverHostAndPort(serverContext)).initializeHttp(builder -> builder.appendClientFilter(__ -> true, setupResponseVerifierFilter(asyncErrors)));
// thus no route is found and it should result in UNIMPLEMENTED.
try (GreeterClient client = clientBuilder.build(new Greeter.ClientFactory())) {
verifyException(client.sayHello(HelloRequest.newBuilder().build()).toFuture(), UNIMPLEMENTED);
assertNoAsyncErrors(asyncErrors);
}
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class TrailersOnlyErrorTest method testServiceFilterThrows.
@Test
void testServiceFilterThrows() throws Exception {
final BlockingQueue<Throwable> asyncErrors = new LinkedBlockingDeque<>();
final TesterService service = mockTesterService();
final GrpcServerBuilder serverBuilder = GrpcServers.forAddress(localAddress(0)).initializeHttp(builder -> builder.appendServiceFilter(svc -> new StreamingHttpServiceFilter(svc) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
throw DELIBERATE_EXCEPTION;
}
}));
try (ServerContext serverContext = serverBuilder.listenAndAwait(new Tester.ServiceFactory(service))) {
final GrpcClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = GrpcClients.forAddress(serverHostAndPort(serverContext)).initializeHttp(builder -> builder.appendClientFilter(__ -> true, setupResponseVerifierFilter(asyncErrors)));
try (TesterClient client = clientBuilder.build(new Tester.ClientFactory())) {
verifyException(client.test(TestRequest.newBuilder().build()).toFuture(), UNKNOWN);
assertNoAsyncErrors(asyncErrors);
verifyException(client.testRequestStream(Publisher.from(TestRequest.newBuilder().build())).toFuture(), UNKNOWN);
assertNoAsyncErrors(asyncErrors);
verifyException(client.testResponseStream(TestRequest.newBuilder().build()).toFuture(), UNKNOWN);
assertNoAsyncErrors(asyncErrors);
verifyException(client.testBiDiStream(from(TestRequest.newBuilder().build()).concat(never())).toFuture(), UNKNOWN);
assertNoAsyncErrors(asyncErrors);
verifyException(client.testBiDiStream(from(TestRequest.newBuilder().build())).toFuture(), UNKNOWN);
assertNoAsyncErrors(asyncErrors);
}
}
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class ServiceTalkContentCodingTest method newServiceTalkServer.
private ServerContext newServiceTalkServer(final Scenario scenario, final Queue<Throwable> errors) throws Exception {
HttpServerBuilder httpServerBuilder = HttpServers.forAddress(localAddress(0));
StreamingHttpService service = (ctx, request, responseFactory) -> succeeded(buildResponse(responseFactory));
StreamingHttpServiceFilterFactory filterFactory = REQ_FILTER.apply(scenario, errors);
return httpServerBuilder.executionStrategy(defaultStrategy()).protocols(scenario.protocol.config).appendServiceFilter(new ContentCodingHttpServiceFilter(scenario.serverSupported, scenario.serverSupported)).appendServiceFilter(filterFactory).listenStreamingAndAwait(service);
}
use of io.servicetalk.transport.api.ServerContext in project servicetalk by apple.
the class ServiceTalkContentEncodingTest method runTest.
@Override
protected void runTest(final HttpProtocol protocol, final Encoder clientEncoding, final Decoders clientDecoder, final Encoders serverEncoder, final Decoders serverDecoder, boolean valid) throws Throwable {
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).protocols(protocol.config).appendServiceFilter(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
return delegate().handle(ctx, request, responseFactory).map(resp -> {
// content encoding should be stripped by the time the decoding is done.
assertThat(resp.headers().get(ACCEPT_ENCODING), nullValue());
CharSequence contentEncoding = resp.headers().get(CONTENT_ENCODING);
boolean found = contentEncoding == null && (serverEncoder.list.isEmpty() || !request.headers().contains(ACCEPT_ENCODING));
for (BufferEncoder be : serverEncoder.list) {
if (contentEncoding == null && contentEqualsIgnoreCase(be.encodingName(), identityEncoder().encodingName()) || contentEncoding != null && contentEqualsIgnoreCase(be.encodingName(), contentEncoding)) {
found = true;
break;
}
}
return found || !valid ? resp : responseFactory.ok().payloadBody(Publisher.from("server error: invalid " + CONTENT_ENCODING + ": " + contentEncoding), appSerializerUtf8FixLen());
}).onErrorReturn(AssertionError.class, cause -> responseFactory.ok().payloadBody(Publisher.from("server error: " + cause.toString()), appSerializerUtf8FixLen()));
}
}).appendServiceFilter(new ContentEncodingHttpServiceFilter(serverEncoder.list, serverDecoder.group)).listenBlockingAndAwait((ctx, request, responseFactory) -> {
String requestPayload = request.payloadBody(textSerializerUtf8());
if (payloadAsString((byte) 'a').equals(requestPayload)) {
return responseFactory.ok().payloadBody(payloadAsString((byte) 'b'), textSerializerUtf8());
} else {
return responseFactory.badRequest().payloadBody(requestPayload, textSerializerUtf8());
}
});
BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).protocols(protocol.config).appendClientFilter(new ContentEncodingHttpRequesterFilter(clientDecoder.group)).appendClientFilter(c -> new StreamingHttpClientFilter(c) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return Single.defer(() -> {
assertHeader(() -> clientEncoding.encoder == null ? null : clientEncoding.encoder.encodingName(), request.headers().get(CONTENT_ENCODING), true);
assertHeader(clientDecoder.group::advertisedMessageEncoding, request.headers().get(ACCEPT_ENCODING), false);
return delegate.request(request).shareContextOnSubscribe();
});
}
}).buildBlocking()) {
HttpResponse response = client.request(client.get("/").contentEncoding(clientEncoding.encoder).payloadBody(payloadAsString((byte) 'a'), textSerializerUtf8()));
if (valid) {
assertThat(response.status(), is(OK));
// content encoding should be stripped by the time the decoding is done.
assertThat(response.headers().get(CONTENT_ENCODING), nullValue());
assertEquals(payloadAsString((byte) 'b'), response.payloadBody(textSerializerUtf8()));
} else {
assertThat(response.status(), is(UNSUPPORTED_MEDIA_TYPE));
}
}
}
Aggregations