Search in sources :

Example 1 with HttpServerConfig

use of reactor.netty.http.server.HttpServerConfig in project reactor-netty by reactor.

the class HttpProtocolsTests method data.

static Object[][] data(boolean onlyCompatible, boolean disablePool, boolean useCustomPool) throws Exception {
    SelfSignedCertificate cert = new SelfSignedCertificate();
    Http11SslContextSpec serverCtxHttp11 = Http11SslContextSpec.forServer(cert.certificate(), cert.privateKey());
    Http11SslContextSpec clientCtxHttp11 = Http11SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
    Http2SslContextSpec serverCtxHttp2 = Http2SslContextSpec.forServer(cert.certificate(), cert.privateKey());
    Http2SslContextSpec clientCtxHttp2 = Http2SslContextSpec.forClient().configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
    HttpServer _server = createServer().httpRequestDecoder(spec -> spec.h2cMaxContentLength(256));
    HttpServer[] servers = new HttpServer[] { // by default protocol is HTTP/1.1
    _server, _server.protocol(HttpProtocol.H2C), _server.protocol(HttpProtocol.HTTP11, HttpProtocol.H2C), // by default protocol is HTTP/1.1
    _server.secure(spec -> spec.sslContext(serverCtxHttp11)), _server.secure(spec -> spec.sslContext(serverCtxHttp2)).protocol(HttpProtocol.H2), _server.secure(spec -> spec.sslContext(serverCtxHttp2)).protocol(HttpProtocol.HTTP11, HttpProtocol.H2) };
    HttpClient _client;
    if (disablePool) {
        _client = HttpClient.newConnection();
    } else if (useCustomPool) {
        _client = HttpClient.create(provider);
    } else {
        _client = HttpClient.create();
    }
    _client = _client.wiretap(true);
    HttpClient[] clients = new HttpClient[] { // by default protocol is HTTP/1.1
    _client, _client.protocol(HttpProtocol.H2C), _client.protocol(HttpProtocol.HTTP11, HttpProtocol.H2C), // by default protocol is HTTP/1.1
    _client.secure(spec -> spec.sslContext(clientCtxHttp11)), _client.secure(spec -> spec.sslContext(clientCtxHttp2)).protocol(HttpProtocol.H2), _client.secure(spec -> spec.sslContext(clientCtxHttp2)).protocol(HttpProtocol.HTTP11, HttpProtocol.H2) };
    Flux<HttpServer> f1 = Flux.fromArray(servers).concatMap(o -> Flux.just(o).repeat(clients.length - 1));
    Flux<HttpClient> f2 = Flux.fromArray(clients).repeat(servers.length - 1);
    return Flux.zip(f1, f2).filter(tuple2 -> {
        if (onlyCompatible) {
            HttpServerConfig serverConfig = tuple2.getT1().configuration();
            HttpClientConfig clientConfig = tuple2.getT2().configuration();
            List<HttpProtocol> serverProtocols = Arrays.asList(serverConfig.protocols());
            List<HttpProtocol> clientProtocols = Arrays.asList(clientConfig.protocols());
            if (serverConfig.isSecure() != clientConfig.isSecure()) {
                return false;
            } else if (serverProtocols.size() == 1 && serverProtocols.get(0) == HttpProtocol.H2C && clientProtocols.size() == 2) {
                return false;
            }
            return serverProtocols.containsAll(clientProtocols) || clientProtocols.containsAll(serverProtocols);
        }
        return true;
    }).map(Tuple2::toArray).collectList().block(Duration.ofSeconds(30)).toArray(new Object[0][2]);
}
Also used : ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Retention(java.lang.annotation.Retention) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) AfterAll(org.junit.jupiter.api.AfterAll) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) ArgumentCaptor(org.mockito.ArgumentCaptor) ByteBuf(io.netty.buffer.ByteBuf) ByteBufFlux(reactor.netty.ByteBufFlux) NettyPipeline(reactor.netty.NettyPipeline) Appender(ch.qos.logback.core.Appender) Duration(java.time.Duration) Schedulers(reactor.core.scheduler.Schedulers) HttpServerConfig(reactor.netty.http.server.HttpServerConfig) Connection(reactor.netty.Connection) MethodSource(org.junit.jupiter.params.provider.MethodSource) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) Predicate(java.util.function.Predicate) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) Mono(reactor.core.publisher.Mono) HttpClientConfig(reactor.netty.http.client.HttpClientConfig) Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) HttpServer(reactor.netty.http.server.HttpServer) List(java.util.List) Logger(ch.qos.logback.classic.Logger) ChannelHandler(io.netty.channel.ChannelHandler) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) HttpClient(reactor.netty.http.client.HttpClient) RetentionPolicy(java.lang.annotation.RetentionPolicy) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpClientConfig(reactor.netty.http.client.HttpClientConfig) HttpServerConfig(reactor.netty.http.server.HttpServerConfig) HttpClient(reactor.netty.http.client.HttpClient) HttpServer(reactor.netty.http.server.HttpServer)

