use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.
the class HealthCheckHandlerTest method testNonHealthCheckRequest.
/**
* Does a test to see that a non health check request results in expected responses
* @param httpMethod the {@link HttpMethod} for the request.
* @param uri Uri to be used during the request
* @throws IOException
*/
private void testNonHealthCheckRequest(HttpMethod httpMethod, String uri) throws IOException {
EmbeddedChannel channel = createChannel();
HttpRequest request = RestTestUtils.createRequest(httpMethod, uri, null);
FullHttpResponse response = sendRequestAndGetResponse(channel, request);
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
assertEquals("Unexpected content", httpMethod.toString(), RestTestUtils.getContentString(response));
channel.close();
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.
the class NettyMessageProcessorTest method requestHandleWithBadInputTest.
/**
* Tests for error handling flow when bad input streams are provided to the {@link NettyMessageProcessor}.
*/
@Test
public void requestHandleWithBadInputTest() throws IOException {
String content = "@@randomContent@@@";
// content without request.
EmbeddedChannel channel = createChannel();
channel.writeInbound(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content.getBytes())));
HttpResponse response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
assertFalse("Channel is not closed", channel.isOpen());
// content without request on a channel that was kept alive
channel = createChannel();
// send and receive response for a good request and keep the channel alive
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, MockBlobStorageService.ECHO_REST_METHOD, null));
channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
// drain the content
while (channel.readOutbound() != null) {
;
}
assertTrue("Channel is not active", channel.isActive());
// send content without request
channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
assertFalse("Channel is not closed", channel.isOpen());
// content when no content is expected.
channel = createChannel();
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
channel.writeInbound(new DefaultLastHttpContent(Unpooled.wrappedBuffer(content.getBytes())));
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
assertFalse("Channel is not closed", channel.isOpen());
// wrong HTTPObject.
channel = createChannel();
channel.writeInbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK));
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
assertFalse("Channel is not closed", channel.isOpen());
// request while another request is in progress.
channel = createChannel();
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
// channel should be closed by now
assertFalse("Channel is not closed", channel.isOpen());
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
// decoding failure
channel = createChannel();
HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.GET, "/", null);
httpRequest.setDecoderResult(DecoderResult.failure(new IllegalStateException("Induced failure")));
channel.writeInbound(httpRequest);
// channel should be closed by now
assertFalse("Channel is not closed", channel.isOpen());
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
// unsupported method
channel = createChannel();
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.TRACE, "/", null));
// channel should be closed by now
assertFalse("Channel is not closed", channel.isOpen());
response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.BAD_REQUEST, response.status());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.
the class NettyMessageProcessorTest method sendRequestCheckResponse.
/**
* Sends the provided {@code httpRequest} and verifies that the response is an echo of the {@code restMethod}.
* @param channel the {@link EmbeddedChannel} to send the request over.
* @param httpMethod the {@link HttpMethod} for the request.
* @param restMethod the equivalent {@link RestMethod} for {@code httpMethod}. Used to check for correctness of
* response.
* @param isKeepAlive if the request needs to be keep-alive.
* @throws IOException
*/
private void sendRequestCheckResponse(EmbeddedChannel channel, HttpMethod httpMethod, RestMethod restMethod, boolean isKeepAlive) throws IOException {
long requestId = REQUEST_ID_GENERATOR.getAndIncrement();
String uri = MockBlobStorageService.ECHO_REST_METHOD + requestId;
HttpRequest httpRequest = RestTestUtils.createRequest(httpMethod, uri, null);
HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
channel.writeInbound(httpRequest);
channel.writeInbound(new DefaultLastHttpContent());
HttpResponse response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
// MockBlobStorageService echoes the RestMethod + request id.
String expectedResponse = restMethod.toString() + requestId;
assertEquals("Unexpected content", expectedResponse, RestTestUtils.getContentString((HttpContent) channel.readOutbound()));
assertTrue("End marker was expected", channel.readOutbound() instanceof LastHttpContent);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.
the class NettyMessageProcessorTest method rawBytesPostTest.
/**
* Tests the case where raw bytes are POSTed as chunks.
* @throws InterruptedException
*/
@Test
public void rawBytesPostTest() throws InterruptedException {
Random random = new Random();
// request also contains content.
ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(random.nextInt(128) + 128));
HttpRequest postRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.wrappedBuffer(content));
postRequest.headers().set(RestUtils.Headers.SERVICE_ID, "rawBytesPostTest");
postRequest = ReferenceCountUtil.retain(postRequest);
ByteBuffer receivedContent = doPostTest(postRequest, null);
compareContent(receivedContent, Collections.singletonList(content));
// request and content separate.
final int NUM_CONTENTS = 5;
postRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
List<ByteBuffer> contents = new ArrayList<ByteBuffer>(NUM_CONTENTS);
int blobSize = 0;
for (int i = 0; i < NUM_CONTENTS; i++) {
ByteBuffer buffer = ByteBuffer.wrap(TestUtils.getRandomBytes(random.nextInt(128) + 128));
blobSize += buffer.remaining();
contents.add(i, buffer);
}
postRequest.headers().set(RestUtils.Headers.SERVICE_ID, "rawBytesPostTest");
receivedContent = doPostTest(postRequest, contents);
compareContent(receivedContent, contents);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest in project ambry by linkedin.
the class NettyMultipartRequestTest method createRequest.
// helpers
// general
/**
* Creates a {@link NettyMultipartRequest} with the given {@code headers} and {@code parts}.
* @param headers the {@link HttpHeaders} that need to be added to the request.
* @param parts the files that will form the parts of the request.
* @return a {@link NettyMultipartRequest} containing all the {@code headers} and {@code parts}.
* @throws Exception
*/
private NettyMultipartRequest createRequest(HttpHeaders headers, InMemoryFile[] parts) throws Exception {
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
if (headers != null) {
httpRequest.headers().set(headers);
}
HttpPostRequestEncoder encoder = createEncoder(httpRequest, parts);
NettyMultipartRequest request = new NettyMultipartRequest(encoder.finalizeRequest(), new MockChannel(), NETTY_METRICS, Collections.emptySet(), Long.MAX_VALUE);
assertTrue("Request channel is not open", request.isOpen());
while (!encoder.isEndOfInput()) {
// Sending null for ctx because the encoder is OK with that.
request.addContent(encoder.readChunk(PooledByteBufAllocator.DEFAULT));
}
return request;
}
Aggregations