use of io.netty.handler.codec.http.HttpRequest in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttps10ConnectionClose.
@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttps10ConnectionClose() 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, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
try {
final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(request), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
cached.subscribe();
// server side recv req
final HttpTrade trade = trades.take();
// recv all request
trade.inbound().toCompletable().await();
final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
// for HTTP 1.0 Connection: Close response behavior
final FullHttpResponse fullrespfromsvr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, svrRespContent);
fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
// missing Content-Length
// response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
trade.outbound(Observable.just(fullrespfromsvr));
// wait for recv all resp at client side
cached.toCompletable().await();
svrRespContent.release();
assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
}
});
} finally {
assertEquals(0, allActiveAllocationsCount(allocator));
client.close();
server.unsubscribe();
}
}
use of io.netty.handler.codec.http.HttpRequest 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.HttpRequest in project cdap by caskdata.
the class NettyRouterPipelineTest method testHttpPipelining.
@Test
public void testHttpPipelining() throws Exception {
final BlockingQueue<HttpResponseStatus> responseStatuses = new LinkedBlockingQueue<>();
EventLoopGroup eventGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(eventGroup).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("codec", new HttpClientCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(1048576));
pipeline.addLast("handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpResponse) {
responseStatuses.add(((HttpResponse) msg).status());
}
ReferenceCountUtil.release(msg);
}
});
}
});
// Create a connection and make five consecutive HTTP call without waiting for the first to respond
Channel channel = bootstrap.connect(HOSTNAME, ROUTER.getServiceMap().get(GATEWAY_NAME)).sync().channel();
for (int i = 0; i < 5; i++) {
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/v1/sleep?sleepMillis=3000");
request.headers().set(HttpHeaderNames.HOST, HOSTNAME);
channel.writeAndFlush(request);
}
// Should get the first response as normal one
HttpResponseStatus status = responseStatuses.poll(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpResponseStatus.OK, status);
// The rest four should be failure responses
for (int i = 0; i < 4; i++) {
Assert.assertEquals(HttpResponseStatus.NOT_IMPLEMENTED, responseStatuses.poll(1, TimeUnit.SECONDS));
}
eventGroup.shutdownGracefully();
channel.close();
Assert.assertTrue(responseStatuses.isEmpty());
}
use of io.netty.handler.codec.http.HttpRequest in project cdap by caskdata.
the class RouterPathTest method testStreamPath.
@Test
public void testStreamPath() throws Exception {
// Following URIs might not give actual results but we want to test resilience of Router Path Lookup
String streamPath = "/v3/namespaces/default/streams";
HttpRequest httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), streamPath);
RouteDestination result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.STREAMS_SERVICE, result);
streamPath = "///v3/namespaces/default/streams///";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("POST"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.STREAMS_SERVICE, result);
streamPath = "v3/namespaces/default///streams///";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("PUT"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.STREAMS_SERVICE, result);
streamPath = "//v3/namespaces/default///streams/HelloStream//programs///";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.APP_FABRIC_HTTP, result);
streamPath = "//v3/namespaces/default///streams/HelloStream//programs///";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("DELETE"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.STREAMS_SERVICE, result);
streamPath = "//v3/namespaces/default///streams/HelloStream//programs///";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("POST"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.STREAMS_SERVICE, result);
streamPath = "v3/namespaces/default//streams//flows///";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("DELETE"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.STREAMS_SERVICE, result);
streamPath = "v3/namespaces/default//streams/InvalidStreamName/programs/";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.APP_FABRIC_HTTP, result);
streamPath = "v3/namespaces/default//streams/InvalidStreamName/programs";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.APP_FABRIC_HTTP, result);
streamPath = "v3/namespaces/default//streams/InvalidStreamName/programs/";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("DELETE"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.STREAMS_SERVICE, result);
streamPath = "v3/namespaces/default//streams/InvalidStreamName/info/";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), streamPath);
result = pathLookup.getRoutingService(FALLBACKSERVICE, streamPath, httpRequest);
Assert.assertEquals(RouterPathLookup.STREAMS_SERVICE, result);
}
use of io.netty.handler.codec.http.HttpRequest in project cdap by caskdata.
the class RouterPathTest method testUserServicePath.
@Test
public void testUserServicePath() {
for (ProgramType programType : EnumSet.of(ProgramType.SERVICE, ProgramType.SPARK)) {
String path = "/v3/namespaces/n1/apps/a1/" + programType.getCategoryName() + "/s1/methods/m1";
HttpRequest httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), path);
RouteDestination result = pathLookup.getRoutingService(FALLBACKSERVICE, path, httpRequest);
Assert.assertEquals(ServiceDiscoverable.getName("n1", "a1", programType, "s1"), result.getServiceName());
Assert.assertTrue(ServiceDiscoverable.isUserService(result.getServiceName()));
Assert.assertNull(result.getVersion());
path = "/v3/namespaces/n1/apps/a1/versions/v1/" + programType.getCategoryName() + "/s1/methods/m1";
httpRequest = new DefaultHttpRequest(VERSION, new HttpMethod("GET"), path);
result = pathLookup.getRoutingService(FALLBACKSERVICE, path, httpRequest);
Assert.assertEquals(ServiceDiscoverable.getName("n1", "a1", programType, "s1"), result.getServiceName());
Assert.assertTrue(ServiceDiscoverable.isUserService(result.getServiceName()));
Assert.assertEquals("v1", result.getVersion());
}
}
Aggregations