Search in sources :

Example 6 with HttpObject

use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorMultiCalldefineInteractionAndSubscribe.

@Test(timeout = 5000)
public void testInitiatorMultiCalldefineInteractionAndSubscribe() 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, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    try (final HttpInitiator initiator = client.initiator().remoteAddress(new LocalAddress(addr)).build().toBlocking().single()) {
        final Observable<? extends DisposableWrapper<HttpObject>> resp1 = initiator.defineInteraction(Observable.just(fullHttpRequest()));
        final Observable<? extends DisposableWrapper<HttpObject>> resp2 = initiator.defineInteraction(Observable.just(fullHttpRequest()));
        resp1.subscribe();
        final TestSubscriber<DisposableWrapper<HttpObject>> subscriber = new TestSubscriber<>();
        resp2.subscribe(subscriber);
        subscriber.awaitTerminalEvent();
        subscriber.assertError(RuntimeException.class);
    // assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) HttpObject(io.netty.handler.codec.http.HttpObject) TestSubscriber(rx.observers.TestSubscriber) Subscription(rx.Subscription) Test(org.junit.Test)

Example 7 with HttpObject

use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionSendPartRequestThenFailedAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionSendPartRequestThenFailedAsHttp() 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 {
        final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
        req.headers().set(HttpHeaderNames.CONTENT_LENGTH, 100);
        final ConnectableObservable<HttpObject> errorOfEnd = Observable.<HttpObject>error(new RuntimeException("test error")).publish();
        final Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.concat(Observable.<HttpObject>just(req), errorOfEnd), 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();
                assertTrue(trade.isActive());
                // fire error
                errorOfEnd.connect();
                subscriber.awaitTerminalEvent();
                subscriber.assertError(RuntimeException.class);
                subscriber.assertNoValues();
                TerminateAware.Util.awaitTerminated(trade);
                assertTrue(!trade.isActive());
            }
        }, new Action1<WriteCtrl>() {

            @Override
            public void call(final WriteCtrl writeCtrl) {
                writeCtrl.setFlushPerWrite(true);
            }
        }).transport();
        assertEquals(0, allActiveAllocationsCount(allocator));
        final Channel ch2 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).transport();
        assertEquals(0, allActiveAllocationsCount(allocator));
        assertNotSame(ch1, ch2);
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : DisposableWrapper(org.jocean.idiom.DisposableWrapper) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) WriteCtrl(org.jocean.http.WriteCtrl) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) LocalAddress(io.netty.channel.local.LocalAddress) Channel(io.netty.channel.Channel) 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 8 with HttpObject

use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.

the class DefaultSignalClientTestCase method buildResponse.

private static Observable<HttpObject> buildResponse(final Object responseBean, final Action1<Action0> onTerminate) {
    final byte[] responseBytes = JSON.toJSONBytes(responseBean);
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(responseBytes));
    onTerminate.call(new Action0() {

        @Override
        public void call() {
            final boolean released = response.release();
            if (LOG.isDebugEnabled()) {
                LOG.debug("buildBytesResponse release {} released({})", response, released);
            }
        }
    });
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return Observable.<HttpObject>just(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Action0(rx.functions.Action0) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 9 with HttpObject

use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.

the class DefaultSignalClientTestCase method buildBytesResponse.

private static Observable<HttpObject> buildBytesResponse(final byte[] bodyAsBytes, final Action1<Action0> onTerminate) {
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(bodyAsBytes));
    onTerminate.call(new Action0() {

        @Override
        public void call() {
            final boolean released = response.release();
            if (LOG.isDebugEnabled()) {
                LOG.debug("buildBytesResponse release {} released({})", response, released);
            }
        }
    });
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return Observable.<HttpObject>just(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Action0(rx.functions.Action0) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 10 with HttpObject

use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForReadyOutboundResponseAfterResponseOnNext.

@Test
public final void testTradeForReadyOutboundResponseAfterResponseOnNext() {
    final HttpTrade trade = new DefaultHttpTrade(new EmbeddedChannel());
    assertTrue(trade.isActive());
    final SubscriberHolder<HttpObject> subsholder1 = new SubscriberHolder<>();
    final Subscription subscription1 = trade.outbound(Observable.unsafeCreate(subsholder1));
    assertNotNull(subscription1);
    final DefaultHttpRequest req1 = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    Nettys4Test.emitHttpObjects(subsholder1.getAt(0), req1);
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Subscription(rx.Subscription) SubscriberHolder(org.jocean.idiom.rx.SubscriberHolder) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Aggregations

HttpObject (io.netty.handler.codec.http.HttpObject)66 Test (org.junit.Test)38 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)31 HttpContent (io.netty.handler.codec.http.HttpContent)29 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)27 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)25 ArrayList (java.util.ArrayList)19 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)16 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)14 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)9 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)9 HttpResponse (io.netty.handler.codec.http.HttpResponse)9 Subscription (rx.Subscription)9 Nettys4Test (org.jocean.http.util.Nettys4Test)8 TestSubscriber (rx.observers.TestSubscriber)8 HttpRequest (io.netty.handler.codec.http.HttpRequest)7 DisposableWrapper (org.jocean.idiom.DisposableWrapper)7 Channel (io.netty.channel.Channel)6