use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse 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.HttpResponse 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.HttpResponse 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);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project ambry by linkedin.
the class ChannelWriteCallback method errorResponseTest.
/**
* Tests that error responses are correctly formed.
*/
@Test
public void errorResponseTest() {
EmbeddedChannel channel = createEmbeddedChannel();
for (RestServiceErrorCode errorCode : RestServiceErrorCode.values()) {
HttpHeaders httpHeaders = new DefaultHttpHeaders();
httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, errorCode);
channel.writeInbound(RestTestUtils.createRequest(HttpMethod.HEAD, TestingUri.OnResponseCompleteWithRestException.toString(), httpHeaders));
HttpResponse response = channel.readOutbound();
HttpResponseStatus expectedStatus = getExpectedHttpResponseStatus(errorCode);
assertEquals("Unexpected response status", expectedStatus, response.status());
boolean containsFailureReasonHeader = response.headers().contains(NettyResponseChannel.FAILURE_REASON_HEADER);
if (expectedStatus == HttpResponseStatus.BAD_REQUEST) {
assertTrue("Could not find failure reason header.", containsFailureReasonHeader);
} else {
assertFalse("Should not have found failure reason header.", containsFailureReasonHeader);
}
if (HttpStatusClass.CLIENT_ERROR.contains(response.status().code())) {
assertEquals("Wrong error code", errorCode, RestServiceErrorCode.valueOf(response.headers().get(NettyResponseChannel.ERROR_CODE_HEADER)));
} else {
assertFalse("Should not have found error code header", response.headers().contains(NettyResponseChannel.ERROR_CODE_HEADER));
}
if (response instanceof FullHttpResponse) {
// assert that there is no content
assertEquals("The response should not contain content", 0, ((FullHttpResponse) response).content().readableBytes());
} else {
HttpContent content = channel.readOutbound();
assertTrue("End marker should be received", content instanceof LastHttpContent);
}
assertNull("There should be no more data in the channel", channel.readOutbound());
boolean shouldBeAlive = !NettyResponseChannel.CLOSE_CONNECTION_ERROR_STATUSES.contains(expectedStatus);
assertEquals("Channel state (open/close) not as expected", shouldBeAlive, channel.isActive());
assertEquals("Connection header should be consistent with channel state", shouldBeAlive, HttpUtil.isKeepAlive(response));
if (!shouldBeAlive) {
channel = createEmbeddedChannel();
}
}
channel.close();
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse 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());
}
Aggregations