use of io.servicetalk.http.api.HttpResponseStatus.OK in project servicetalk by apple.
the class HttpClientAsyncContextTest method makeClientRequestWithId.
private static void makeClientRequestWithId(StreamingHttpClient client, String requestId) throws ExecutionException, InterruptedException {
StreamingHttpRequest request = client.get("/");
request.headers().set(REQUEST_ID_HEADER, requestId);
client.request(request).whenOnSuccess(response -> assertEquals(OK, response.status())).flatMapCompletable(response -> response.messageBody().ignoreElements()).toFuture().get();
}
use of io.servicetalk.http.api.HttpResponseStatus.OK in project servicetalk by apple.
the class HttpAuthConnectionFactoryClientTest method simulateAuth.
@Test
void simulateAuth() throws Exception {
serverContext = forAddress(localAddress(0)).ioExecutor(CTX.ioExecutor()).executionStrategy(offloadNever()).listenStreamingAndAwait((ctx, request, factory) -> succeeded(newTestResponse(factory)));
client = forSingleAddress(serverHostAndPort(serverContext)).appendConnectionFactoryFilter(TestHttpAuthConnectionFactory::new).ioExecutor(CTX.ioExecutor()).executionStrategy(offloadNever()).buildStreaming();
StreamingHttpResponse response = client.request(newTestRequest(client, "/foo")).toFuture().get();
assertEquals(OK, response.status());
}
use of io.servicetalk.http.api.HttpResponseStatus.OK in project servicetalk by apple.
the class ConnectionAcceptingNettyHttpServerTest method assertConnectionRequestSucceeds.
private void assertConnectionRequestSucceeds(final StreamingHttpRequest request) throws Exception {
final StreamingHttpResponse response = awaitIndefinitely(streamingHttpClient().reserveConnection(request).flatMap(conn -> conn.request(request)));
assert response != null;
assertResponse(response, HTTP_1_1, OK, "");
}
use of io.servicetalk.http.api.HttpResponseStatus.OK in project servicetalk by apple.
the class HttpConnectionEmptyPayloadTest method headRequestContentEmpty.
@Test
void headRequestContentEmpty() throws Exception {
try (CompositeCloseable closeable = AsyncCloseables.newCompositeCloseable()) {
final int expectedContentLength = 128;
byte[] expectedPayload = new byte[expectedContentLength];
ThreadLocalRandom.current().nextBytes(expectedPayload);
ServerContext serverContext = closeable.merge(HttpServers.forAddress(localAddress(0)).ioExecutor(executionContextRule.ioExecutor()).executionStrategy(offloadNever()).listenStreamingAndAwait((ctx, req, factory) -> {
StreamingHttpResponse resp = factory.ok().payloadBody(from(HEAD.equals(req.method()) ? EMPTY_BUFFER : ctx.executionContext().bufferAllocator().newBuffer(expectedContentLength).writeBytes(expectedPayload)));
resp.addHeader(CONTENT_LENGTH, String.valueOf(expectedContentLength));
return succeeded(resp);
}));
StreamingHttpClient client = closeable.merge(forResolvedAddress(serverHostAndPort(serverContext)).ioExecutor(executionContextRule.ioExecutor()).protocols(h1().maxPipelinedRequests(3).build()).executor(executionContextRule.executor()).executionStrategy(defaultStrategy()).buildStreaming());
StreamingHttpConnection connection = closeable.merge(client.reserveConnection(client.get("/")).toFuture().get());
// Request HEAD, GET, HEAD to verify that we can keep reading data despite a HEAD request providing a hint
// about content-length (and not actually providing the content).
Single<StreamingHttpResponse> response1Single = connection.request(connection.newRequest(HEAD, "/"));
Single<StreamingHttpResponse> response2Single = connection.request(connection.get("/"));
Single<StreamingHttpResponse> response3Single = connection.request(connection.newRequest(HEAD, "/"));
StreamingHttpResponse response = awaitIndefinitelyNonNull(response1Single);
assertEquals(OK, response.status());
CharSequence contentLength = response.headers().get(CONTENT_LENGTH);
assertNotNull(contentLength);
assertEquals(expectedContentLength, parseInt(contentLength.toString()));
// Drain the current response content so we will be able to read the next response.
response.messageBody().ignoreElements().toFuture().get();
response = awaitIndefinitelyNonNull(response2Single);
assertEquals(OK, response.status());
contentLength = response.headers().get(CONTENT_LENGTH);
assertNotNull(contentLength);
assertEquals(expectedContentLength, parseInt(contentLength.toString()));
Buffer buffer = awaitIndefinitelyNonNull(response.payloadBody().collect(() -> connection.connectionContext().executionContext().bufferAllocator().newBuffer(), Buffer::writeBytes));
byte[] actualBytes = new byte[buffer.readableBytes()];
buffer.readBytes(actualBytes);
assertArrayEquals(expectedPayload, actualBytes);
response = awaitIndefinitelyNonNull(response3Single);
assertEquals(OK, response.status());
contentLength = response.headers().get(CONTENT_LENGTH);
assertNotNull(contentLength);
assertEquals(expectedContentLength, parseInt(contentLength.toString()));
response.messageBody().ignoreElements().toFuture().get();
}
}
use of io.servicetalk.http.api.HttpResponseStatus.OK in project servicetalk by apple.
the class BlockingStreamingToStreamingServiceTest method throwAfterSendMetaData.
@Test
void throwAfterSendMetaData() throws Exception {
CountDownLatch onErrorLatch = new CountDownLatch(1);
AtomicReference<Throwable> throwableRef = new AtomicReference<>();
BlockingStreamingHttpService syncService = (ctx, request, response) -> {
response.sendMetaData();
throw DELIBERATE_EXCEPTION;
};
StreamingHttpService asyncService = toStreamingHttpService(syncService, offloadNone()).adaptor();
StreamingHttpResponse asyncResponse = asyncService.handle(mockCtx, reqRespFactory.get("/"), reqRespFactory).subscribeOn(executorExtension.executor()).toFuture().get();
assertMetaData(OK, asyncResponse);
toSource(asyncResponse.payloadBody()).subscribe(new Subscriber<Buffer>() {
@Override
public void onSubscribe(final Subscription s) {
}
@Override
public void onNext(final Buffer s) {
}
@Override
public void onError(final Throwable t) {
throwableRef.set(t);
onErrorLatch.countDown();
}
@Override
public void onComplete() {
}
});
onErrorLatch.await();
assertThat(throwableRef.get(), is(DELIBERATE_EXCEPTION));
}
Aggregations