Search in sources :

Example 31 with HttpResponse

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

the class FrontendIntegrationTest method getBlobInfoAndVerify.

/**
 * Gets the blob info of the blob with blob ID {@code blobId} and verifies them against what is expected.
 * @param blobId the blob ID of the blob to HEAD.
 * @param getOption the options to use while getting the blob.
 * @param expectedHeaders the expected headers in the response.
 * @param isPrivate {@code true} if the blob is expected to be private
 * @param accountName the expected account name in the response.
 * @param containerName the expected container name in response.
 * @param usermetadata if non-null, this is expected to come as the body.
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void getBlobInfoAndVerify(String blobId, GetOption getOption, HttpHeaders expectedHeaders, boolean isPrivate, String accountName, String containerName, byte[] usermetadata) throws ExecutionException, InterruptedException {
    HttpHeaders headers = new DefaultHttpHeaders();
    if (getOption != null) {
        headers.add(RestUtils.Headers.GET_OPTION, getOption.toString());
    }
    FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId + "/" + RestUtils.SubResource.BlobInfo, headers, null);
    ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
    checkCommonGetHeadHeaders(response.headers());
    verifyBlobProperties(expectedHeaders, isPrivate, response);
    verifyAccountAndContainerHeaders(accountName, containerName, response);
    verifyUserMetadata(expectedHeaders, response, usermetadata, responseParts.queue);
    assertTrue("Channel should be active", HttpUtil.isKeepAlive(response));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts)

Example 32 with HttpResponse

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

the class FrontendIntegrationTest method multipartPostGetHeadTest.

/**
 * Tests multipart POST and verifies it via GET operations.
 * @throws Exception
 */
@Test
public void multipartPostGetHeadTest() throws Exception {
    Account refAccount = ACCOUNT_SERVICE.createAndAddRandomAccount();
    Container refContainer = refAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID);
    doPostGetHeadDeleteTest(0, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
    doPostGetHeadDeleteTest(FRONTEND_CONFIG.frontendChunkedGetResponseThresholdInBytes * 3, refAccount, refContainer, refAccount.getName(), !refContainer.isCacheable(), refAccount.getName(), refContainer.getName(), true);
    // failure case
    // size of content being POSTed is higher than what is allowed via multipart/form-data
    long maxAllowedSizeBytes = new NettyConfig(FRONTEND_VERIFIABLE_PROPS).nettyMultipartPostMaxSizeBytes;
    ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes((int) maxAllowedSizeBytes + 1));
    HttpHeaders headers = new DefaultHttpHeaders();
    setAmbryHeadersForPut(headers, 7200, !refContainer.isCacheable(), refAccount.getName(), "application/octet-stream", null, refAccount.getName(), refContainer.getName());
    HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", headers);
    HttpPostRequestEncoder encoder = createEncoder(httpRequest, content, ByteBuffer.allocate(0));
    ResponseParts responseParts = nettyClient.sendRequest(encoder.finalizeRequest(), encoder, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, response.status());
    assertTrue("No Date header", response.headers().getTimeMillis(HttpHeaderNames.DATE, -1) != -1);
    assertFalse("Channel should not be active", HttpUtil.isKeepAlive(response));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Account(com.github.ambry.account.Account) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Container(com.github.ambry.account.Container) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) NettyConfig(com.github.ambry.config.NettyConfig) ByteBuffer(java.nio.ByteBuffer) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Example 33 with HttpResponse

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

the class FrontendIntegrationTest method getUserMetadataAndVerify.

/**
 * Gets the user metadata of the blob with blob ID {@code blobId} and verifies them against what is expected.
 * @param blobId the blob ID of the blob to HEAD.
 * @param getOption the options to use while getting the blob.
 * @param expectedHeaders the expected headers in the response.
 * @param usermetadata if non-null, this is expected to come as the body.
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void getUserMetadataAndVerify(String blobId, GetOption getOption, HttpHeaders expectedHeaders, byte[] usermetadata) throws ExecutionException, InterruptedException {
    HttpHeaders headers = new DefaultHttpHeaders();
    if (getOption != null) {
        headers.add(RestUtils.Headers.GET_OPTION, getOption.toString());
    }
    FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId + "/" + RestUtils.SubResource.UserMetadata, headers, null);
    ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
    checkCommonGetHeadHeaders(response.headers());
    verifyUserMetadata(expectedHeaders, response, usermetadata, responseParts.queue);
    assertTrue("Channel should be active", HttpUtil.isKeepAlive(response));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts)

Example 34 with HttpResponse

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

the class FrontendIntegrationTest method verifyGetBlobResponse.

/**
 * Verifies the GET blob response.
 * @param responseParts the response received from the server.
 * @param range the {@link ByteRange} for the request.
 * @param expectedHeaders the expected headers in the response.
 * @param isPrivate {@code true} if the blob is private, {@code false} if not.
 * @param expectedContent the expected content of the blob.
 * @throws RestServiceException
 */
