use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpClientTest method testIssue303.
@Test
public void testIssue303() {
NettyContext server = HttpServer.create(0).newHandler((req, resp) -> resp.sendString(Mono.just("OK"))).block(Duration.ofSeconds(30));
Mono<String> content = HttpClient.create(server.address().getPort()).get("/", req -> req.sendByteArray(Mono.defer(() -> Mono.just("Hello".getBytes())))).flatMap(it -> it.receive().aggregate().asString());
StepVerifier.create(content).expectNextMatches(s -> "OK".equals(s)).expectComplete().verify(Duration.ofSeconds(30));
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpClientTest method gzip.
@Test
public void gzip() {
String content = "HELLO WORLD";
NettyContext c = HttpServer.create(opts -> opts.compression(true).port(0)).newHandler((req, res) -> res.sendString(Mono.just(content))).block();
// verify gzip is negotiated (when no decoder)
StepVerifier.create(HttpClient.create(c.address().getPort()).get("/", req -> req.followRedirect().addHeader("Accept-Encoding", "gzip").addHeader("Accept-Encoding", "deflate")).flatMap(r -> r.receive().aggregate().asString().zipWith(Mono.just(r.responseHeaders().get("Content-Encoding", ""))).zipWith(Mono.just(r)))).expectNextMatches(tuple -> {
return !tuple.getT1().getT1().equals(content) && "gzip".equals(tuple.getT1().getT2());
}).expectComplete().verify();
// verify decoder does its job and removes the header
StepVerifier.create(HttpClient.create(c.address().getPort()).get("/", req -> {
req.context().addHandlerFirst("gzipDecompressor", new HttpContentDecompressor());
return req.followRedirect().addHeader("Accept-Encoding", "gzip").addHeader("Accept-Encoding", "deflate");
}).flatMap(r -> r.receive().aggregate().asString().zipWith(Mono.just(r.responseHeaders().get("Content-Encoding", ""))).zipWith(Mono.just(r)))).expectNextMatches(tuple -> {
return tuple.getT1().getT1().equals(content) && "".equals(tuple.getT1().getT2());
}).expectComplete().verify();
c.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpClientTest method abort.
@Test
public void abort() throws Exception {
NettyContext x = TcpServer.create("localhost", 0).newHandler((in, out) -> in.receive().take(1).thenMany(Flux.defer(() -> out.context(c -> c.addHandlerFirst(new HttpResponseEncoder())).sendObject(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED)).then(Mono.delay(Duration.ofSeconds(2)).then())))).block(Duration.ofSeconds(30));
PoolResources pool = PoolResources.fixed("test", 1);
HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").flatMap(r -> {
r.dispose();
return Mono.just(r.status().code());
}).log().block(Duration.ofSeconds(30));
HttpClientResponse resp = HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").log().block(Duration.ofSeconds(30));
resp.dispose();
resp = HttpClient.create(opts -> opts.host("localhost").port(x.address().getPort()).poolResources(pool)).get("/").log().block(Duration.ofSeconds(30));
resp.dispose();
x.dispose();
pool.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpRedirectTest method testIssue278.
@Test
public void testIssue278() {
NettyContext server1 = HttpServer.create(8888).newRouter(r -> r.get("/1", (req, res) -> res.sendRedirect("/3")).get("/2", (req, res) -> res.sendRedirect("http://localhost:8888/3")).get("/3", (req, res) -> res.sendString(Mono.just("OK"))).get("/4", (req, res) -> res.sendRedirect("http://localhost:8889/1"))).block(Duration.ofSeconds(30));
NettyContext server2 = HttpServer.create(8889).newRouter(r -> r.get("/1", (req, res) -> res.sendString(Mono.just("Other")))).block(Duration.ofSeconds(30));
HttpClient client = HttpClient.create(8888);
Mono<String> response = client.get("/1", req -> req.followRedirect()).flatMap(res -> res.receive().aggregate().asString());
StepVerifier.create(response).expectNextMatches(s -> "OK".equals(s)).expectComplete().verify(Duration.ofSeconds(30));
response = client.get("/2", req -> req.followRedirect()).flatMap(res -> res.receive().aggregate().asString());
StepVerifier.create(response).expectNextMatches(s -> "OK".equals(s)).expectComplete().verify(Duration.ofSeconds(30));
response = client.get("/4", req -> req.followRedirect()).flatMap(res -> res.receive().aggregate().asString());
StepVerifier.create(response).expectNextMatches(s -> "Other".equals(s)).expectComplete().verify(Duration.ofSeconds(30));
server1.dispose();
server2.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpRedirectTest method redirectTests.
private void redirectTests(String url) {
AtomicInteger counter = new AtomicInteger(1);
NettyContext server = HttpServer.create(0).newHandler((req, res) -> {
if (req.uri().contains("/login") && req.method().equals(HttpMethod.POST) && counter.getAndDecrement() > 0) {
return res.sendRedirect(url);
} else {
return res.status(200).send();
}
}).block(Duration.ofSeconds(30));
PoolResources pool = PoolResources.fixed("test", 1);
HttpClient client = HttpClient.create(ops -> ops.connectAddress(() -> server.address()).poolResources(pool));
try {
Flux.range(0, this.numberOfTests).concatMap(i -> client.post("/login", r -> r.followRedirect()).flatMap(r -> r.receive().then())).blockLast(Duration.ofSeconds(30));
} finally {
server.dispose();
}
}
Aggregations