Search in sources :

Example 1 with TestHandler

use of io.cdap.cdap.internal.app.runtime.monitor.proxy.TestHandler in project cdap by caskdata.

the class TrafficRelayServerTest method testRelay.

@Test
public void testRelay() throws Exception {
    NettyHttpService httpServer = NettyHttpService.builder("test").setHttpHandlers(new TestHandler()).build();
    httpServer.start();
    try {
        TrafficRelayServer relayServer = new TrafficRelayServer(InetAddress.getLoopbackAddress(), httpServer::getBindAddress);
        relayServer.startAndWait();
        try {
            InetSocketAddress relayAddr = relayServer.getBindAddress();
            // GET
            URL url = new URL(String.format("http://%s:%d/ping", relayAddr.getHostName(), relayAddr.getPort()));
            HttpResponse response = HttpRequests.execute(HttpRequest.get(url).build(), new DefaultHttpRequestConfig(false));
            Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
            // POST
            url = new URL(String.format("http://%s:%d/echo", relayAddr.getHostName(), relayAddr.getPort()));
            response = HttpRequests.execute(HttpRequest.post(url).withBody("Testing").build(), new DefaultHttpRequestConfig(false));
            Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
            Assert.assertEquals("Testing", response.getResponseBodyAsString());
        } finally {
            relayServer.stopAndWait();
        }
    } finally {
        httpServer.stop();
    }
}
Also used : TestHandler(io.cdap.cdap.internal.app.runtime.monitor.proxy.TestHandler) InetSocketAddress(java.net.InetSocketAddress) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) NettyHttpService(io.cdap.http.NettyHttpService) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) TrafficRelayServer(io.cdap.cdap.internal.app.runtime.monitor.TrafficRelayServer) Test(org.junit.Test)

Aggregations

DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)1 TrafficRelayServer (io.cdap.cdap.internal.app.runtime.monitor.TrafficRelayServer)1 TestHandler (io.cdap.cdap.internal.app.runtime.monitor.proxy.TestHandler)1 HttpResponse (io.cdap.common.http.HttpResponse)1 NettyHttpService (io.cdap.http.NettyHttpService)1 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 Test (org.junit.Test)1