use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpClientWSOperationsTest method failOnClientServerError.
private void failOnClientServerError(boolean clientError, boolean serverError, int serverStatus, String serverSubprotocol, String clientSubprotocol) {
NettyContext httpServer = HttpServer.create(0).newRouter(routes -> routes.post("/login", (req, res) -> res.status(serverStatus).sendHeaders()).get("/ws", (req, res) -> {
int token = Integer.parseInt(req.requestHeaders().get("Authorization"));
if (token >= 400) {
return res.status(token).send();
}
return res.sendWebsocket(serverSubprotocol, (i, o) -> o.sendString(Mono.just("test")));
})).block(Duration.ofSeconds(30));
Mono<HttpClientResponse> response = HttpClient.create(httpServer.address().getPort()).get("/ws", request -> Mono.just(request.failOnClientError(clientError).failOnServerError(serverError)).transform(req -> doLoginFirst(req, httpServer.address().getPort())).flatMapMany(req -> ws(req, clientSubprotocol))).switchIfEmpty(Mono.error(new Exception()));
if (clientError || serverError) {
StepVerifier.create(response).expectError().verify(Duration.ofSeconds(30));
} else {
StepVerifier.create(response).assertNext(res -> {
assertThat(res.status().code()).isEqualTo(serverStatus == 200 ? 101 : serverStatus);
res.dispose();
}).expectComplete().verify(Duration.ofSeconds(30));
}
httpServer.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class HttpRedirectTest method testIssue253.
@Test
public void testIssue253() {
NettyContext server = HttpServer.create(9991).newRouter(r -> r.get("/1", (req, res) -> res.sendRedirect("http://localhost:9991/3")).get("/2", (req, res) -> res.status(301).header(HttpHeaderNames.LOCATION, "http://localhost:9991/3").send()).get("/3", (req, res) -> res.status(200).sendString(Mono.just("OK")))).block(Duration.ofSeconds(30));
HttpClient client = HttpClient.create(ops -> ops.connectAddress(() -> server.address()));
String value = client.get("/1", req -> req.followRedirect().send()).flatMap(res -> res.receive().aggregate().asString()).block(Duration.ofSeconds(30));
Assertions.assertThat(value).isEqualTo("OK");
value = client.get("/1").flatMap(res -> res.receive().aggregate().asString()).block(Duration.ofSeconds(30));
Assertions.assertThat(value).isNull();
value = client.get("/2", req -> req.followRedirect().send()).flatMap(res -> res.receive().aggregate().asString()).block(Duration.ofSeconds(30));
Assertions.assertThat(value).isEqualTo("OK");
value = client.get("/2").flatMap(res -> res.receive().aggregate().asString()).block(Duration.ofSeconds(30));
Assertions.assertThat(value).isNull();
server.dispose();
}
use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.
the class WebsocketTest method closePool.
@Test
public void closePool() {
PoolResources pr = PoolResources.fixed("wstest", 1);
NettyContext httpServer = HttpServer.create(0).newHandler((in, out) -> out.sendWebsocket((i, o) -> o.options(opt -> opt.flushOnEach()).sendString(Mono.just("test").delayElement(Duration.ofMillis(100)).repeat()))).block(Duration.ofSeconds(30));
Flux<String> ws = HttpClient.create(opts -> opts.port(httpServer.address().getPort()).poolResources(pr)).ws("/").flatMapMany(in -> in.receiveWebsocket().aggregateFrames().receive().asString());
StepVerifier.create(Flux.range(1, 10).concatMap(i -> ws.take(2).log())).expectNextSequence(Flux.range(1, 20).map(v -> "test").toIterable()).expectComplete().verify();
httpServer.dispose();
pr.dispose();
}
use of reactor.ipc.netty.NettyContext 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.NettyContext 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();
}
Aggregations