Search in sources :

Example 21 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForResponseAfterAbort.

@Test
public final void testTradeForResponseAfterAbort() {
    final HttpTrade trade = new DefaultHttpTrade(new EmbeddedChannel());
    trade.close();
    assertFalse(trade.isActive());
    final SubscriberHolder<HttpObject> subsholder = new SubscriberHolder<>();
    final Subscription subscription = trade.outbound(Observable.unsafeCreate(subsholder));
    assertNull(subscription);
    assertEquals(0, subsholder.getSubscriberCount());
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) HttpObject(io.netty.handler.codec.http.HttpObject) 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)

Example 22 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testDoOnClosedBeforeAndAfterOutboundResponse.

@Test
public final void testDoOnClosedBeforeAndAfterOutboundResponse() {
    final HttpTrade trade = new DefaultHttpTrade(new EmbeddedChannel());
    final AtomicBoolean onClosed = new AtomicBoolean(false);
    trade.doOnTerminate(new Action1<HttpTrade>() {

        @Override
        public void call(final HttpTrade trade) {
            onClosed.set(true);
        }
    });
    assertFalse(onClosed.get());
    assertTrue(trade.isActive());
    trade.outbound(Observable.<HttpObject>error(new RuntimeException("ResponseError")));
    assertFalse(trade.isActive());
    assertTrue(onClosed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 23 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForRequestAndContentsSourceAndDumpFullRequests.

@Test
public final void testTradeForRequestAndContentsSourceAndDumpFullRequests() throws IOException {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    Observable.<HttpObject>concat(Observable.<HttpObject>just(request), Observable.<HttpObject>from(req_contents), Observable.<HttpObject>just(LastHttpContent.EMPTY_LAST_CONTENT)).map(obj -> writeToInboundAndFlush(channel, obj)).last().toBlocking().single().syncUninterruptibly();
    callByteBufHolderBuilderOnceAndAssertDumpContentAndRefCnt(trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap(), REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    callByteBufHolderBuilderOnceAndAssertDumpContentAndRefCnt(trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap(), REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 24 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForMultiSubscribeRequestOnlyOneToSource2.

// 3 subscriber subscribe inbound request at different time,
// so push with different httpobject
@Test
public final void testTradeForMultiSubscribeRequestOnlyOneToSource2() {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    // trade.inboundHolder().setMaxBlockSize(-1);
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber1 = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber1);
    writeToInboundAndFlush(channel, request);
    reqSubscriber1.assertValueCount(1);
    reqSubscriber1.assertValues(RxNettys.<HttpObject>wrap4release(request));
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber2 = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber2);
    writeToInboundAndFlush(channel, req_contents[0]);
    reqSubscriber1.assertValueCount(2);
    reqSubscriber1.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
    reqSubscriber2.assertValueCount(2);
    reqSubscriber2.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber3 = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber3);
    reqSubscriber1.assertValueCount(2);
    reqSubscriber1.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
    reqSubscriber2.assertValueCount(2);
    reqSubscriber2.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
    reqSubscriber3.assertValueCount(2);
    reqSubscriber3.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DisposableWrapper(org.jocean.idiom.DisposableWrapper) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TestSubscriber(rx.observers.TestSubscriber) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 25 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class HttpServerDemo method main.

public static void main(final String[] args) throws Exception {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final // SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    // create for LocalChannel
    @SuppressWarnings("resource") final HttpServerBuilder server = new DefaultHttpServerBuilder(new AbstractBootstrapCreator(new DefaultEventLoopGroup(1), new DefaultEventLoopGroup()) {

        @Override
        protected void initializeBootstrap(final ServerBootstrap bootstrap) {
            bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
            bootstrap.channel(LocalServerChannel.class);
        }
    }, Feature.ENABLE_LOGGING, new Feature.ENABLE_SSL(sslCtx));
    @SuppressWarnings("unused") final Subscription subscription = server.defineServer(new LocalAddress("test")).subscribe(new Action1<HttpTrade>() {

        @Override
        public void call(final HttpTrade trade) {
            trade.outbound(trade.inbound().compose(RxNettys.message2fullreq(trade)).map(DisposableWrapperUtil.<FullHttpRequest>unwrap()).map(new Func1<FullHttpRequest, HttpObject>() {

                @Override
                public HttpObject call(final FullHttpRequest fullreq) {
                    try {
                        final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(Nettys.dumpByteBufAsBytes(fullreq.content())));
                        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
                        return response;
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }));
        }
    });
    @SuppressWarnings("resource") final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING, new Feature.ENABLE_SSL(SslContextBuilder.forClient().build()));
    while (true) {
        final ByteBuf content = Unpooled.buffer(0);
        content.writeBytes("test content".getBytes("UTF-8"));
        final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", content);
        HttpUtil.setContentLength(request, content.readableBytes());
        /* // TODO using initiator
            final Iterator<HttpObject> itr =
                client.defineInteraction(
                new LocalAddress("test"), 
                Observable.just(request))
                .map(RxNettys.<HttpObject>retainer())
                .toBlocking().toIterable().iterator();
            
            final byte[] bytes = RxNettys.httpObjectsAsBytes(itr);
            LOG.info("recv Response: {}", new String(bytes, "UTF-8"));
            */
        Thread.sleep(1000);
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpServerBuilder(org.jocean.http.server.HttpServerBuilder) ByteBuf(io.netty.buffer.ByteBuf) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Feature(org.jocean.http.Feature) DefaultHttpClient(org.jocean.http.client.impl.DefaultHttpClient) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) LocalServerChannel(io.netty.channel.local.LocalServerChannel) HttpObject(io.netty.handler.codec.http.HttpObject) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Subscription(rx.Subscription) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) LocalAddress(io.netty.channel.local.LocalAddress) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) TestChannelCreator(org.jocean.http.client.impl.TestChannelCreator)

Aggregations

HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)59 Test (org.junit.Test)56 Subscription (rx.Subscription)41 DisposableWrapper (org.jocean.idiom.DisposableWrapper)29 LocalAddress (io.netty.channel.local.LocalAddress)26 IOException (java.io.IOException)25 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)25 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)24 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)24 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)23 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)20 Nettys4Test (org.jocean.http.util.Nettys4Test)20 TestSubscriber (rx.observers.TestSubscriber)20 SSLException (javax.net.ssl.SSLException)18 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)17 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 HttpObject (io.netty.handler.codec.http.HttpObject)17 ConnectException (java.net.ConnectException)17 CertificateException (java.security.cert.CertificateException)17