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());
}
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();
}
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]));
}
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();
}
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;
}
}));
}
});
}
Aggregations