Search in sources :

Example 6 with HttpClient

use of reactor.netty.http.client.HttpClient in project spring-boot by spring-projects.

the class NettyRSocketServerFactoryTests method createSecureHttpClient.

private HttpClient createSecureHttpClient() {
    HttpClient httpClient = createHttpClient();
    Http11SslContextSpec sslContextSpec = Http11SslContextSpec.forClient().configure((builder) -> builder.sslProvider(SslProvider.JDK).trustManager(InsecureTrustManagerFactory.INSTANCE));
    return httpClient.secure((spec) -> spec.sslContext(sslContextSpec));
}
Also used : HttpClient(reactor.netty.http.client.HttpClient) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec)

Example 7 with HttpClient

use of reactor.netty.http.client.HttpClient in project reactor-netty by reactor.

the class EchoClient method main.

public static void main(String[] args) {
    HttpClient client = HttpClient.create().port(PORT).wiretap(WIRETAP).compress(COMPRESS);
    if (SECURE) {
        Http11SslContextSpec http11SslContextSpec = Http11SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
        client = client.secure(spec -> spec.sslContext(http11SslContextSpec));
    }
    String response = client.post().uri("/echo").send(ByteBufFlux.fromString(Mono.just("echo"))).responseContent().aggregate().asString().block();
    System.out.println("Response: " + response);
}
Also used : InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) ByteBufFlux(reactor.netty.ByteBufFlux) Mono(reactor.core.publisher.Mono) HttpClient(reactor.netty.http.client.HttpClient) HttpClient(reactor.netty.http.client.HttpClient) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec)

Example 8 with HttpClient

use of reactor.netty.http.client.HttpClient in project reactor-netty by reactor.

the class HelloWorldClient method main.

public static void main(String[] args) {
    HttpClient client = HttpClient.create().port(PORT).wiretap(WIRETAP).compress(COMPRESS);
    if (SECURE) {
        Http11SslContextSpec http11SslContextSpec = Http11SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
        client = client.secure(spec -> spec.sslContext(http11SslContextSpec));
    }
    String response = client.get().uri("/hello").responseContent().aggregate().asString().block();
    System.out.println("Response: " + response);
}
Also used : InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) HttpClient(reactor.netty.http.client.HttpClient) HttpClient(reactor.netty.http.client.HttpClient) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec)

Example 9 with HttpClient

use of reactor.netty.http.client.HttpClient in project reactor-netty by reactor.

the class HttpServerTests method doTestIssue1978.

