use of reactor.ipc.netty.NettyContext 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.NettyContext 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.NettyContext in project reactor-netty by reactor.
the class HttpCookieHandlingTests method clientWithoutCookieGetsANewOneFromServer.
@Test
public void clientWithoutCookieGetsANewOneFromServer() {
NettyContext server = HttpServer.create(0).newRouter(r -> r.get("/test", (req, resp) -> resp.addCookie(new DefaultCookie("cookie1", "test_value")).send(req.receive().log("server received")))).block(Duration.ofSeconds(30));
Mono<Map<CharSequence, Set<Cookie>>> cookieResponse = HttpClient.create("localhost", server.address().getPort()).get("/test").flatMap(res -> Mono.just(res.cookies())).doOnSuccess(m -> System.out.println(m)).doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
StepVerifier.create(cookieResponse).expectNextMatches(l -> {
Set<Cookie> cookies = l.get("cookie1");
return cookies.stream().filter(e -> e.value().equals("test_value")).findFirst().isPresent();
}).expectComplete().verify(Duration.ofSeconds(30));
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpTests method streamAndPoolExplicitCompression.
@Test
public void streamAndPoolExplicitCompression() throws Exception {
EmitterProcessor<String> ep = EmitterProcessor.create();
NettyContext server = HttpServer.create(opts -> opts.port(0)).newRouter(r -> r.post("/hi", (req, res) -> req.receive().aggregate().asString().log().then(res.sendString(Flux.just("test")).then())).get("/stream", (req, res) -> req.receive().then(res.compression(true).options(op -> op.flushOnEach()).sendString(ep.log()).then()))).block(Duration.ofSeconds(30));
String content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
Flux<String> f = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).get("/stream").flatMapMany(res -> res.receive().asString());
System.out.println(content);
StepVerifier.create(f).then(() -> ep.onNext("test1")).expectNext("test1").thenAwait(Duration.ofMillis(300)).then(() -> ep.onNext("test2")).thenAwait(Duration.ofMillis(300)).expectNext("test2").thenAwait(Duration.ofMillis(300)).then(() -> ep.onComplete()).verifyComplete();
content = HttpClient.create(opts -> opts.port(server.address().getPort()).compression(true)).post("/hi", req -> req.sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString().log()).block();
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpTests method httpRespondsEmpty.
@Test
public void httpRespondsEmpty() {
NettyContext server = HttpServer.create(0).newRouter(r -> r.post("/test/{param}", (req, res) -> Mono.empty())).block(Duration.ofSeconds(30));
HttpClient client = HttpClient.create("localhost", server.address().getPort());
Mono<ByteBuf> content = client.post("/test/World", req -> req.header("Content-Type", "text/plain").sendString(Mono.just("Hello").log("client-send"))).flatMap(res -> res.receive().log("client-received").next()).doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
StepVerifier.create(content).expectComplete().verify(Duration.ofSeconds(5000));
server.dispose();
}
Aggregations