use of reactor.ipc.netty.http.server.HttpServer 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.http.server.HttpServer in project reactor-netty by reactor.
the class ProxyClientIssue method startContentServer.
@Test
@Ignore
public void startContentServer() throws Exception {
Random random = new Random(0);
byte[] content = new byte[1024 * 10];
random.nextBytes(content);
HttpServer server = HttpServer.create(options -> options.host("0.0.0.0").port(CONTENT_SERVER_PORT).option(ChannelOption.SO_LINGER, -1));
server.startRouterAndAwait(routes -> routes.get("/**", (req, res) -> res.header("Content-length", String.valueOf(content.length)).header("Content-type", "application/octet-stream").header("Connection", "Close").sendByteArray(Flux.just(content))));
}
use of reactor.ipc.netty.http.server.HttpServer 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.http.server.HttpServer in project reactor-netty by reactor.
the class WebsocketTest method sendToWebsocketSubprotocol.
@Test
public void sendToWebsocketSubprotocol() throws InterruptedException {
AtomicReference<String> serverSelectedProtocol = new AtomicReference<>();
AtomicReference<String> clientSelectedProtocol = new AtomicReference<>();
AtomicReference<String> clientSelectedProtocolWhenSimplyUpgrading = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
httpServer = HttpServer.create(0).newHandler((in, out) -> out.sendWebsocket("not,proto1", (i, o) -> {
serverSelectedProtocol.set(i.selectedSubprotocol());
latch.countDown();
return i.receive().asString().doOnNext(System.err::println).then();
})).block(Duration.ofSeconds(30));
HttpClient.create(httpServer.address().getPort()).ws("/test", "proto1,proto2").flatMap(in -> {
clientSelectedProtocolWhenSimplyUpgrading.set(in.receiveWebsocket().selectedSubprotocol());
return in.receiveWebsocket((i, o) -> {
clientSelectedProtocol.set(o.selectedSubprotocol());
return o.sendString(Mono.just("HELLO" + o.selectedSubprotocol()));
});
}).block(Duration.ofSeconds(30));
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
Assert.assertThat(serverSelectedProtocol.get(), is("proto1"));
Assert.assertThat(clientSelectedProtocol.get(), is("proto1"));
Assert.assertThat(clientSelectedProtocolWhenSimplyUpgrading.get(), is("proto1"));
}
use of reactor.ipc.netty.http.server.HttpServer 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();
}
Aggregations