Search in sources :

Example 6 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.

the class TestHttpResponseProcessor method testReadTimeoutNoChunking.

@Test
public void testReadTimeoutNoChunking() throws InterruptedException, DatabusException {
    final Logger log = Logger.getLogger("GenericHttpResponseHandler.testReadTimeoutNoChunking");
    final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(KeepAliveType.KEEP_ALIVE);
    responseHandler.getLog().setLevel(_logLevel);
    log.info("start");
    log.setLevel(_logLevel);
    TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
    TestConnectListener connectListener = new TestConnectListener(log);
    TestSendRequestListener requestListener = new TestSendRequestListener(log);
    TestCloseListener closeListener = new TestCloseListener(log);
    responseHandler.setConnectionListener(connectListener);
    Channel channel = createClientBootstrap(responseHandler);
    try {
        setListeners(responseHandler, respProcessor, requestListener, closeListener);
        ChannelFuture writeFuture = channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
        Assert.assertTrue(writeFuture.await(1000));
        //It seems that there is a race condition between the writeFuture succeeding
        //and the writeComplete message getting to the handler. Make sure that the
        //writeComplete has got to the handler before we do anything else with
        //the channel.
        final GenericHttpResponseHandler handler = getResponseHandler(channel);
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return handler._messageState.hasSentRequest();
            }
        }, "request sent", 1000, log);
        Channels.fireExceptionCaught(channel, new ReadTimeoutException());
        channel.close();
        final List<String> callbacks = respProcessor.getCallbacks();
        final List<String> closeCallbacks = closeListener.getCallbacks();
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                log.debug("# callbacks:" + callbacks + ";expecting 1");
                return 1 == callbacks.size() && 1 == closeCallbacks.size();
            }
        }, "waiting for response processed", 5000, null);
        final List<String> connectCallbacks = connectListener.getCallbacks();
        final List<String> requestCallbacks = requestListener.getCallbacks();
        stateSanityCheck(connectCallbacks, requestCallbacks, callbacks, closeCallbacks);
        Assert.assertTrue(callbacks.get(0).startsWith("channelException"));
        //Assert.assertEquals(callbacks.get(1), "channelClosed");  // we don't get channelClosed after exception anymore
        //make sure that no new callbacks have showed up
        Assert.assertEquals(callbacks.size(), 1);
    } finally {
        channel.close();
        log.info("end");
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ConditionCheck(com.linkedin.databus2.test.ConditionCheck) ReadTimeoutException(org.jboss.netty.handler.timeout.ReadTimeoutException) Channel(org.jboss.netty.channel.Channel) Logger(org.apache.log4j.Logger) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Test(org.testng.annotations.Test)

Example 7 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.

the class TestRelayCommandsLocal method testSourcesTrackingCommand.

@Test
public void testSourcesTrackingCommand() throws Exception {
    LOG.debug("\n\nstarting testSourcesTrackingCommand()\n");
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/sources");
    httpRequest.setHeader(DatabusHttpHeaders.DBUS_CLIENT_HOST_HDR, "localhost");
    httpRequest.setHeader(DatabusHttpHeaders.DBUS_CLIENT_SERVICE_HDR, "unittestclient");
    SimpleTestHttpClient httpClient = SimpleTestHttpClient.createLocal(TimeoutPolicy.ALL_TIMEOUTS);
    SimpleHttpResponseHandler respHandler = httpClient.sendRequest(_serverAddress, httpRequest);
    assertTrue("failed to get a response", respHandler.awaitResponseUninterruptedly(1, TimeUnit.SECONDS));
    ByteArrayInputStream in = new ByteArrayInputStream(respHandler.getReceivedBytes());
    ObjectMapper objMapper = new ObjectMapper();
    List<IdNamePair> res = objMapper.readValue(in, new TypeReference<List<IdNamePair>>() {
    });
    assertNotNull("no result", res);
    if (LOG.isDebugEnabled()) {
        LOG.debug("/sources response:" + new String(respHandler.getReceivedBytes()));
    }
    HashSet<IdNamePair> origSet = new HashSet<IdNamePair>(_staticConfig.getSourceIds());
    HashSet<IdNamePair> resSet = new HashSet<IdNamePair>(res);
    Assert.assertEquals(origSet, resSet);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SimpleHttpResponseHandler(com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) SimpleTestHttpClient(com.linkedin.databus.core.test.netty.SimpleTestHttpClient) List(java.util.List) IdNamePair(com.linkedin.databus.core.util.IdNamePair) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 8 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.

the class DummyHttpRequestHandler method testSetResponseCode.

@Test
public void testSetResponseCode() {
    LOG.info("Start: Testing response code with headers with empty body");
    setupClient();
    ArrayList<byte[]> chunks = new ArrayList<byte[]>();
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("header1", "value1");
    headers.put("header2", "value2");
    HashMap<String, String> footers = new HashMap<String, String>();
    setupServer(HttpResponseStatus.BAD_GATEWAY, chunks, headers, footers);
    ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
    connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", connectFuture.isSuccess());
    HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
    Channel requestChannel = connectFuture.getChannel();
    ChannelFuture writeFuture = requestChannel.write(request);
    writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", writeFuture.isSuccess());
    HttpResponse response = _responseHandler.getResponse();
    assertEquals("response code", HttpResponseStatus.BAD_GATEWAY, response.getStatus());
    assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
    assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
    assertEquals("Checking header3 value", null, response.getHeader("header3"));
    byte[] responseBody = _responseHandler.getReceivedBytes();
    assertTrue("empty response", null == responseBody || responseBody.length == 0);
    LOG.info("Done: Testing response code with headers with empty body");
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HashMap(java.util.HashMap) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChunkedBodyWritableByteChannel(com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel) Channel(org.jboss.netty.channel.Channel) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.testng.annotations.Test)

Example 9 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.

the class DummyHttpRequestHandler method testWriteOneChunk.

@Test
public void testWriteOneChunk() {
    LOG.info("Start: Testing headers with one chunk");
    setupClient();
    String chunk1 = "hello";
    ArrayList<byte[]> chunks = new ArrayList<byte[]>();
    chunks.add(chunk1.getBytes(Charset.defaultCharset()));
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("header1", "value1");
    headers.put("header2", "value2");
    HashMap<String, String> footers = new HashMap<String, String>();
    setupServer(HttpResponseStatus.OK, chunks, headers, footers);
    ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
    connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", connectFuture.isSuccess());
    HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
    Channel requestChannel = connectFuture.getChannel();
    ChannelFuture writeFuture = requestChannel.write(request);
    writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", writeFuture.isSuccess());
    HttpResponse response = _responseHandler.getResponse();
    assertEquals("response code", Integer.toString(HttpResponseStatus.OK.getCode()), response.getHeader(ChunkedBodyWritableByteChannel.RESPONSE_CODE_FOOTER_NAME));
    assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
    assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
    byte[] responseBody = _responseHandler.getReceivedBytes();
    assertEquals("response length", chunk1.getBytes(Charset.defaultCharset()).length, responseBody.length);
    assertTrue("response content", Arrays.equals(chunk1.getBytes(Charset.defaultCharset()), responseBody));
    LOG.info("Done: Testing headers with one chunk");
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HashMap(java.util.HashMap) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChunkedBodyWritableByteChannel(com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel) Channel(org.jboss.netty.channel.Channel) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.testng.annotations.Test)

Example 10 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project databus by linkedin.

the class DummyHttpRequestHandler method testWriteHeadersEmptyBody.

@Test
public void testWriteHeadersEmptyBody() {
    LOG.info("Start: Testing headers with empty body");
    setupClient();
    ArrayList<byte[]> chunks = new ArrayList<byte[]>();
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("header1", "value1");
    headers.put("header2", "value2");
    HashMap<String, String> footers = new HashMap<String, String>();
    setupServer(HttpResponseStatus.OK, chunks, headers, footers);
    ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
    connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", connectFuture.isSuccess());
    HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
    Channel requestChannel = connectFuture.getChannel();
    ChannelFuture writeFuture = requestChannel.write(request);
    writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", writeFuture.isSuccess());
    HttpResponse response = _responseHandler.getResponse();
    assertEquals("response code", HttpResponseStatus.OK, response.getStatus());
    assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
    assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
    assertEquals("Checking header3 value", null, response.getHeader("header3"));
    byte[] responseBody = _responseHandler.getReceivedBytes();
    assertTrue("empty response", null == responseBody || responseBody.length == 0);
    LOG.info("Done: Testing headers with empty body");
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HashMap(java.util.HashMap) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChunkedBodyWritableByteChannel(com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel) Channel(org.jboss.netty.channel.Channel) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.testng.annotations.Test)

Aggregations

DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)128 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)101 Test (org.junit.Test)53 Channel (org.jboss.netty.channel.Channel)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)33 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)25 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)24 Test (org.testng.annotations.Test)23 HttpMethod (org.jboss.netty.handler.codec.http.HttpMethod)21 ChannelFuture (org.jboss.netty.channel.ChannelFuture)19 SimpleHttpResponseHandler (com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler)12 SimpleTestHttpClient (com.linkedin.databus.core.test.netty.SimpleTestHttpClient)12 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)11 InetSocketAddress (java.net.InetSocketAddress)11 Logger (org.apache.log4j.Logger)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)10 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 SocketAddress (java.net.SocketAddress)9