Search in sources :

Example 96 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class NettysTestCase method test_httpobjs2fullresp_misslastcontent.

@Test
public final void test_httpobjs2fullresp_misslastcontent() 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));
        }
    };
    RxActions.applyArrayBy(resp_contents, new Action1<HttpContent>() {

        @Override
        public void call(final HttpContent c) {
            assertEquals(1, c.content().refCnt());
        }
    });
    FullHttpResponse fullresp = null;
    thrown.expect(RuntimeException.class);
    try {
        fullresp = Nettys.httpobjs2fullresp(resps);
    } finally {
        assertNull(fullresp);
        RxActions.applyArrayBy(resp_contents, 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)

Example 97 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class RxNettysTestCase method test_BUILD_FULL_REQUEST_WhenNoLastContent.

@Test
public final void test_BUILD_FULL_REQUEST_WhenNoLastContent() {
    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<HttpObject> reqs = new ArrayList<HttpObject>() {

        private static final long serialVersionUID = 1L;

        {
            this.add(request);
            this.addAll(Arrays.asList(req_contents));
        }
    };
    final FullHttpRequest fullreq = RxNettys.BUILD_FULL_REQUEST.call(reqs.toArray(new HttpObject[0]));
    assertNull(fullreq);
}
Also used : 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) ArrayList(java.util.ArrayList) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 98 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class RxNettysTestCase method test_BUILD_FULL_REQUEST_WhenNoRequest.

@Test
public final void test_BUILD_FULL_REQUEST_WhenNoRequest() {
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final List<HttpObject> reqs = new ArrayList<HttpObject>() {

        private static final long serialVersionUID = 1L;

        {
            this.addAll(Arrays.asList(req_contents));
            this.add(LastHttpContent.EMPTY_LAST_CONTENT);
        }
    };
    final FullHttpRequest fullreq = RxNettys.BUILD_FULL_REQUEST.call(reqs.toArray(new HttpObject[0]));
    assertNull(fullreq);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) ArrayList(java.util.ArrayList) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 99 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForCompleteRoundWithMultiContentRequest.

@Test
public final void testTradeForCompleteRoundWithMultiContentRequest() {
    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)).subscribe(obj -> writeToInboundAndFlush(channel, obj));
    // new Action1<HttpObject>() {
    // @Override
    // public void call(final HttpObject obj) {
    // writeToInboundAndFlush(channel, obj);
    // }}
    assertTrue(trade.isActive());
    final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK);
    trade.outbound(Observable.<HttpObject>just(response));
    assertFalse(trade.isActive());
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) 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) 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) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 100 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project jocean-http by isdom.

the class RxNettys method httpObjectsAsBytes.

public static byte[] httpObjectsAsBytes(final Iterator<HttpObject> itr) throws IOException {
    final CompositeByteBuf composite = Unpooled.compositeBuffer();
    try {
        while (itr.hasNext()) {
            final HttpObject obj = itr.next();
            if (obj instanceof HttpContent) {
                composite.addComponent(((HttpContent) obj).content());
            }
        }
        composite.setIndex(0, composite.capacity());
        @SuppressWarnings("resource") final InputStream is = new ByteBufInputStream(composite);
        final byte[] bytes = new byte[is.available()];
        is.read(bytes);
        return bytes;
    } finally {
        ReferenceCountUtil.release(composite);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpObject(io.netty.handler.codec.http.HttpObject) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)158 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)122 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)62 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)59 Test (org.junit.Test)59 ByteBuf (io.netty.buffer.ByteBuf)40 HttpResponse (io.netty.handler.codec.http.HttpResponse)37 HttpRequest (io.netty.handler.codec.http.HttpRequest)35 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)34 ArrayList (java.util.ArrayList)30 HttpObject (io.netty.handler.codec.http.HttpObject)28 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)24 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 Channel (io.netty.channel.Channel)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)15 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 IOException (java.io.IOException)13