Example 2 with HttpServerConfig

use of reactor.netty.http.server.HttpServerConfig in project reactor-netty by reactor.

the class HttpProtocolsTests method testGetRequest.

@ParameterizedAllCombinationsTest
void testGetRequest(HttpServer server, HttpClient client) {
    HttpServerConfig serverConfig = server.configuration();
    HttpClientConfig clientConfig = client.configuration();
    List<HttpProtocol> serverProtocols = Arrays.asList(serverConfig.protocols());
    List<HttpProtocol> clientProtocols = Arrays.asList(clientConfig.protocols());
    disposableServer = server.handle((req, res) -> {
        boolean secure = "https".equals(req.scheme());
        if (serverConfig.isSecure() != secure) {
            return res.status(400).send();
        }
        return res.sendString(Mono.just("Hello"));
    }).bindNow();
    Mono<String> response = client.port(disposableServer.port()).get().uri("/").responseContent().aggregate().asString();
    if (serverConfig.isSecure() != clientConfig.isSecure()) {
        StepVerifier.create(response).expectError().verify(Duration.ofSeconds(30));
    } else if (serverProtocols.size() == 1 && serverProtocols.get(0) == HttpProtocol.H2C && clientProtocols.size() == 2) {
        StepVerifier.create(response).expectError().verify(Duration.ofSeconds(30));
    } else if (serverProtocols.containsAll(clientProtocols) || clientProtocols.containsAll(serverProtocols)) {
        StepVerifier.create(response).expectNext("Hello").expectComplete().verify(Duration.ofSeconds(30));
    } else {
        StepVerifier.create(response).expectError().verify(Duration.ofSeconds(30));
    }
}
Also used : HttpClientConfig(reactor.netty.http.client.HttpClientConfig) HttpServerConfig(reactor.netty.http.server.HttpServerConfig)

Example 3 with HttpServerConfig

use of reactor.netty.http.server.HttpServerConfig in project reactor-netty by reactor.

the class HttpProtocolsTests method doTestPostRequest.

private void doTestPostRequest(HttpServer server, HttpClient client, boolean externalThread) {
    HttpServerConfig serverConfig = server.configuration();
    HttpClientConfig clientConfig = client.configuration();
    List<HttpProtocol> serverProtocols = Arrays.asList(serverConfig.protocols());
    List<HttpProtocol> clientProtocols = Arrays.asList(clientConfig.protocols());
    disposableServer = server.handle((req, res) -> {
        boolean secure = "https".equals(req.scheme());
        if (serverConfig.isSecure() != secure) {
            return res.status(400).send();
        }
        Flux<ByteBuf> publisher = req.receive().retain();
        if (externalThread) {
            publisher = publisher.subscribeOn(Schedulers.boundedElastic());
        }
        return res.send(publisher);
    }).bindNow();
    Mono<String> response = client.port(disposableServer.port()).post().uri("/").send(ByteBufFlux.fromString(Mono.just("Hello"))).responseContent().aggregate().asString();
    if (serverConfig.isSecure() != clientConfig.isSecure()) {
        StepVerifier.create(response).expectError().verify(Duration.ofSeconds(30));
    } else if (serverProtocols.size() == 1 && serverProtocols.get(0) == HttpProtocol.H2C && clientProtocols.size() == 2) {
        StepVerifier.create(response).expectError().verify(Duration.ofSeconds(30));
    } else if (serverProtocols.containsAll(clientProtocols) || clientProtocols.containsAll(serverProtocols)) {
        StepVerifier.create(response).expectNext("Hello").expectComplete().verify(Duration.ofSeconds(30));
    } else {
        StepVerifier.create(response).expectError().verify(Duration.ofSeconds(30));
    }
}
Also used : HttpClientConfig(reactor.netty.http.client.HttpClientConfig) HttpServerConfig(reactor.netty.http.server.HttpServerConfig) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

HttpClientConfig (reactor.netty.http.client.HttpClientConfig)3 HttpServerConfig (reactor.netty.http.server.HttpServerConfig)3 ByteBuf (io.netty.buffer.ByteBuf)2 Logger (ch.qos.logback.classic.Logger)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)1 Appender (ch.qos.logback.core.Appender)1 ChannelHandler (io.netty.channel.ChannelHandler)1 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)1 InsecureTrustManagerFactory (io.netty.handler.ssl.util.InsecureTrustManagerFactory)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)1 ElementType (java.lang.annotation.ElementType)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Target (java.lang.annotation.Target)1 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1