Search in sources :

Example 11 with HttpInitiator

use of org.jocean.http.client.HttpClient.HttpInitiator in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionClientCanceledAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionClientCanceledAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                final Subscription subscription = getresp.subscribe(subscriber);
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv request from client side
                trade.inbound().doOnNext(DISPOSE_EACH).toCompletable().await();
                // server not send response, and client cancel this interaction
                subscription.unsubscribe();
                TerminateAware.Util.awaitTerminated(trade);
                TerminateAware.Util.awaitTerminated(initiator);
                assertTrue(!initiator.isActive());
                subscriber.assertNoTerminalEvent();
                subscriber.assertNoValues();
            }
        });
        assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TestSubscriber(rx.observers.TestSubscriber) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Example 12 with HttpInitiator

use of org.jocean.http.client.HttpClient.HttpInitiator in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttps.

@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttps() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    assertEquals(0, allActiveAllocationsCount(allocator));
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
    try {
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
                // initiator 开始发送 请求
                cached.subscribe();
                LOG.debug("before get tarde");
                // server side recv req
                final HttpTrade trade = trades.take();
                LOG.debug("after get tarde");
                // recv all request
                trade.inbound().toCompletable().await();
                final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
                // send back resp
                trade.outbound(TestHttpUtil.buildByteBufResponse("text/plain", svrRespContent));
                // wait for recv all resp at client side
                cached.toCompletable().await();
                svrRespContent.release();
                assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
            }
        });
    } finally {
        assertEquals(0, allActiveAllocationsCount(allocator));
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) ByteBuf(io.netty.buffer.ByteBuf) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ConnectableObservable(rx.observables.ConnectableObservable) Observable(rx.Observable) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Example 13 with HttpInitiator

use of org.jocean.http.client.HttpClient.HttpInitiator in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionFailedAsHttp10ConnectionCloseMissingPartContent.

@Test(timeout = 5000)
public void testInitiatorInteractionFailedAsHttp10ConnectionCloseMissingPartContent() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    try {
        final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(request), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final CountDownLatch cdl4initiator = new CountDownLatch(1);
                initiator.doOnTerminate(new Action0() {

                    @Override
                    public void call() {
                        cdl4initiator.countDown();
                    }
                });
                assertEquals(0, allActiveAllocationsCount(allocator));
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                getresp.subscribe(subscriber);
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv all request
                trade.inbound().toCompletable().await();
                final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
                // for HTTP 1.0 Connection: Close response behavior
                final FullHttpResponse fullrespfromsvr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, svrRespContent);
                fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                // missing Content-Length
                fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_LENGTH, fullrespfromsvr.content().readableBytes() + 1);
                fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
                trade.outbound(Observable.just(fullrespfromsvr));
                TerminateAware.Util.awaitTerminated(trade);
                subscriber.awaitTerminalEvent();
                assertTrue(svrRespContent.release());
                subscriber.assertError(TransportException.class);
                // assertTrue(subscriber.getValueCount() > 0);
                cdl4initiator.await();
                assertEquals(0, allActiveAllocationsCount(allocator));
            }
        });
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : DisposableWrapper(org.jocean.idiom.DisposableWrapper) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Action0(rx.functions.Action0) LocalAddress(io.netty.channel.local.LocalAddress) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) TransportException(org.jocean.http.TransportException) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 14 with HttpInitiator

use of org.jocean.http.client.HttpClient.HttpInitiator in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionDisconnectFromServerAfterConnectedAsHttps.

@Test(timeout = 5000)
public void testInitiatorInteractionDisconnectFromServerAfterConnectedAsHttps() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                getresp.subscribe(subscriber);
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv request from client side
                trade.inbound().toCompletable().await();
                // disconnect
                trade.close();
                subscriber.awaitTerminalEvent();
                subscriber.assertError(TransportException.class);
                subscriber.assertNotCompleted();
                subscriber.assertNoValues();
            }
        });
        assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) TransportException(org.jocean.http.TransportException) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TestSubscriber(rx.observers.TestSubscriber) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Example 15 with HttpInitiator

use of org.jocean.http.client.HttpClient.HttpInitiator in project jocean-http by isdom.

the class DefaultHttpClientTestCase method standardInteraction.

private static Interaction standardInteraction(final PooledByteBufAllocator allocator, final BlockingQueue<HttpTrade> tradepipe) {
    return new Interaction() {

        @Override
        public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
            final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
            cached.subscribe();
            // server side recv req
            final HttpTrade trade = tradepipe.take();
            // recv all request
            trade.inbound().toCompletable().await();
            final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
            // send back resp
            trade.outbound(TestHttpUtil.buildByteBufResponse("text/plain", svrRespContent));
            // wait for recv all resp at client side
            cached.toCompletable().await();
            svrRespContent.release();
            assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
        }
    };
}
Also used : HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DisposableWrapper(org.jocean.idiom.DisposableWrapper) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) ConnectableObservable(rx.observables.ConnectableObservable) Observable(rx.Observable)

Aggregations

HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)30 LocalAddress (io.netty.channel.local.LocalAddress)26 Test (org.junit.Test)26 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)25 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)24 Subscription (rx.Subscription)24 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)23 DisposableWrapper (org.jocean.idiom.DisposableWrapper)23 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)19 IOException (java.io.IOException)18 ConnectException (java.net.ConnectException)17 CertificateException (java.security.cert.CertificateException)17 SSLException (javax.net.ssl.SSLException)17 TransportException (org.jocean.http.TransportException)17 TestSubscriber (rx.observers.TestSubscriber)16 ByteBuf (io.netty.buffer.ByteBuf)11 Observable (rx.Observable)10 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 ConnectableObservable (rx.observables.ConnectableObservable)9