Search in sources :

Example 81 with DefaultHttpRequest

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

the class TestHttpResponseProcessor method testConnectFail.

@Test
public void testConnectFail() throws DatabusException {
    Logger log = Logger.getLogger("GenericHttpResponseHandler.testConnectFail");
    TestHttpResponseProcessor respProcessor = new TestHttpResponseProcessor(log);
    TestConnectListener connectListener = new TestConnectListener(log);
    TestSendRequestListener requestListener = new TestSendRequestListener(log);
    TestCloseListener closeListener = new TestCloseListener(log);
    //Need this call to set respProcessor without triggering erroneous check
    final GenericHttpResponseHandler responseHandler = new GenericHttpResponseHandler(respProcessor, KeepAliveType.KEEP_ALIVE);
    responseHandler.setRequestListener(requestListener);
    responseHandler.setConnectionListener(connectListener);
    responseHandler.setCloseListener(closeListener);
    //use port 0 to generate connect fail
    ChannelFuture channelFuture = createChannelFuture(responseHandler, 0);
    Channel channel = channelFuture.getChannel();
    try {
        channel.write(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/test"));
        final List<String> respCallbacks = respProcessor.getCallbacks();
        final List<String> connectCallbacks = connectListener.getCallbacks();
        final List<String> requestCallbacks = requestListener.getCallbacks();
        final List<String> closeChannelCallbacks = closeListener.getCallbacks();
        TestUtil.assertWithBackoff(new ConditionCheck() {

            @Override
            public boolean check() {
                return 1 == closeChannelCallbacks.size();
            }
        }, "waiting for close channel callback", 1000, null);
        //make sure that no new callbacks have showed up
        stateSanityCheck(connectCallbacks, requestCallbacks, respCallbacks, closeChannelCallbacks);
        Assert.assertEquals(connectCallbacks.size(), 1);
        Assert.assertEquals(connectCallbacks.get(0), "onConnectFailure");
        Assert.assertEquals(closeChannelCallbacks.size(), 1);
        Assert.assertEquals(closeChannelCallbacks.get(0), "onChannelClose");
    } finally {
        channel.close();
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ConditionCheck(com.linkedin.databus2.test.ConditionCheck) 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 82 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project graylog2-server by Graylog2.

the class HttpTransportHandlerTest method messageReceivedReturns404ForWrongPath.

@Test
public void messageReceivedReturns404ForWrongPath() throws Exception {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    httpRequest.headers().add("Host", "localhost");
    httpRequest.headers().add("Origin", "http://example.com");
    httpRequest.headers().add("Connection", "close");
    channel.offer(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.poll();
    assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.NOT_FOUND);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 83 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project graylog2-server by Graylog2.

the class HttpTransportHandlerTest method messageReceivedReturns405ForInvalidMethod.

@Test
public void messageReceivedReturns405ForInvalidMethod() throws Exception {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    httpRequest.headers().add("Host", "localhost");
    httpRequest.headers().add("Origin", "http://example.com");
    httpRequest.headers().add("Connection", "close");
    channel.offer(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.poll();
    assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.METHOD_NOT_ALLOWED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 84 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project graylog2-server by Graylog2.

the class HttpTransportHandlerTest method messageReceivedSuccessfullyProcessesOPTIONSRequest.

@Test
public void messageReceivedSuccessfullyProcessesOPTIONSRequest() throws Exception {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/gelf");
    httpRequest.headers().add("Host", "localhost");
    httpRequest.headers().add("Origin", "http://example.com");
    httpRequest.headers().add("Connection", "close");
    channel.offer(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.poll();
    assertThat(httpResponse.getStatus()).isEqualTo(HttpResponseStatus.OK);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaders.Names.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 85 with DefaultHttpRequest

use of org.jboss.netty.handler.codec.http.DefaultHttpRequest in project opentsdb by OpenTSDB.

the class TestHttpQuery method getContentDefault.

@Test
public void getContentDefault() {
    final Channel channelMock = NettyMocks.fakeChannel();
    final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    final ChannelBuffer buf = ChannelBuffers.copiedBuffer("Sí Señor", CharsetUtil.UTF_8);
    req.setContent(buf);
    final HttpQuery query = new HttpQuery(tsdb, req, channelMock);
    assertEquals("Sí Señor", query.getContent());
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Channel(org.jboss.netty.channel.Channel) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.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