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.
}
}
}
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());
}
}
}
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;
}
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");
}
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");
}
Aggregations