use of io.servicetalk.http.api.StreamingHttpRequester in project servicetalk by apple.
the class H2PriorKnowledgeFeatureParityTest method clientSendsInvalidContentLength.
private void clientSendsInvalidContentLength(boolean addTrailers, BiConsumer<HttpHeaders, Integer> headersModifier) throws Exception {
assumeFalse(!h2PriorKnowledge && addTrailers, "HTTP/1.1 does not support Content-Length with trailers");
InetSocketAddress serverAddress = bindHttpEchoServer();
try (BlockingHttpClient client = forSingleAddress(HostAndPort.of(serverAddress)).protocols(h2PriorKnowledge ? h2Default() : h1Default()).executionStrategy(clientExecutionStrategy).appendClientFilter(client1 -> new StreamingHttpClientFilter(client1) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return request.toRequest().map(req -> {
req.headers().remove(TRANSFER_ENCODING);
headersModifier.accept(req.headers(), req.payloadBody().readableBytes());
return req.toStreamingRequest();
}).flatMap(delegate::request);
}
}).buildBlocking()) {
HttpRequest request = client.get("/").payloadBody("a", textSerializerUtf8());
if (addTrailers) {
request.trailers().set("mytrailer", "myvalue");
}
if (h2PriorKnowledge) {
assertThrows(H2StreamResetException.class, () -> client.request(request));
} else {
try (ReservedBlockingHttpConnection reservedConn = client.reserveConnection(request)) {
assertThrows(IOException.class, () -> {
// Either the current request or the next one should fail
reservedConn.request(request);
reservedConn.request(client.get("/"));
});
}
}
}
}
use of io.servicetalk.http.api.StreamingHttpRequester in project servicetalk by apple.
the class ResponseTimeoutTest method setUp.
private void setUp(Duration clientTimeout, Duration serverTimeout) throws Exception {
ctx = forAddress(localAddress(0)).appendServiceFilter(new TimeoutHttpServiceFilter((req, ts) -> serverTimeout, true)).listenAndAwait((__, ___, factory) -> {
Single<HttpResponse> resp = Single.never();
serverResponses.add(resp);
return resp;
});
client = forSingleAddress(serverHostAndPort(ctx)).appendClientFilter(client -> new StreamingHttpClientFilter(client) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return Single.succeeded(null).afterOnSubscribe(delayedClientCancels::add).concat(delegate().request(request).liftSync(target -> new Subscriber<StreamingHttpResponse>() {
@Override
public void onSubscribe(final Cancellable cancellable) {
target.onSubscribe(() -> {
delayedClientCancels.add(cancellable);
cancellable.cancel();
});
}
@Override
public void onSuccess(final StreamingHttpResponse result) {
ClientTerminationSignal signal = OK.equals(result.status()) ? new ClientTerminationSignal(target, result) : new ClientTerminationSignal(target, new HttpResponseStatusException(result.status()));
delayedClientTermination.add(signal);
target.onSuccess(result);
}
@Override
public void onError(final Throwable t) {
delayedClientTermination.add(new ClientTerminationSignal(target, t));
target.onError(t);
}
})).filter(Objects::nonNull).firstOrError().map(thing -> (StreamingHttpResponse) thing);
}
}).appendConnectionFactoryFilter(original -> new CountingConnectionFactory(original, connectionCount)).appendClientFilter(new TimeoutHttpRequesterFilter((req, ts) -> clientTimeout, true)).build();
}
use of io.servicetalk.http.api.StreamingHttpRequester in project servicetalk by apple.
the class DefaultMultiAddressUrlHttpClientBuilderTest method buildWithProvidedServiceDiscoverer.
@Test
@SuppressWarnings("unchecked")
void buildWithProvidedServiceDiscoverer() throws Exception {
ServiceDiscoverer<HostAndPort, InetSocketAddress, ServiceDiscovererEvent<InetSocketAddress>> mockedServiceDiscoverer = mock(ServiceDiscoverer.class);
StreamingHttpRequester newRequester = HttpClients.forMultiAddressUrl(mockedServiceDiscoverer).ioExecutor(CTX.ioExecutor()).buildStreaming();
newRequester.closeAsync().toFuture().get();
verify(mockedServiceDiscoverer, never()).closeAsync();
}
use of io.servicetalk.http.api.StreamingHttpRequester in project servicetalk by apple.
the class AbstractEchoServerBasedHttpRequesterTest method makeRequestValidateResponseAndClose.
static void makeRequestValidateResponseAndClose(StreamingHttpRequester requester) throws ExecutionException, InterruptedException {
try {
StreamingHttpRequest request = requester.get("/request?foo=bar&foo=baz").payloadBody(from(DEFAULT_ALLOCATOR.fromAscii("Testing123")));
request.headers().set(HOST, "mock.servicetalk.io");
StreamingHttpResponse resp = awaitIndefinitelyNonNull(requester.request(request).retryWhen(retryWithExponentialBackoffFullJitter(10, t -> true, ofMillis(100), ofDays(10), CTX.executor())));
assertThat(resp.status(), equalTo(OK));
Single<String> respBody = resp.payloadBody().collect(StringBuilder::new, (sb, buf) -> {
sb.append(buf.toString(UTF_8));
return sb;
}).map(StringBuilder::toString);
HttpHeaders headers = resp.headers();
assertThat(headers.get("test-req-method"), hasToString(GET.toString()));
assertThat(headers.get("test-req-target"), hasToString("/request?foo=bar&foo=baz"));
assertThat(headers.get("test-req-header-host"), hasToString("mock.servicetalk.io"));
assertThat(headers.get("test-req-header-transfer-encoding"), equalTo(CHUNKED));
assertThat(respBody.toFuture().get(), equalTo("Testing123"));
} finally {
requester.closeAsync().toFuture().get();
}
}
use of io.servicetalk.http.api.StreamingHttpRequester 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