use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class RedirectingClientAndConnectionFilterTest method redirectFilterWithHostHeaderAbsoluteLocation.
@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterWithHostHeaderAbsoluteLocation(final RequesterType type, final SecurityType security) throws Exception {
setUp(security);
BlockingHttpRequester client = asBlockingRequester(createFilter(type, (responseFactory, request) -> {
if (request.requestTarget().equals("/")) {
return succeeded(responseFactory.permanentRedirect().addHeader(LOCATION, "http://servicetalk.io:80/next"));
}
return succeeded(responseFactory.ok());
}, newFilterFactory()));
HttpRequest request = client.get("/").addHeader(HOST, "servicetalk.io:80");
HttpResponse response = client.request(request);
assertThat(response.status(), equalTo(PERMANENT_REDIRECT));
response = client.request(request.addHeader("X-REDIRECT", "TRUE"));
assertThat(response.status(), equalTo(OK));
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class RedirectingClientAndConnectionFilterTest method redirectFilterWithHostHeaderRelativeLocation.
@ParameterizedTest(name = "{displayName} [{index}] {0}-{1}")
@MethodSource("requesterTypes")
void redirectFilterWithHostHeaderRelativeLocation(final RequesterType type, final SecurityType security) throws Exception {
setUp(security);
BlockingHttpRequester client = asBlockingRequester(createFilter(type, (responseFactory, request) -> {
if (request.requestTarget().equals("/")) {
return succeeded(responseFactory.permanentRedirect().addHeader(LOCATION, "/next"));
}
return succeeded(responseFactory.ok());
}, newFilterFactory()));
HttpRequest request = client.get("/").addHeader(HOST, "servicetalk.io");
HttpResponse response = client.request(request);
assertThat(response.status(), equalTo(PERMANENT_REDIRECT));
response = client.request(request.addHeader("X-REDIRECT", "TRUE"));
assertThat(response.status(), equalTo(OK));
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class RequestResponseContextTest method testBlockingAggregated.
@Test
void testBlockingAggregated() throws Exception {
setUp(Api.BlockingAggregated);
String requestContextValue = randomString(10);
BlockingHttpClient client = streamingHttpClient().asBlockingClient();
HttpRequest request = client.get(SVC_ECHO);
request.context().put(CLIENT_REQUEST_KEY, singletonList(requestContextValue));
HttpResponse response = client.request(request);
assertResponse(response.toStreamingResponse(), HTTP_1_1, OK, 0);
assertContext(requestContextValue, request, response);
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class IoUringTest method ioUringIsAvailableOnLinux.
@Test
@EnabledOnOs(value = { LINUX })
void ioUringIsAvailableOnLinux() throws Exception {
EventLoopAwareNettyIoExecutor ioUringExecutor = null;
try {
IoUringUtils.tryIoUring(true);
assertTrue(IoUringUtils.isAvailable());
IOUring.ensureAvailability();
ioUringExecutor = NettyIoExecutors.createIoExecutor(2, "io-uring");
assertThat(ioUringExecutor.eventLoopGroup(), is(instanceOf(IOUringEventLoopGroup.class)));
try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).ioExecutor(ioUringExecutor).listenStreamingAndAwait(new TestServiceStreaming());
BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).ioExecutor(ioUringExecutor).buildBlocking()) {
HttpRequest request = client.post(SVC_ECHO).payloadBody("bonjour!", textSerializerUtf8());
HttpResponse response = client.request(request);
assertThat(response.status(), is(OK));
assertThat(response.payloadBody(textSerializerUtf8()), is("bonjour!"));
}
} finally {
IoUringUtils.tryIoUring(false);
if (ioUringExecutor != null) {
ioUringExecutor.closeAsync().toFuture().get();
assertTrue(ioUringExecutor.eventLoopGroup().isShutdown());
}
}
}
use of io.servicetalk.http.api.HttpRequest in project servicetalk by apple.
the class ManualRedirectClient method main.
public static void main(String... args) throws Exception {
try (HttpClient secureClient = HttpClients.forSingleAddress("localhost", SECURE_SERVER_PORT).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).build()).build()) {
try (HttpClient client = HttpClients.forSingleAddress("localhost", NON_SECURE_SERVER_PORT).build()) {
System.out.println("- Redirect of a GET request with a custom header:");
HttpRequest originalGet = client.get("/non-relative").addHeader(CUSTOM_HEADER, "value");
client.request(originalGet).flatMap(response -> {
if (response.status().statusClass() == REDIRECTION_3XX) {
CharSequence location = response.headers().get(LOCATION);
HttpClient redirectClient = lookupClient(location, client, secureClient);
return redirectClient.request(redirectClient.newRequest(originalGet.method(), location.toString()).addHeader(CUSTOM_HEADER, originalGet.headers().get(CUSTOM_HEADER)));
}
// Decided not to follow redirect, return the original response or an error:
return succeeded(response);
}).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerAscii()));
System.out.println();
}).toFuture().get();
System.out.println("- Redirect of a POST request with a payload body:");
HttpRequest originalPost = client.post("/non-relative").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"));
client.request(originalPost).flatMap(response -> {
if (response.status().statusClass() == REDIRECTION_3XX) {
CharSequence location = response.headers().get(LOCATION);
HttpClient redirectClient = lookupClient(location, client, secureClient);
return redirectClient.request(redirectClient.newRequest(originalPost.method(), location.toString()).payloadBody(originalPost.payloadBody()));
}
// Decided not to follow redirect, return the original response or an error:
return succeeded(response);
}).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerAscii()));
}).toFuture().get();
}
}
}
Aggregations