private void doTestIssue1978(HttpProtocol[] serverProtocols, HttpProtocol[] clientProtocols, @Nullable Http2SslContextSpec serverCtx, @Nullable Http2SslContextSpec clientCtx, long serverDelay, long clientDelay) throws Exception {
    int count = 5;
    CountDownLatch latch = new CountDownLatch(count);
    Mono<String> mono = Mono.just("testIssue1978");
    Mono<String> serverResponse = serverDelay == 0 ? mono : mono.delayElement(Duration.ofMillis(serverDelay));
    HttpServer mainServer = HttpServer.create().protocol(serverProtocols).httpRequestDecoder(spec -> spec.h2cMaxContentLength(8 * 1024));
    HttpServer server = serverCtx != null ? mainServer.secure(sslContextSpec -> sslContextSpec.sslContext(serverCtx)) : mainServer;
    disposableServer = server.handle((req, res) -> {
        req.withConnection(conn -> conn.channel().closeFuture().addListener(f -> latch.countDown()));
        return res.sendString(serverResponse);
    }).bindNow();
    byte[] content = new byte[1024];
    Random rndm = new Random();
    rndm.nextBytes(content);
    String strContent = new String(content, Charset.defaultCharset());
    Flux<String> flux = Flux.just(strContent, strContent, strContent, strContent);
    Flux<String> clientRequest = clientDelay == 0 ? flux : flux.delayElements(Duration.ofMillis(clientDelay));
    HttpClient mainClient = HttpClient.create().port(disposableServer.port()).protocol(clientProtocols);
    HttpClient client = clientCtx != null ? mainClient.secure(sslContextSpec -> sslContextSpec.sslContext(clientCtx)) : mainClient;
    Flux.range(0, count).flatMap(i -> client.post().uri("/").send(ByteBufFlux.fromString(clientRequest)).responseContent().aggregate().asString()).blockLast(Duration.ofSeconds(10));
    assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
}
Also used : AttributeKey(io.netty.util.AttributeKey) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) SNIHostName(javax.net.ssl.SNIHostName) AbortedException(reactor.netty.channel.AbortedException) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpMessage(io.netty.handler.codec.http.HttpMessage) ByteBufHolder(io.netty.buffer.ByteBufHolder) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) TcpClient(reactor.netty.tcp.TcpClient) ConnectionObserver(reactor.netty.ConnectionObserver) Future(java.util.concurrent.Future) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) DEFAULT_FORM_DECODER_SPEC(reactor.netty.http.server.HttpServerFormDecoderProvider.DEFAULT_FORM_DECODER_SPEC) Path(java.nio.file.Path) LoopResources(reactor.netty.resources.LoopResources) HttpObjectDecoder(io.netty.handler.codec.http.HttpObjectDecoder) HttpRequest(io.netty.handler.codec.http.HttpRequest) Context(reactor.util.context.Context) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) Assertions.fail(org.assertj.core.api.Assertions.fail) NettyOutbound(reactor.netty.NettyOutbound) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) GZIPOutputStream(java.util.zip.GZIPOutputStream) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpClient(reactor.netty.http.client.HttpClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChannelOption(io.netty.channel.ChannelOption) HttpProtocol(reactor.netty.http.HttpProtocol) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ValueSource(org.junit.jupiter.params.provider.ValueSource) PrematureCloseException(reactor.netty.http.client.PrematureCloseException) SslContext(io.netty.handler.ssl.SslContext) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Field(java.lang.reflect.Field) ReferenceCounted(io.netty.util.ReferenceCounted) Channel(io.netty.channel.Channel) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) FutureMono(reactor.netty.FutureMono) Paths(java.nio.file.Paths) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) ServerCookieEncoder(io.netty.handler.codec.http.cookie.ServerCookieEncoder) HttpUtil(io.netty.handler.codec.http.HttpUtil) SocketAddress(java.net.SocketAddress) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) TimeoutException(java.util.concurrent.TimeoutException) BaseHttpTest(reactor.netty.BaseHttpTest) Random(java.util.Random) Unpooled(io.netty.buffer.Unpooled) GlobalEventExecutor(io.netty.util.concurrent.GlobalEventExecutor) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) TransportConfig(reactor.netty.transport.TransportConfig) ServerCookieDecoder(io.netty.handler.codec.http.cookie.ServerCookieDecoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteBufFlux(reactor.netty.ByteBufFlux) NettyPipeline(reactor.netty.NettyPipeline) MethodSource(org.junit.jupiter.params.provider.MethodSource) ChannelGroup(io.netty.channel.group.ChannelGroup) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) SignalType(reactor.core.publisher.SignalType) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ChannelBindException(reactor.netty.ChannelBindException) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) DisposableServer(reactor.netty.DisposableServer) HttpVersion(io.netty.handler.codec.http.HttpVersion) Tuple3(reactor.util.function.Tuple3) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) Tuple2(reactor.util.function.Tuple2) HttpClientRequest(reactor.netty.http.client.HttpClientRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SniCompletionEvent(io.netty.handler.ssl.SniCompletionEvent) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpMethod(io.netty.handler.codec.http.HttpMethod) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) TcpServer(reactor.netty.tcp.TcpServer) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Comparator(java.util.Comparator) Timeout(org.junit.jupiter.api.Timeout) Random(java.util.Random) HttpClient(reactor.netty.http.client.HttpClient) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with HttpClient

use of reactor.netty.http.client.HttpClient in project reactor-netty by reactor.

the class HttpServerTests method testMaxKeepAliveRequests.