private void verifyGetBlobResponse(ResponseParts responseParts, ByteRange range, HttpHeaders expectedHeaders, boolean isPrivate, ByteBuffer expectedContent) throws RestServiceException {
    HttpResponse response = getHttpResponse(responseParts);
    assertEquals("Unexpected response status", range == null ? HttpResponseStatus.OK : HttpResponseStatus.PARTIAL_CONTENT, response.status());
    checkCommonGetHeadHeaders(response.headers());
    assertEquals("Content-Type does not match", expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE), response.headers().get(HttpHeaderNames.CONTENT_TYPE));
    assertEquals(RestUtils.Headers.BLOB_SIZE + " does not match", expectedHeaders.get(RestUtils.Headers.BLOB_SIZE), response.headers().get(RestUtils.Headers.BLOB_SIZE));
    assertEquals("Accept-Ranges not set correctly", "bytes", response.headers().get(RestUtils.Headers.ACCEPT_RANGES));
    byte[] expectedContentArray = expectedContent.array();
    if (range != null) {
        long blobSize = Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE));
        assertEquals("Content-Range header not set correctly", RestUtils.buildContentRangeAndLength(range, blobSize).getFirst(), response.headers().get(RestUtils.Headers.CONTENT_RANGE));
        ByteRange resolvedRange = range.toResolvedByteRange(blobSize);
        expectedContentArray = Arrays.copyOfRange(expectedContentArray, (int) resolvedRange.getStartOffset(), (int) resolvedRange.getEndOffset() + 1);
    } else {
        assertNull("Content-Range header should not be set", response.headers().get(RestUtils.Headers.CONTENT_RANGE));
    }
    if (expectedContentArray.length < FRONTEND_CONFIG.frontendChunkedGetResponseThresholdInBytes) {
        assertEquals("Content-length not as expected", expectedContentArray.length, HttpUtil.getContentLength(response));
    }
    verifyCacheHeaders(isPrivate, response);
    byte[] responseContentArray = getContent(responseParts.queue, expectedContentArray.length).array();
    assertArrayEquals("GET content does not match original content", expectedContentArray, responseContentArray);
    assertTrue("Channel should be active", HttpUtil.isKeepAlive(response));
}
Also used : ByteRange(com.github.ambry.router.ByteRange) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 35 with HttpResponse

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

the class FrontendIntegrationTest method getReplicasTest.

/**
 * Tests {@link RestUtils.SubResource#Replicas} requests
 * <p/>
 * For each {@link PartitionId} in the {@link ClusterMap}, a {@link BlobId} is created. The replica list returned from
 * server is checked for equality against a locally obtained replica list.
 * @throws Exception
 */
@Test
public void getReplicasTest() throws Exception {
    List<? extends PartitionId> partitionIds = CLUSTER_MAP.getWritablePartitionIds();
    for (PartitionId partitionId : partitionIds) {
        String originalReplicaStr = partitionId.getReplicaIds().toString().replace(", ", ",");
        BlobId blobId = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, ClusterMapUtils.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, partitionId, false);
        FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, blobId.getID() + "/" + RestUtils.SubResource.Replicas, Unpooled.buffer(0));
        ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
        HttpResponse response = getHttpResponse(responseParts);
        assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
        ByteBuffer content = getContent(responseParts.queue, HttpUtil.getContentLength(response));
        JSONObject responseJson = new JSONObject(new String(content.array()));
        String returnedReplicasStr = responseJson.getString(GetReplicasHandler.REPLICAS_KEY).replace("\"", "");
        assertEquals("Replica IDs returned for the BlobId do no match with the replicas IDs of partition", originalReplicaStr, returnedReplicasStr);
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) JSONObject(org.json.JSONObject) HttpResponse(io.netty.handler.codec.http.HttpResponse) ResponseParts(com.github.ambry.rest.NettyClient.ResponseParts) PartitionId(com.github.ambry.clustermap.PartitionId) BlobId(com.github.ambry.commons.BlobId) ByteBuffer(java.nio.ByteBuffer) UtilsTest(com.github.ambry.utils.UtilsTest) Test(org.junit.Test)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)230 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)103 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)56 Test (org.junit.jupiter.api.Test)56 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)54 HttpRequest (io.netty.handler.codec.http.HttpRequest)52 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)47 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)40 Test (org.junit.Test)39 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)38 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)35 HttpContent (io.netty.handler.codec.http.HttpContent)32 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)25 ByteBuf (io.netty.buffer.ByteBuf)18 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)17 ChannelFuture (io.netty.channel.ChannelFuture)16 UtilsTest (com.github.ambry.utils.UtilsTest)15 IOException (java.io.IOException)15 Map (java.util.Map)15 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)13