Search in sources :

Example 21 with HttpInitiator

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

the class DefaultHttpClientTestCase method testInitiatorInteractionDisconnectFromServerAfterConnectedAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionDisconnectFromServerAfterConnectedAsHttp() 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<>();
                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 22 with HttpInitiator

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

the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttp10ConnectionClose.

@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttp10ConnectionClose() 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 Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
                cached.subscribe();
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv all request
                trade.inbound().doOnNext(DISPOSE_EACH).toCompletable().await();
                assertEquals(0, allActiveAllocationsCount(allocator));
                final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
                assertEquals(1, allActiveAllocationsCount(allocator));
                // 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
                // response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
                fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
                trade.outbound(Observable.just(fullrespfromsvr));
                // wait for recv all resp at client side
                cached.toCompletable().await();
                svrRespContent.release();
                // holder create clientside resp's content
                assertEquals(1, allActiveAllocationsCount(allocator));
                assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
            }
        });
    } finally {
        assertEquals(0, allActiveAllocationsCount(allocator));
        client.close();
        server.unsubscribe();
    }
}
Also used : 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) LocalAddress(io.netty.channel.local.LocalAddress) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) 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 23 with HttpInitiator

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

the class DefaultHttpClientTestCase method testGetInitiatorUnsubscribedAlreadyAsHttp.

@Test(timeout = 5000)
public void testGetInitiatorUnsubscribedAlreadyAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final String addr = UUID.randomUUID().toString();
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        final TestSubscriber<HttpInitiator> subscriber = new TestSubscriber<>();
        subscriber.unsubscribe();
        final Subscription subscription = client.initiator().remoteAddress(new LocalAddress(addr)).build().subscribe(subscriber);
        assertTrue(subscription.isUnsubscribed());
        subscriber.assertNoTerminalEvent();
        subscriber.assertNoValues();
        assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
    }
}
Also used : HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) LocalAddress(io.netty.channel.local.LocalAddress) TestSubscriber(rx.observers.TestSubscriber) Subscription(rx.Subscription) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.Test)

Example 24 with HttpInitiator

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

the class DefaultHttpClientTestCase method testInitiatorInteractionNotConnectedAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionNotConnectedAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final String addr = UUID.randomUUID().toString();
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        final TestSubscriber<HttpInitiator> subscriber = new TestSubscriber<>();
        client.initiator().remoteAddress(new LocalAddress(addr)).build().subscribe(subscriber);
        subscriber.awaitTerminalEvent();
        subscriber.assertError(ConnectException.class);
        subscriber.assertNoValues();
        assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
    }
}
Also used : HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) LocalAddress(io.netty.channel.local.LocalAddress) TestSubscriber(rx.observers.TestSubscriber) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.Test)

Example 25 with HttpInitiator

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

the class DefaultHttpClientTestCase method testInitiatorInteractionStillActiveAsHttp10ConnectionClose.

// (timeout=5000)
@Test
public void testInitiatorInteractionStillActiveAsHttp10ConnectionClose() 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 Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
                cached.subscribe();
                // 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
                // response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
                fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
                trade.outbound(Observable.just(fullrespfromsvr));
                // wait for recv all resp at client side
                cached.toCompletable().await();
                svrRespContent.release();
                TerminateAware.Util.awaitTerminated(trade);
                final Channel channel = (Channel) initiator.transport();
                assertTrue(!channel.isActive());
                assertTrue(initiator.isActive());
                assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
            }
        });
    } finally {
        assertEquals(0, allActiveAllocationsCount(allocator));
        client.close();
        server.unsubscribe();
    }
}
Also used : 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) LocalAddress(io.netty.channel.local.LocalAddress) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DisposableWrapper(org.jocean.idiom.DisposableWrapper) Channel(io.netty.channel.Channel) 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)

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