@ParameterizedTest(name = "{displayName}({arguments})")
@ValueSource(ints = { -1, 1, 2 })
void testMaxKeepAliveRequests(int maxKeepAliveRequests) {
    HttpServer server = createServer().handle((req, res) -> res.sendString(Mono.just("testMaxKeepAliveRequests")));
    assertThat(server.configuration().maxKeepAliveRequests()).isEqualTo(-1);
    server = server.maxKeepAliveRequests(maxKeepAliveRequests);
    assertThat(server.configuration().maxKeepAliveRequests()).isEqualTo(maxKeepAliveRequests);
    disposableServer = server.bindNow();
    ConnectionProvider provider = ConnectionProvider.create("testMaxKeepAliveRequests", 1);
    HttpClient client = createClient(provider, disposableServer.port());
    Flux.range(0, 2).flatMap(i -> client.get().uri("/").responseSingle((res, bytes) -> bytes.asString().zipWith(Mono.just(res.responseHeaders().get(HttpHeaderNames.CONNECTION, "persistent"))))).collectList().as(StepVerifier::create).expectNextMatches(l -> {
        boolean result = l.size() == 2 && "testMaxKeepAliveRequests".equals(l.get(0).getT1()) && "testMaxKeepAliveRequests".equals(l.get(1).getT1());
        if (maxKeepAliveRequests == -1) {
            return result && "persistent".equals(l.get(0).getT2()) && "persistent".equals(l.get(1).getT2());
        } else if (maxKeepAliveRequests == 1) {
            return result && "close".equals(l.get(0).getT2()) && "close".equals(l.get(1).getT2());
        } else if (maxKeepAliveRequests == 2) {
            return result && "persistent".equals(l.get(0).getT2()) && "close".equals(l.get(1).getT2());
        }
        return false;
    }).expectComplete().verify(Duration.ofSeconds(5));
    provider.disposeLater().block(Duration.ofSeconds(5));
}
Also used : AttributeKey(io.netty.util.AttributeKey) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) StepVerifier(reactor.test.StepVerifier) SNIHostName(javax.net.ssl.SNIHostName) AbortedException(reactor.netty.channel.AbortedException) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpMessage(io.netty.handler.codec.http.HttpMessage) ByteBufHolder(io.netty.buffer.ByteBufHolder) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) TcpClient(reactor.netty.tcp.TcpClient) ConnectionObserver(reactor.netty.ConnectionObserver) Future(java.util.concurrent.Future) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) DEFAULT_FORM_DECODER_SPEC(reactor.netty.http.server.HttpServerFormDecoderProvider.DEFAULT_FORM_DECODER_SPEC) Path(java.nio.file.Path) LoopResources(reactor.netty.resources.LoopResources) HttpObjectDecoder(io.netty.handler.codec.http.HttpObjectDecoder) HttpRequest(io.netty.handler.codec.http.HttpRequest) Context(reactor.util.context.Context) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) Assertions.fail(org.assertj.core.api.Assertions.fail) NettyOutbound(reactor.netty.NettyOutbound) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) GZIPOutputStream(java.util.zip.GZIPOutputStream) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpClient(reactor.netty.http.client.HttpClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChannelOption(io.netty.channel.ChannelOption) HttpProtocol(reactor.netty.http.HttpProtocol) Nullable(reactor.util.annotation.Nullable) ArrayList(java.util.ArrayList) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ValueSource(org.junit.jupiter.params.provider.ValueSource) PrematureCloseException(reactor.netty.http.client.PrematureCloseException) SslContext(io.netty.handler.ssl.SslContext) Publisher(org.reactivestreams.Publisher) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Field(java.lang.reflect.Field) ReferenceCounted(io.netty.util.ReferenceCounted) Channel(io.netty.channel.Channel) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultEventExecutor(io.netty.util.concurrent.DefaultEventExecutor) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) FutureMono(reactor.netty.FutureMono) Paths(java.nio.file.Paths) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) ServerCookieEncoder(io.netty.handler.codec.http.cookie.ServerCookieEncoder) HttpUtil(io.netty.handler.codec.http.HttpUtil) SocketAddress(java.net.SocketAddress) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) TimeoutException(java.util.concurrent.TimeoutException) BaseHttpTest(reactor.netty.BaseHttpTest) Random(java.util.Random) Unpooled(io.netty.buffer.Unpooled) GlobalEventExecutor(io.netty.util.concurrent.GlobalEventExecutor) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) TransportConfig(reactor.netty.transport.TransportConfig) ServerCookieDecoder(io.netty.handler.codec.http.cookie.ServerCookieDecoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteBufFlux(reactor.netty.ByteBufFlux) NettyPipeline(reactor.netty.NettyPipeline) MethodSource(org.junit.jupiter.params.provider.MethodSource) ChannelGroup(io.netty.channel.group.ChannelGroup) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) SignalType(reactor.core.publisher.SignalType) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ChannelBindException(reactor.netty.ChannelBindException) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) DisposableServer(reactor.netty.DisposableServer) HttpVersion(io.netty.handler.codec.http.HttpVersion) Tuple3(reactor.util.function.Tuple3) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketCloseStatus(io.netty.handler.codec.http.websocketx.WebSocketCloseStatus) Tuple2(reactor.util.function.Tuple2) HttpClientRequest(reactor.netty.http.client.HttpClientRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SniCompletionEvent(io.netty.handler.ssl.SniCompletionEvent) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Connection(reactor.netty.Connection) ExecutorService(java.util.concurrent.ExecutorService) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpMethod(io.netty.handler.codec.http.HttpMethod) CertificateException(java.security.cert.CertificateException) TimeUnit(java.util.concurrent.TimeUnit) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) TcpServer(reactor.netty.tcp.TcpServer) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Comparator(java.util.Comparator) Timeout(org.junit.jupiter.api.Timeout) HttpClient(reactor.netty.http.client.HttpClient) ConnectionProvider(reactor.netty.resources.ConnectionProvider) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpClient (reactor.netty.http.client.HttpClient)98 BaseHttpTest (reactor.netty.BaseHttpTest)53 Mono (reactor.core.publisher.Mono)50 Test (org.junit.jupiter.api.Test)49 Duration (java.time.Duration)47 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)45 StepVerifier (reactor.test.StepVerifier)45 Flux (reactor.core.publisher.Flux)44 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)40 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)40 AtomicReference (java.util.concurrent.atomic.AtomicReference)38 HttpServer (reactor.netty.http.server.HttpServer)36 TimeUnit (java.util.concurrent.TimeUnit)35 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)34 ConnectionProvider (reactor.netty.resources.ConnectionProvider)33 CountDownLatch (java.util.concurrent.CountDownLatch)32 ByteBufFlux (reactor.netty.ByteBufFlux)32 ByteBuf (io.netty.buffer.ByteBuf)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)30 MethodSource (org.junit.jupiter.params.provider.MethodSource)30