Search in sources :

Example 41 with HttpObject

use of io.netty.handler.codec.http.HttpObject 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 42 with HttpObject

use of io.netty.handler.codec.http.HttpObject 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 43 with HttpObject

use of io.netty.handler.codec.http.HttpObject 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 44 with HttpObject

use of io.netty.handler.codec.http.HttpObject 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)

Example 45 with HttpObject

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

the class NettysTestCase method test_httpobjs2fullresp_firstcontentdisposed.

@Test
public final void test_httpobjs2fullresp_firstcontentdisposed() throws Exception {
    final DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    final HttpContent[] resp_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final List<HttpObject> resps = new ArrayList<HttpObject>() {

        private static final long serialVersionUID = 1L;

        {
            this.add(response);
            this.addAll(Arrays.asList(resp_contents));
            this.add(LastHttpContent.EMPTY_LAST_CONTENT);
        }
    };
    RxActions.applyArrayBy(resp_contents, new Action1<HttpContent>() {

        @Override
        public void call(final HttpContent c) {
            assertEquals(1, c.content().refCnt());
        }
    });
    // release [0]'s content
    resp_contents[0].release();
    FullHttpResponse fullresp = null;
    thrown.expect(IllegalReferenceCountException.class);
    try {
        fullresp = Nettys.httpobjs2fullresp(resps);
    } finally {
        assertNull(fullresp);
        RxActions.applyArrayBy(Arrays.copyOfRange(resp_contents, 1, resp_contents.length), new Action1<HttpContent>() {

            @Override
            public void call(final HttpContent c) {
                assertEquals(1, c.content().refCnt());
            }
        });
    }
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpObject(io.netty.handler.codec.http.HttpObject) ArrayList(java.util.ArrayList) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) 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