use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.
the class HttpCompressionClientServerTests method serverCompressionEnabledBigResponse.
@Test
public void serverCompressionEnabledBigResponse() throws Exception {
HttpServer server = HttpServer.create(o -> o.port(0).compression(4));
NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
// don't activate compression on the client options to avoid auto-handling (which removes the header)
HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
HttpClientResponse resp = // edit the header manually to attempt to trigger compression on server side
client.get("/test", req -> req.header("accept-encoding", "gzip")).block();
assertThat(resp.responseHeaders().get("content-encoding")).isEqualTo("gzip");
byte[] replyBuffer = resp.receive().aggregate().asByteArray().block();
assertThat(new String(replyBuffer)).isNotEqualTo("reply");
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(replyBuffer));
byte[] deflatedBuf = new byte[1024];
int readable = gis.read(deflatedBuf);
gis.close();
assertThat(readable).isGreaterThan(0);
String deflated = new String(deflatedBuf, 0, readable);
assertThat(deflated).isEqualTo("reply");
nettyContext.dispose();
nettyContext.onClose().block();
}
use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.
the class HttpCompressionClientServerTests method serverCompressionEnabledSmallResponse.
@Test
public void serverCompressionEnabledSmallResponse() throws Exception {
HttpServer server = HttpServer.create(o -> o.port(0).compression(25));
NettyContext nettyContext = server.newHandler((in, out) -> out.header("content-length", "5").sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
// don't activate compression on the client options to avoid auto-handling (which removes the header)
HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
HttpClientResponse resp = // edit the header manually to attempt to trigger compression on server side
client.get("/test", req -> req.header("Accept-Encoding", "gzip")).block();
// check the server didn't send the gzip header, only transfer-encoding
HttpHeaders headers = resp.responseHeaders();
assertThat(headers.get("conTENT-encoding")).isNull();
// check the server sent plain text
String reply = resp.receive().asString().blockFirst();
Assert.assertEquals("reply", reply);
resp.dispose();
nettyContext.dispose();
nettyContext.onClose().block();
}
use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.
the class HttpCompressionClientServerTests method serverCompressionPredicateTrue.
@Test
public void serverCompressionPredicateTrue() throws Exception {
HttpServer server = HttpServer.create(o -> o.port(0).compression((req, res) -> true));
NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
// don't activate compression on the client options to avoid auto-handling (which removes the header)
HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
HttpClientResponse resp = // edit the header manually to attempt to trigger compression on server side
client.get("/test", req -> req.header("Accept-Encoding", "gzip")).block();
assertThat(resp.responseHeaders().get("content-encoding")).isEqualTo("gzip");
byte[] replyBuffer = resp.receive().aggregate().asByteArray().block();
assertThat(new String(replyBuffer)).isNotEqualTo("reply");
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(replyBuffer));
byte[] deflatedBuf = new byte[1024];
int readable = gis.read(deflatedBuf);
gis.close();
assertThat(readable).isGreaterThan(0);
String deflated = new String(deflatedBuf, 0, readable);
assertThat(deflated).isEqualTo("reply");
nettyContext.dispose();
nettyContext.onClose().block();
}
use of reactor.ipc.netty.http.client.HttpClientResponse in project reactor-netty by reactor.
the class HttpCompressionClientServerTests method serverCompressionDisabled.
@Test
public void serverCompressionDisabled() throws Exception {
HttpServer server = HttpServer.create(o -> o.port(0).compression(false));
NettyContext nettyContext = server.newHandler((in, out) -> out.sendString(Mono.just("reply"))).block(Duration.ofMillis(10_000));
// don't activate compression on the client options to avoid auto-handling (which removes the header)
HttpClient client = HttpClient.create(o -> o.connectAddress(() -> address(nettyContext)));
HttpClientResponse resp = // edit the header manually to attempt to trigger compression on server side
client.get("/test", req -> req.header("Accept-Encoding", "gzip")).block();
assertThat(resp.responseHeaders().get("content-encoding")).isNull();
String reply = resp.receive().asString().blockFirst();
Assert.assertEquals("reply", reply);
resp.dispose();
nettyContext.dispose();
nettyContext.onClose().block();
}
use of reactor.ipc.netty.http.client.HttpClientResponse in project java-buildpack-dependency-builder by cloudfoundry.
the class NetworkLogging method response.
public static Function<Mono<HttpClientResponse>, Mono<HttpClientResponse>> response(String uri) {
if (!LOGGER.isInfoEnabled()) {
return inbound -> inbound;
}
AtomicLong startTimeHolder = new AtomicLong();
AtomicReference<HttpClientResponse> responseHolder = new AtomicReference<>();
return inbound -> inbound.doOnSubscribe(s -> startTimeHolder.set(System.currentTimeMillis())).doOnNext(responseHolder::set).doFinally(signalType -> {
String elapsed = asTime(System.currentTimeMillis() - startTimeHolder.get());
HttpClientResponse response = responseHolder.get();
if (response == null) {
return;
}
LOGGER.info("{} {} ({})", response.status().code(), uri, elapsed);
});
}
Aggregations