Search in sources :

Example 91 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project ambry by linkedin.

the class NettyMultipartRequestTest method instantiationTest.

/**
 * Tests instantiation of {@link NettyMultipartRequest} with different {@link HttpMethod} types.
 * </p>
 * Only {@link HttpMethod#POST} should succeed.
 * @throws RestServiceException
 */
@Test
public void instantiationTest() throws RestServiceException {
    HttpMethod[] successMethods = { HttpMethod.POST, HttpMethod.PUT };
    // POST and PUT will succeed.
    for (HttpMethod method : successMethods) {
        NettyRequest.bufferWatermark = 1;
        HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, "/");
        MockChannel channel = new MockChannel();
        RecvByteBufAllocator expected = channel.config().getRecvByteBufAllocator();
        NettyMultipartRequest request = new NettyMultipartRequest(httpRequest, channel, NETTY_METRICS, Collections.emptySet(), Long.MAX_VALUE);
        assertTrue("Auto-read should not have been changed", channel.config().isAutoRead());
        assertEquals("RecvByteBufAllocator should not have changed", expected, channel.config().getRecvByteBufAllocator());
        closeRequestAndValidate(request);
    }
    // Methods that will fail. Can include other methods, but these should be enough.
    HttpMethod[] methods = { HttpMethod.GET, HttpMethod.DELETE, HttpMethod.HEAD };
    for (HttpMethod method : methods) {
        HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, "/");
        try {
            new NettyMultipartRequest(httpRequest, new MockChannel(), NETTY_METRICS, Collections.emptySet(), Long.MAX_VALUE);
            fail("Creation of NettyMultipartRequest should have failed for " + method);
        } catch (IllegalArgumentException e) {
        // expected. Nothing to do.
        }
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 92 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project ambry by linkedin.

the class CopyForcingByteBuf method conversionWithBadInputTest.

/**
 * Tests conversion of {@link HttpRequest} to {@link NettyRequest} given bad input (i.e. checks for the correct
 * exception and {@link RestServiceErrorCode} if any).
 * @throws RestServiceException
 */
@Test
public void conversionWithBadInputTest() throws RestServiceException {
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
    // HttpRequest null.
    try {
        new NettyRequest(null, new MockChannel(), new NettyMetrics(new MetricRegistry()), DENY_LISTED_QUERY_PARAM_SET);
        fail("Provided null HttpRequest to NettyRequest, yet it did not fail");
    } catch (IllegalArgumentException e) {
    // expected. nothing to do.
    }
    // Channel null.
    try {
        new NettyRequest(httpRequest, null, new NettyMetrics(new MetricRegistry()), DENY_LISTED_QUERY_PARAM_SET);
        fail("Provided null Channel to NettyRequest, yet it did not fail");
    } catch (IllegalArgumentException e) {
    // expected. nothing to do.
    }
    // unknown http method
    try {
        createNettyRequest(HttpMethod.TRACE, "/", null, new MockChannel());
        fail("Unknown http method was supplied to NettyRequest. It should have failed to construct");
    } catch (RestServiceException e) {
        assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.UnsupportedHttpMethod, e.getErrorCode());
    }
    String[] invalidBlobSizeStrs = { "aba123", "12ab", "-1", "ddsdd", "999999999999999999999999999", "1.234" };
    for (String blobSizeStr : invalidBlobSizeStrs) {
        // bad blob size
        try {
            createNettyRequest(HttpMethod.GET, "/", new DefaultHttpHeaders().add(RestUtils.Headers.BLOB_SIZE, blobSizeStr), new MockChannel());
            fail("Bad blob size header was supplied to NettyRequest. It should have failed to construct");
        } catch (RestServiceException e) {
            assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.InvalidArgs, e.getErrorCode());
        }
    }
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) MetricRegistry(com.codahale.metrics.MetricRegistry) Test(org.junit.Test)

Example 93 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project ambry by linkedin.

the class CopyForcingByteBuf method createNettyRequest.

// helpers
// general
/**
 * Creates a {@link NettyRequest} with the given parameters.
 * @param httpMethod the {@link HttpMethod} desired.
 * @param uri the URI desired.
 * @param headers {@link HttpHeaders} that need to be a part of the request.
 * @param channel the {@link Channel} that the request arrived over.
 * @return {@link NettyRequest} encapsulating a {@link HttpRequest} with the given parameters.
 * @throws RestServiceException if the {@code httpMethod} is not recognized by {@link NettyRequest}.
 */
private NettyRequest createNettyRequest(HttpMethod httpMethod, String uri, HttpHeaders headers, Channel channel) throws RestServiceException {
    MetricRegistry metricRegistry = new MetricRegistry();
    RestRequestMetricsTracker.setDefaults(metricRegistry);
    HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, httpMethod, uri, false);
    if (headers != null) {
        httpRequest.headers().set(headers);
    }
    NettyRequest nettyRequest = new NettyRequest(httpRequest, channel, new NettyMetrics(metricRegistry), DENY_LISTED_QUERY_PARAM_SET);
    assertEquals("Auto-read is in an invalid state", (!httpMethod.equals(HttpMethod.POST) && !httpMethod.equals(HttpMethod.PUT)) || NettyRequest.bufferWatermark <= 0, channel.config().isAutoRead());
    return nettyRequest;
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) MetricRegistry(com.codahale.metrics.MetricRegistry)

Example 94 with DefaultHttpRequest

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

the class HttpHandlerTest method return404ForWrongPath.

@Test
public void return404ForWrongPath() {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.ORIGIN, "http://example.com");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    channel.writeInbound(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.NOT_FOUND);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 95 with DefaultHttpRequest

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

the class HttpHandlerTest method messageReceivedReturns405ForInvalidMethod.

@Test
public void messageReceivedReturns405ForInvalidMethod() {
    final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.ORIGIN, "http://example.com");
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    channel.writeInbound(httpRequest);
    channel.finish();
    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.METHOD_NOT_ALLOWED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
    assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Aggregations

DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)115 HttpRequest (io.netty.handler.codec.http.HttpRequest)81 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)53 Test (org.junit.Test)51 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)44 HttpMethod (io.netty.handler.codec.http.HttpMethod)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)20 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)18 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)16 HttpContent (io.netty.handler.codec.http.HttpContent)13 HttpObject (io.netty.handler.codec.http.HttpObject)13 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)12 ByteBuf (io.netty.buffer.ByteBuf)11 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)11 Test (org.junit.jupiter.api.Test)10 Channel (io.netty.channel.Channel)9 ArrayList (java.util.ArrayList)9 Nettys4Test (org.jocean.http.util.Nettys4Test)9 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)8 DisposableWrapper (org.jocean.idiom.DisposableWrapper)6