Search in sources :

Example 31 with HttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.

the class ChannelWriteCallback method headersPresenceTest.

/**
 * Sends a request with certain headers that will copied into the response. Checks the response for those headers to
 * see that values match.
 * @throws ParseException
 */
@Test
public void headersPresenceTest() throws ParseException {
    HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.CopyHeaders.toString());
    HttpUtil.setKeepAlive(request, false);
    EmbeddedChannel channel = createEmbeddedChannel();
    channel.writeInbound(request);
    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertFalse("Channel not closed on the server", channel.isActive());
    checkHeaders(request, response);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 32 with HttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.

the class ChannelWriteCallback method setStatusTest.

/**
 * Tests setting of different available {@link ResponseStatus} codes and sees that they are recognized and converted
 * in {@link NettyResponseChannel}.
 * <p/>
 * If this test fails, a case for conversion probably needs to be added in {@link NettyResponseChannel}.
 */
@Test
public void setStatusTest() {
    // ask for every status to be set
    for (ResponseStatus expectedResponseStatus : ResponseStatus.values()) {
        HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.SetStatus.toString());
        request.headers().set(MockNettyMessageProcessor.STATUS_HEADER_NAME, expectedResponseStatus);
        HttpUtil.setKeepAlive(request, false);
        EmbeddedChannel channel = createEmbeddedChannel();
        channel.writeInbound(request);
        // pull but discard response
        channel.readOutbound();
        assertFalse("Channel not closed on the server", channel.isActive());
    }
    // check if all the ResponseStatus codes were recognized.
    String metricName = MetricRegistry.name(NettyResponseChannel.class, "UnknownResponseStatusCount");
    long metricCount = MockNettyMessageProcessor.METRIC_REGISTRY.getCounters().get(metricName).getCount();
    assertEquals("Some of the ResponseStatus codes were not recognized", 0, metricCount);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 33 with HttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.

the class ChannelWriteCallback method closeTest.

/**
 * Tests that the underlying network channel is closed when {@link NettyResponseChannel#close()} is called.
 */
@Test
public void closeTest() {
    // request is keep-alive by default.
    HttpRequest request = createRequestWithHeaders(HttpMethod.GET, TestingUri.Close.toString());
    EmbeddedChannel channel = createEmbeddedChannel();
    channel.writeInbound(request);
    HttpResponse response = (HttpResponse) channel.readOutbound();
    assertEquals("Unexpected response status", HttpResponseStatus.INTERNAL_SERVER_ERROR, response.status());
    assertFalse("Inconsistent value for Connection header", HttpUtil.isKeepAlive(response));
    // drain the channel of content.
    while (channel.readOutbound() != null) {
    }
    assertFalse("Channel should be closed", channel.isOpen());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 34 with HttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.

the class PublicAccessLogHandlerTest method doRequestHandleWithKeepAliveTest.

/**
 * Does a test to see that two consecutive request handling results in expected entries in public access log
 * with keep alive
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param uri Uri to be used during the request
 * @param useSSL {@code true} to test SSL logging.
 * @throws Exception
 */
private void doRequestHandleWithKeepAliveTest(HttpMethod httpMethod, String uri, boolean useSSL) throws Exception {
    EmbeddedChannel channel = createChannel(useSSL);
    // contains one logged request header
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(HttpHeaderNames.CONTENT_LENGTH, new Random().nextLong());
    HttpRequest request = RestTestUtils.createRequest(httpMethod, uri, headers);
    HttpUtil.setKeepAlive(request, true);
    sendRequestCheckResponse(channel, request, uri, headers, false, false, useSSL);
    Assert.assertTrue("Channel should not be closed ", channel.isOpen());
    // contains one logged and not logged header
    headers = new DefaultHttpHeaders();
    headers.add(NOT_LOGGED_HEADER_KEY + "1", "headerValue1");
    headers.add(HttpHeaderNames.CONTENT_LENGTH, new Random().nextLong());
    request = RestTestUtils.createRequest(httpMethod, uri, headers);
    HttpUtil.setKeepAlive(request, true);
    sendRequestCheckResponse(channel, request, uri, headers, false, false, useSSL);
    Assert.assertTrue("Channel should not be closed ", channel.isOpen());
    channel.close();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Random(java.util.Random) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Example 35 with HttpRequest

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.

the class PublicAccessLogHandlerTest method doRequestHandleTest.

// requestHandleTest() helpers
/**
 * Does a test to see that request handling results in expected entries in public access log
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param uri Uri to be used during the request
 * @param testErrorCase true if error case has to be tested, false otherwise
 * @param useSSL {@code true} to test SSL logging.
 * @throws Exception
 */
private void doRequestHandleTest(HttpMethod httpMethod, String uri, boolean testErrorCase, boolean useSSL) throws Exception {
    EmbeddedChannel channel = createChannel(useSSL);
    List<HttpHeaders> httpHeadersList = getHeadersList();
    for (HttpHeaders headers : httpHeadersList) {
        HttpRequest request = RestTestUtils.createRequest(httpMethod, uri, headers);
        HttpUtil.setKeepAlive(request, true);
        sendRequestCheckResponse(channel, request, uri, headers, testErrorCase, false, useSSL);
        if (!testErrorCase) {
            Assert.assertTrue("Channel should not be closed ", channel.isOpen());
        } else {
            Assert.assertFalse("Channel should have been closed ", channel.isOpen());
            channel = createChannel(useSSL);
        }
    }
    channel.close();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Aggregations

HttpRequest (io.netty.handler.codec.http.HttpRequest)283 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)104 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)95 Test (org.junit.Test)83 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)66 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)50 HttpResponse (io.netty.handler.codec.http.HttpResponse)49 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)34 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)34 ByteBuf (io.netty.buffer.ByteBuf)32 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)31 HttpContent (io.netty.handler.codec.http.HttpContent)30 Test (org.junit.jupiter.api.Test)30 URI (java.net.URI)29 Channel (io.netty.channel.Channel)28 HttpMethod (io.netty.handler.codec.http.HttpMethod)26 IOException (java.io.IOException)25 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)24 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)19 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)19