use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.
the class DefaultSignalClientTestCase method testSignalClientOnlySignalForGetDuplicateFeatures.
@Test
public void testSignalClientOnlySignalForGetDuplicateFeatures() throws Exception {
final TestResponse respToSendback = new TestResponse("0", "OK");
final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqbeanReceivedRef = new AtomicReference<>();
final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {
@Override
public void call(final FullHttpRequest req, final HttpTrade trade) {
reqMethodReceivedRef.set(req.method());
final QueryStringDecoder decoder = new QueryStringDecoder(req.uri());
reqpathReceivedRef.set(decoder.path());
reqbeanReceivedRef.set(decoder.parameters().get("id").get(0));
trade.outbound(buildResponse(respToSendback, trade.onTerminate()));
}
};
final String testAddr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
try {
final TestChannelCreator creator = new TestChannelCreator();
final TestChannelPool pool = new TestChannelPool(1);
final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool);
final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), httpclient);
signalClient.registerRequestType(TestRequest.class, TestResponse.class, null, buildUri2Addr(testAddr), // duplicate ENABLE_SETURI
RosaProfiles.ENABLE_SETPATH, Feature.ENABLE_LOGGING);
final TestRequest reqToSend = new TestRequest("1");
final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
assertEquals(HttpMethod.GET, reqMethodReceivedRef.get());
assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
assertEquals(reqToSend.getId(), reqbeanReceivedRef.get());
assertEquals(respToSendback, respReceived);
pool.awaitRecycleChannels();
} finally {
server.unsubscribe();
}
}
use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.
the class DefaultSignalClientTestCase method testSignalClientOnlySignalForPost.
@Test
public void testSignalClientOnlySignalForPost() throws Exception {
final TestResponse respToSendback = new TestResponse("0", "OK");
final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
final AtomicReference<TestRequestByPost> reqbeanReceivedRef = new AtomicReference<>();
final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {
@Override
public void call(final FullHttpRequest req, final HttpTrade trade) {
try {
reqMethodReceivedRef.set(req.method());
reqpathReceivedRef.set(req.uri());
reqbeanReceivedRef.set((TestRequestByPost) JSON.parseObject(Nettys.dumpByteBufAsBytes(req.content()), TestRequestByPost.class));
} catch (IOException e) {
LOG.warn("exception when Nettys.dumpByteBufAsBytes, detail: {}", ExceptionUtils.exception2detail(e));
}
trade.outbound(buildResponse(respToSendback, trade.onTerminate()));
}
};
final String testAddr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
try {
final TestChannelCreator creator = new TestChannelCreator();
final TestChannelPool pool = new TestChannelPool(1);
final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool);
final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), httpclient);
signalClient.registerRequestType(TestRequestByPost.class, TestResponse.class, null, buildUri2Addr(testAddr), Feature.ENABLE_LOGGING);
final TestRequestByPost reqToSend = new TestRequestByPost("1", null);
final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
assertEquals(HttpMethod.POST, reqMethodReceivedRef.get());
assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
assertEquals(reqToSend, reqbeanReceivedRef.get());
assertEquals(respToSendback, respReceived);
pool.awaitRecycleChannels();
} finally {
server.unsubscribe();
}
}
use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.
the class DefaultSignalClientTestCase method testSignalClientOnlySignalForPostWithJSONContentWithoutRegisterRespType.
@Test
public void testSignalClientOnlySignalForPostWithJSONContentWithoutRegisterRespType() throws Exception {
final byte[] respToSendback = new byte[] { 12, 13, 14, 15 };
final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
final AtomicReference<TestRequestByPost> reqbeanReceivedRef = new AtomicReference<>();
final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {
@Override
public void call(final FullHttpRequest req, final HttpTrade trade) {
try {
reqMethodReceivedRef.set(req.method());
reqpathReceivedRef.set(req.uri());
reqbeanReceivedRef.set((TestRequestByPost) JSON.parseObject(Nettys.dumpByteBufAsBytes(req.content()), TestRequestByPost.class));
} catch (IOException e) {
LOG.warn("exception when Nettys.dumpByteBufAsBytes, detail: {}", ExceptionUtils.exception2detail(e));
}
trade.outbound(buildBytesResponse(respToSendback, trade.onTerminate()));
}
};
final String testAddr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
try {
final TestChannelCreator creator = new TestChannelCreator();
final TestChannelPool pool = new TestChannelPool(1);
final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool, Feature.ENABLE_LOGGING);
final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), buildUri2Addr(testAddr), httpclient);
final TestRequestByPost reqToSend = new TestRequestByPost("1", null);
final byte[] bytesReceived = ((SignalClient) signalClient).interaction().request(reqToSend).feature(new SignalClient.UsingPath("/test/simpleRequest"), new SignalClient.UsingMethod(POST.class), new SignalClient.JSONContent("{\"code\": \"added\"}")).<byte[]>build().toBlocking().single();
assertEquals(HttpMethod.POST, reqMethodReceivedRef.get());
assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
reqToSend.setCode("added");
assertEquals(reqToSend, reqbeanReceivedRef.get());
assertTrue(Arrays.equals(respToSendback, bytesReceived));
pool.awaitRecycleChannels();
} finally {
server.unsubscribe();
}
}
use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.
the class DefaultHttpTradeTestCase method testTradeForFullRequestSourceAndMultiInboundThenDumpFullRequestContent.
@Test
public final void testTradeForFullRequestSourceAndMultiInboundThenDumpFullRequestContent() throws IOException {
final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Nettys4Test.buildByteBuf(REQ_CONTENT));
final EmbeddedChannel channel = new EmbeddedChannel();
final HttpTrade trade = new DefaultHttpTrade(channel);
final AtomicReference<DefaultFullHttpRequest> ref1 = new AtomicReference<DefaultFullHttpRequest>();
trade.inbound().subscribe(new Action1<DisposableWrapper<HttpObject>>() {
@Override
public void call(DisposableWrapper<HttpObject> dwh) {
ref1.set((DefaultFullHttpRequest) dwh.unwrap());
}
});
final AtomicReference<DefaultFullHttpRequest> ref2 = new AtomicReference<DefaultFullHttpRequest>();
trade.inbound().subscribe(new Action1<DisposableWrapper<HttpObject>>() {
@Override
public void call(DisposableWrapper<HttpObject> dwh) {
ref2.set((DefaultFullHttpRequest) dwh.unwrap());
}
});
writeToInboundAndFlush(channel, request);
final byte[] firstReadBytes = Nettys.dumpByteBufAsBytes(ref1.get().content());
assertTrue(Arrays.equals(REQ_CONTENT.getBytes(Charsets.UTF_8), firstReadBytes));
final byte[] secondReadBytes = Nettys.dumpByteBufAsBytes(ref2.get().content());
assertTrue(Arrays.equals(REQ_CONTENT.getBytes(Charsets.UTF_8), secondReadBytes));
}
use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.
the class DefaultHttpTradeTestCase method testTradeForCallAbortAfterPartRequestThenPushError.
@Test
public final void testTradeForCallAbortAfterPartRequestThenPushError() {
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);
// trade.inboundHolder().setMaxBlockSize(-1);
final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber = new TestSubscriber<>();
trade.inbound().subscribe(reqSubscriber);
writeToInboundAndFlush(channel, request);
writeToInboundAndFlush(channel, req_contents[0]);
trade.close();
assertFalse(trade.isActive());
/* TODO, fix no terminal event
reqSubscriber.assertTerminalEvent();
reqSubscriber.assertError(Exception.class);
*/
reqSubscriber.assertValueCount(2);
reqSubscriber.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
}
Aggregations