Search in sources :

Example 51 with HttpTrade

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

the class DefaultHttpTradeTestCase method testTradeUsingUnactiveChannel.

@Test
public final void testTradeUsingUnactiveChannel() {
    final Channel unactiveChannel = new EmbeddedChannel();
    unactiveChannel.disconnect().syncUninterruptibly();
    assertFalse(unactiveChannel.isActive());
    final HttpTrade trade = new DefaultHttpTrade(unactiveChannel);
    assertFalse(trade.isActive());
    final AtomicBoolean onClosed = new AtomicBoolean(false);
    trade.doOnTerminate(new Action1<HttpTrade>() {

        @Override
        public void call(final HttpTrade trade) {
            onClosed.set(true);
        }
    });
    assertTrue(onClosed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 52 with HttpTrade

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

the class DefaultHttpTradeTestCase method testTradeForReadyOutboundResponseAfterResponseOnCompleted.

@Test
public final void testTradeForReadyOutboundResponseAfterResponseOnCompleted() {
    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);
    subsholder1.getAt(0).onCompleted();
}
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 53 with HttpTrade

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

the class DefaultHttpTradeTestCase method testTradeForRequestPartError.

@Test
public final void testTradeForRequestPartError() {
    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 List<DisposableWrapper<HttpObject>> part_req = new ArrayList<DisposableWrapper<HttpObject>>() {

        private static final long serialVersionUID = 1L;

        {
            this.add(RxNettys.<HttpObject>wrap4release(request));
            for (int idx = 0; idx < 5; idx++) {
                this.add(RxNettys.<HttpObject>wrap4release(req_contents[idx]));
            }
        }
    };
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    // trade.inboundHolder().setMaxBlockSize(-1);
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber);
    Observable.<HttpObject>concat(Observable.<HttpObject>just(request), Observable.<HttpObject>just(req_contents[0]), Observable.<HttpObject>just(req_contents[1]), Observable.<HttpObject>just(req_contents[2]), Observable.<HttpObject>just(req_contents[3]), Observable.<HttpObject>just(req_contents[4])).map(obj -> writeToInboundAndFlush(channel, obj)).last().toBlocking().single().syncUninterruptibly();
    channel.disconnect().syncUninterruptibly();
    assertTrue(!trade.isActive());
    // TODO, fix assert terminal event
    // reqSubscriber.assertTerminalEvent();
    // java.lang.AssertionError: Exceptions differ; expected: java.lang.RuntimeException: RequestPartError,
    // actual: java.lang.RuntimeException: trade unactived
    // reqSubscriber.assertError(Exception.class);
    reqSubscriber.assertNotCompleted();
    reqSubscriber.assertValues(part_req.toArray(new DisposableWrapper[0]));
}
Also used : DisposableWrapper(org.jocean.idiom.DisposableWrapper) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) 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 54 with HttpTrade

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

the class DefaultHttpTradeTestCase method testTradeForCompleteRoundAndWithCacheOperator.

@Test
public final void testTradeForCompleteRoundAndWithCacheOperator() {
    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();
    // Error, will cause : exception when invoke visitor(org.jocean.http.util.RxNettys$2@722c517b), detail:
    // java.lang.ClassCastException: io.netty.handler.codec.http.DefaultHttpRequest cannot be cast to
    // io.netty.handler.codec.http.HttpContent
    // inbound.subscribe();    double subscribe holder.assembleAndHold()
    final FullHttpRequest recvreq = trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap();
    assertNotNull(recvreq);
    trade.close();
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) 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 55 with HttpTrade

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

the class HttpServerForCloopen method main.

public static void main(final String[] args) throws Exception {
    @SuppressWarnings("resource") final HttpServerBuilder server = new DefaultHttpServerBuilder();
    @SuppressWarnings("unused") final Subscription subscription = server.defineServer(new InetSocketAddress("0.0.0.0", 8888)).subscribe(new Action1<HttpTrade>() {

        @Override
        public void call(final HttpTrade trade) {
            // final HttpMessageHolder holder = new HttpMessageHolder();
            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 byte[] bytes = Nettys.dumpByteBufAsBytes(fullreq.content());
                        final String reqcontent = bytes.length > 0 ? new String(bytes, Charsets.UTF_8) : "empty";
                        LOG.debug("receive HttpRequest: {}\ncontent:\n{}", fullreq, reqcontent);
                    } catch (Exception e) {
                        LOG.warn("exception when dump http req content, detail:{}", ExceptionUtils.exception2detail(e));
                    }
                    byte[] bytes = new String("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><statuscode>000000</statuscode></Response>").getBytes(Charsets.UTF_8);
                    final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(bytes));
                    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
                    return response;
                }
            }));
        }
    });
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) HttpServerBuilder(org.jocean.http.server.HttpServerBuilder) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) HttpObject(io.netty.handler.codec.http.HttpObject) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Subscription(rx.Subscription)

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