use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorMultiCalldefineInteractionAndSubscribe.
@Test(timeout = 5000)
public void testInitiatorMultiCalldefineInteractionAndSubscribe() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
assertEquals(0, allActiveAllocationsCount(allocator));
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
try (final HttpInitiator initiator = client.initiator().remoteAddress(new LocalAddress(addr)).build().toBlocking().single()) {
final Observable<? extends DisposableWrapper<HttpObject>> resp1 = initiator.defineInteraction(Observable.just(fullHttpRequest()));
final Observable<? extends DisposableWrapper<HttpObject>> resp2 = initiator.defineInteraction(Observable.just(fullHttpRequest()));
resp1.subscribe();
final TestSubscriber<DisposableWrapper<HttpObject>> subscriber = new TestSubscriber<>();
resp2.subscribe(subscriber);
subscriber.awaitTerminalEvent();
subscriber.assertError(RuntimeException.class);
// assertEquals(0, allActiveAllocationsCount(allocator));
} finally {
client.close();
server.unsubscribe();
}
}
use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionSendPartRequestThenFailedAsHttp.
@Test(timeout = 5000)
public void testInitiatorInteractionSendPartRequestThenFailedAsHttp() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
assertEquals(0, allActiveAllocationsCount(allocator));
try {
final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
req.headers().set(HttpHeaderNames.CONTENT_LENGTH, 100);
final ConnectableObservable<HttpObject> errorOfEnd = Observable.<HttpObject>error(new RuntimeException("test error")).publish();
final Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.concat(Observable.<HttpObject>just(req), errorOfEnd), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
getresp.subscribe(subscriber);
// server side recv req
final HttpTrade trade = trades.take();
assertTrue(trade.isActive());
// fire error
errorOfEnd.connect();
subscriber.awaitTerminalEvent();
subscriber.assertError(RuntimeException.class);
subscriber.assertNoValues();
TerminateAware.Util.awaitTerminated(trade);
assertTrue(!trade.isActive());
}
}, new Action1<WriteCtrl>() {
@Override
public void call(final WriteCtrl writeCtrl) {
writeCtrl.setFlushPerWrite(true);
}
}).transport();
assertEquals(0, allActiveAllocationsCount(allocator));
final Channel ch2 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).transport();
assertEquals(0, allActiveAllocationsCount(allocator));
assertNotSame(ch1, ch2);
} finally {
client.close();
server.unsubscribe();
}
}
use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.
the class DefaultSignalClientTestCase method buildResponse.
private static Observable<HttpObject> buildResponse(final Object responseBean, final Action1<Action0> onTerminate) {
final byte[] responseBytes = JSON.toJSONBytes(responseBean);
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(responseBytes));
onTerminate.call(new Action0() {
@Override
public void call() {
final boolean released = response.release();
if (LOG.isDebugEnabled()) {
LOG.debug("buildBytesResponse release {} released({})", response, released);
}
}
});
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
return Observable.<HttpObject>just(response);
}
use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.
the class DefaultSignalClientTestCase method buildBytesResponse.
private static Observable<HttpObject> buildBytesResponse(final byte[] bodyAsBytes, final Action1<Action0> onTerminate) {
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(bodyAsBytes));
onTerminate.call(new Action0() {
@Override
public void call() {
final boolean released = response.release();
if (LOG.isDebugEnabled()) {
LOG.debug("buildBytesResponse release {} released({})", response, released);
}
}
});
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
return Observable.<HttpObject>just(response);
}
use of io.netty.handler.codec.http.HttpObject in project jocean-http by isdom.
the class DefaultHttpTradeTestCase method testTradeForReadyOutboundResponseAfterResponseOnNext.
@Test
public final void testTradeForReadyOutboundResponseAfterResponseOnNext() {
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);
final DefaultHttpRequest req1 = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
Nettys4Test.emitHttpObjects(subsholder1.getAt(0), req1);
}
Aggregations