Search in sources :

Example 86 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpOperationsTest method httpAndJsonDecoders.

@Test
public void httpAndJsonDecoders() {
    EmbeddedChannel channel = new EmbeddedChannel();
    NettyContext testContext = () -> channel;
    ChannelHandler handler = new JsonObjectDecoder(true);
    testContext.addHandlerLast("foo", handler);
    HttpOperations.autoAddHttpExtractor(testContext, "foo", handler);
    String json1 = "[{\"some\": 1} , {\"valu";
    String json2 = "e\": true, \"test\": 1}]";
    Object[] content = new Object[3];
    content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    content[1] = new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8));
    content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2, CharsetUtil.UTF_8));
    channel.writeInbound(content);
    Object t = channel.readInbound();
    assertThat(t, instanceOf(HttpResponse.class));
    assertThat(t, not(instanceOf(HttpContent.class)));
    t = channel.readInbound();
    assertThat(t, instanceOf(ByteBuf.class));
    assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"some\": 1}"));
    ((ByteBuf) t).release();
    t = channel.readInbound();
    assertThat(t, instanceOf(ByteBuf.class));
    assertThat(((ByteBuf) t).toString(CharsetUtil.UTF_8), is("{\"value\": true, \"test\": 1}"));
    ((ByteBuf) t).release();
    t = channel.readInbound();
    assertThat(t, is(LastHttpContent.EMPTY_LAST_CONTENT));
    ((LastHttpContent) t).release();
    t = channel.readInbound();
    assertThat(t, nullValue());
}
Also used : DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) JsonObjectDecoder(io.netty.handler.codec.json.JsonObjectDecoder) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) NettyContext(reactor.ipc.netty.NettyContext) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) Test(org.junit.Test)

Example 87 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpResponseStatusCodesHandlingTests method httpStatusCode404IsHandledByTheClient.

@Test
public void httpStatusCode404IsHandledByTheClient() {
    NettyContext server = HttpServer.create(0).newRouter(r -> r.post("/test", (req, res) -> res.send(req.receive().log("server-received")))).block(Duration.ofSeconds(30));
    HttpClient client = HttpClient.create("localhost", server.address().getPort());
    List<String> replyReceived = new ArrayList<>();
    Mono<String> content = client.get("/unsupportedURI", req -> req.addHeader("Content-Type", "text/plain").sendString(Flux.just("Hello").log("client-send"))).flatMapMany(res -> res.receive().asString().log("client-received").doOnNext(s -> replyReceived.add(s))).next().doOnError(t -> System.err.println("Failed requesting server: " + t.getMessage()));
    StepVerifier.create(content).expectErrorMatches(t -> t.getMessage().equals("HTTP request failed with code: 404.\nFailing URI: " + "/unsupportedURI")).verify(Duration.ofSeconds(30));
    Assertions.assertThat(replyReceived).isEmpty();
    server.dispose();
}
Also used : Flux(reactor.core.publisher.Flux) List(java.util.List) StepVerifier(reactor.test.StepVerifier) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ArrayList(java.util.ArrayList) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpClient(reactor.ipc.netty.http.client.HttpClient) ArrayList(java.util.ArrayList) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 88 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpTests method test100Continue.

@Test
public void test100Continue() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    NettyContext server = HttpServer.create(0).newHandler((req, res) -> req.receive().aggregate().asString().flatMap(s -> {
        latch.countDown();
        return res.sendString(Mono.just(s)).then();
    })).block(Duration.ofSeconds(30));
    String content = HttpClient.create(server.address().getPort()).post("/", req -> req.header("Expect", "100-continue").sendString(Flux.just("1", "2", "3", "4", "5"))).flatMap(res -> res.receive().aggregate().asString()).block();
    System.out.println(content);
    Assertions.assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) CountDownLatch(java.util.concurrent.CountDownLatch) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 89 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpTests method streamAndPoolDefaultCompression.

@Test
public void streamAndPoolDefaultCompression() throws Exception {
    EmitterProcessor<String> ep = EmitterProcessor.create();
    NettyContext server = HttpServer.create(opts -> opts.port(0).compression(true)).newRouter(r -> r.post("/hi", (req, res) -> req.receive().aggregate().asString().log().then(res.compression(false).sendString(Flux.just("test")).then())).get("/stream", (req, res) -> req.receive().then(res.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();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 90 with NettyContext

use of reactor.ipc.netty.NettyContext in project reactor-netty by reactor.

the class HttpTests method webSocketRespondsToRequestsFromClients.

@Test
public void webSocketRespondsToRequestsFromClients() {
    AtomicInteger clientRes = new AtomicInteger();
    AtomicInteger serverRes = new AtomicInteger();
    NettyContext server = HttpServer.create(0).newRouter(r -> r.get("/test/{param}", (req, res) -> {
        System.out.println(req.requestHeaders().get("test"));
        return res.header("content-type", "text/plain").sendWebsocket((in, out) -> out.options(c -> c.flushOnEach()).sendString(in.receive().asString().publishOn(Schedulers.single()).doOnNext(s -> serverRes.incrementAndGet()).map(it -> it + ' ' + req.param("param") + '!').log("server-reply")));
    })).block(Duration.ofSeconds(5));
    HttpClient client = HttpClient.create("localhost", server.address().getPort());
    Mono<List<String>> response = client.get("/test/World", req -> req.header("Content-Type", "text/plain").header("test", "test").options(c -> c.flushOnEach()).sendWebsocket().sendString(Flux.range(1, 1000).log("client-send").map(i -> "" + i))).flatMapMany(res -> res.receive().asString().log("client-received").publishOn(Schedulers.parallel()).doOnNext(s -> clientRes.incrementAndGet())).take(1000).collectList().cache().doOnError(i -> System.err.println("Failed requesting server: " + i));
    System.out.println("STARTING: server[" + serverRes.get() + "] / client[" + clientRes.get() + "]");
    StepVerifier.create(response).expectNextMatches(list -> "1000 World!".equals(list.get(999))).expectComplete().verify(Duration.ofSeconds(30));
    System.out.println("FINISHED: server[" + serverRes.get() + "] / client[" + clientRes + "]");
    server.dispose();
}
Also used : StepVerifier(reactor.test.StepVerifier) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Unpooled(io.netty.buffer.Unpooled) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) EmitterProcessor(reactor.core.publisher.EmitterProcessor) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) HttpClientException(reactor.ipc.netty.http.client.HttpClientException) HttpServer(reactor.ipc.netty.http.server.HttpServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClient(reactor.ipc.netty.http.client.HttpClient) List(java.util.List) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Aggregations

NettyContext (reactor.ipc.netty.NettyContext)92 Test (org.junit.Test)87 Mono (reactor.core.publisher.Mono)86 Duration (java.time.Duration)83 Flux (reactor.core.publisher.Flux)78 InetSocketAddress (java.net.InetSocketAddress)65 HttpServer (reactor.ipc.netty.http.server.HttpServer)63 HttpClient (reactor.ipc.netty.http.client.HttpClient)61 CountDownLatch (java.util.concurrent.CountDownLatch)60 StepVerifier (reactor.test.StepVerifier)60 AtomicReference (java.util.concurrent.atomic.AtomicReference)56 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)53 Unpooled (io.netty.buffer.Unpooled)50 TimeUnit (java.util.concurrent.TimeUnit)44 StandardCharsets (java.nio.charset.StandardCharsets)43 Ignore (org.junit.Ignore)43 SslContext (io.netty.handler.ssl.SslContext)42 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)42 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)42 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)42