use of reactor.ipc.netty.NettyOutbound in project reactor-netty by reactor.
the class HttpServerTests method assertSendFile.
private void assertSendFile(Function<HttpServerResponse, NettyOutbound> fn) {
NettyContext context = HttpServer.create(opt -> opt.host("localhost")).newHandler((req, resp) -> fn.apply(resp)).block();
HttpClientResponse response = HttpClient.create(opt -> opt.connectAddress(() -> context.address())).get("/foo").block(Duration.ofSeconds(120));
context.dispose();
context.onClose().block();
String body = response.receive().aggregate().asString(StandardCharsets.UTF_8).block();
assertThat(body).startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here -><- 1024 mark here").endsWith("End of File");
}
use of reactor.ipc.netty.NettyOutbound in project reactor-netty by reactor.
the class TcpServerTests method exposesNettyPipelineConfiguration.
@Test
public void exposesNettyPipelineConfiguration() throws InterruptedException {
final int port = SocketUtils.findAvailableTcpPort();
final CountDownLatch latch = new CountDownLatch(2);
final TcpClient client = TcpClient.create(port);
BiFunction<? super NettyInbound, ? super NettyOutbound, ? extends Publisher<Void>> serverHandler = (in, out) -> {
in.receive().asString().subscribe(data -> {
log.info("data " + data + " on " + in);
latch.countDown();
});
return Flux.never();
};
TcpServer server = TcpServer.create(opts -> opts.afterChannelInit(c -> c.pipeline().addBefore(NettyPipeline.ReactiveBridge, "codec", new LineBasedFrameDecoder(8 * 1024))).port(port));
NettyContext connected = server.newHandler(serverHandler).block(Duration.ofSeconds(30));
NettyContext clientContext = client.newHandler((in, out) -> out.send(Flux.just("Hello World!\n", "Hello 11!\n").map(b -> out.alloc().buffer().writeBytes(b.getBytes())))).block(Duration.ofSeconds(30));
assertTrue("Latch was counted down", latch.await(10, TimeUnit.SECONDS));
connected.dispose();
clientContext.dispose();
}
use of reactor.ipc.netty.NettyOutbound in project reactor-netty by reactor.
the class TcpServerTests method assertSendFile.
private void assertSendFile(Function<NettyOutbound, NettyOutbound> fn) throws InterruptedException {
NettyContext context = TcpServer.create().newHandler((in, out) -> in.receive().asString().flatMap(word -> "GOGOGO".equals(word) ? fn.apply(out).then() : out.sendString(Mono.just("NOPE")))).block();
MonoProcessor<String> m1 = MonoProcessor.create();
MonoProcessor<String> m2 = MonoProcessor.create();
NettyContext client1 = TcpClient.create(opt -> opt.port(context.address().getPort())).newHandler((in, out) -> {
in.receive().asString().log("-----------------CLIENT1").subscribe(m1::onNext);
return out.sendString(Mono.just("gogogo")).neverComplete();
}).block();
NettyContext client2 = TcpClient.create(opt -> opt.port(context.address().getPort())).newHandler((in, out) -> {
in.receive().asString(StandardCharsets.UTF_8).take(2).reduceWith(String::new, String::concat).log("-----------------CLIENT2").subscribe(m2::onNext);
return out.sendString(Mono.just("GOGOGO")).neverComplete();
}).block();
String client1Response = m1.block();
String client2Response = m2.block();
client1.dispose();
client1.onClose().block();
client2.dispose();
client2.onClose().block();
context.dispose();
context.onClose().block();
Assertions.assertThat(client1Response).isEqualTo("NOPE");
Assertions.assertThat(client2Response).startsWith("This is an UTF-8 file that is larger than 1024 bytes. " + "It contains accents like é.").contains("1024 mark here ->").contains("<- 1024 mark here").endsWith("End of File");
}
use of reactor.ipc.netty.NettyOutbound in project cf-java-client by cloudfoundry.
the class MultipartHttpClientRequest method done.
public Mono<Void> done() {
AsciiString boundary = generateMultipartBoundary();
AsciiString delimiter = getDelimiter(boundary);
AsciiString closeDelimiter = getCloseDelimiter(boundary);
List<PartHttpClientRequest> parts = this.partConsumers.stream().map(partConsumer -> {
PartHttpClientRequest part = new PartHttpClientRequest(this.objectMapper);
partConsumer.accept(part);
return part;
}).collect(Collectors.toList());
Long contentLength = parts.stream().mapToLong(part -> delimiter.length() + CRLF.length() + part.getLength()).sum() + closeDelimiter.length();
NettyOutbound intermediateRequest = this.request.chunkedTransfer(false).header(CONTENT_TYPE, BOUNDARY_PREAMBLE.concat(boundary)).header(CONTENT_LENGTH, String.valueOf(contentLength));
for (PartHttpClientRequest part : parts) {
intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(delimiter.toByteArray()));
intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(CRLF.toByteArray()));
intermediateRequest = intermediateRequest.sendObject(part.renderedHeaders);
intermediateRequest = part.sendPayload(intermediateRequest);
}
intermediateRequest = intermediateRequest.sendObject(Unpooled.wrappedBuffer(closeDelimiter.toByteArray()));
return intermediateRequest.then();
}
Aggregations