use of io.servicetalk.http.api.HttpResponse in project servicetalk by apple.
the class GrpcUtils method newErrorResponse.
static HttpResponse newErrorResponse(final HttpResponseFactory responseFactory, final CharSequence contentType, final Throwable cause, final BufferAllocator allocator) {
final HttpResponse response = responseFactory.ok();
initResponse(response, contentType, null, null);
setStatus(response.headers(), cause, allocator);
return response;
}
use of io.servicetalk.http.api.HttpResponse in project servicetalk by apple.
the class BlockingUdsClient method main.
public static void main(String[] args) throws Exception {
try (BlockingHttpClient client = HttpClients.forResolvedAddress(udsAddress()).buildBlocking()) {
HttpResponse response = client.request(client.get("/sayHello"));
System.out.println(response.toString((name, value) -> value));
System.out.println(response.payloadBody(textSerializerUtf8()));
}
}
use of io.servicetalk.http.api.HttpResponse in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method serverWriteTrailers.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void serverWriteTrailers(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
String payloadBody = "foo";
String myTrailerName = "mytrailer";
h1ServerContext = HttpServers.forAddress(localAddress(0)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).listenStreaming((ctx, request, responseFactory) -> request.payloadBody().map(Buffer::readableBytes).collect(AtomicInteger::new, (contentSize, bufferSize) -> {
contentSize.addAndGet(bufferSize);
return contentSize;
}).flatMap(contentSize -> succeeded(responseFactory.ok().transform(new ContentSizeTrailersTransformer(myTrailerName, contentSize))))).toFuture().get();
InetSocketAddress serverAddress = (InetSocketAddress) h1ServerContext.listenAddress();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpRequest request = client.post("/").payloadBody(payloadBody, textSerializerUtf8());
HttpResponse response = client.request(request);
assertEquals(0, response.payloadBody().readableBytes());
CharSequence responseTrailer = response.trailers().get(myTrailerName);
assertNotNull(responseTrailer);
assertEquals(payloadBody.length(), Integer.parseInt(responseTrailer.toString()));
}
}
use of io.servicetalk.http.api.HttpResponse in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientWriteTrailers.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void clientWriteTrailers(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
InetSocketAddress serverAddress = bindHttpEchoServer();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
String payloadBody = "foo";
String myTrailerName = "mytrailer";
String myTrailerValue = "myvalue";
HttpRequest request = client.post("/").payloadBody(payloadBody, textSerializerUtf8());
request.trailers().add(myTrailerName, myTrailerValue);
HttpResponse response = client.request(request);
assertEquals(payloadBody, response.payloadBody(textSerializerUtf8()));
CharSequence responseTrailer = response.trailers().get(myTrailerName);
assertNotNull(responseTrailer);
assertEquals(0, responseTrailer.toString().compareToIgnoreCase(myTrailerValue));
}
}
use of io.servicetalk.http.api.HttpResponse in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method serverThrowsFromHandler.
@ParameterizedTest(name = "{displayName} [{index}] client={0}, h2PriorKnowledge={1}")
@MethodSource("clientExecutors")
void serverThrowsFromHandler(HttpTestExecutionStrategy strategy, boolean h2PriorKnowledge) throws Exception {
setUp(strategy, h2PriorKnowledge);
InetSocketAddress serverAddress = bindHttpEchoServer(service -> new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
throw DELIBERATE_EXCEPTION;
}
}, null);
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).buildBlocking()) {
HttpResponse response = client.request(client.get("/"));
assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
assertThat(response.payloadBody(), equalTo(EMPTY_BUFFER));
}
}
Aggregations