use of com.github.ambry.rest.MockRestRequest in project ambry by linkedin.
the class NoSizeRSC method readPartByPartTest.
/**
* Tests reading {@link ReadableStreamChannelInputStream} part by part.
* @param in the data that the {@link ReadableStreamChannelInputStream} should contain.
* @throws Exception
*/
private void readPartByPartTest(byte[] in) throws Exception {
// channel with size and one piece of content.
ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
InputStream stream = new ReadableStreamChannelInputStream(channel);
doReadPartByPartTest(stream, in);
stream.close();
// channel with no size but one piece of content.
channel = new NoSizeRSC(ByteBuffer.wrap(in));
stream = new ReadableStreamChannelInputStream(channel);
doReadPartByPartTest(stream, in);
stream.close();
// channel with no size and multiple pieces of content.
List<ByteBuffer> contents = splitContent(in, CONTENT_SPLIT_PART_COUNT);
contents.add(null);
channel = new MockRestRequest(MockRestRequest.DUMMY_DATA, contents);
stream = new ReadableStreamChannelInputStream(channel);
doReadPartByPartTest(stream, in);
stream.close();
}
use of com.github.ambry.rest.MockRestRequest in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method createRestRequest.
// helpers
// general
/**
* Method to easily create {@link RestRequest} objects containing a specific request.
* @param restMethod the {@link RestMethod} desired.
* @param uri string representation of the desired URI.
* @param headers any associated headers as a {@link JSONObject}.
* @param contents the content that accompanies the request.
* @return A {@link RestRequest} object that defines the request required by the input.
* @throws JSONException
* @throws UnsupportedEncodingException
* @throws URISyntaxException
*/
static RestRequest createRestRequest(RestMethod restMethod, String uri, JSONObject headers, List<ByteBuffer> contents) throws JSONException, UnsupportedEncodingException, URISyntaxException {
JSONObject request = new JSONObject();
request.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
request.put(MockRestRequest.URI_KEY, uri);
if (headers != null) {
request.put(MockRestRequest.HEADERS_KEY, headers);
}
return new MockRestRequest(request, contents);
}
use of com.github.ambry.rest.MockRestRequest in project ambry by linkedin.
the class AmbrySecurityServiceTest method createRestRequest.
/**
* Method to easily create {@link RestRequest} objects containing a specific request.
* @param restMethod the {@link RestMethod} desired.
* @param uri string representation of the desired URI.
* @param headers any associated headers as a {@link JSONObject}.
* @return A {@link RestRequest} object that defines the request required by the input.
* @throws org.json.JSONException
* @throws java.io.UnsupportedEncodingException
* @throws java.net.URISyntaxException
*/
private RestRequest createRestRequest(RestMethod restMethod, String uri, JSONObject headers) throws JSONException, UnsupportedEncodingException, URISyntaxException, RestServiceException {
JSONObject request = new JSONObject();
request.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
request.put(MockRestRequest.URI_KEY, uri);
if (headers != null) {
request.put(MockRestRequest.HEADERS_KEY, headers);
}
RestRequest restRequest = new MockRestRequest(request, null);
restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(restRequest, FRONTEND_CONFIG.pathPrefixesToRemove, CLUSTER_NAME));
return restRequest;
}
use of com.github.ambry.rest.MockRestRequest in project ambry by linkedin.
the class GetAccountsHandlerTest method createRestRequest.
// helpers
// general
/**
* Creates a {@link RestRequest} for a GET /accounts or /accounts/containers request
* @param accountName if set, add this account name as a request header.
* @param accountId if set, add this account ID as a request header.
* @param containerName if set, add this container name as request header.
* @param operation the operation this request will perform.
* @return the {@link RestRequest}
* @throws Exception
*/
private RestRequest createRestRequest(String accountName, String accountId, String containerName, String operation) throws Exception {
JSONObject data = new JSONObject();
data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
data.put(MockRestRequest.URI_KEY, operation);
JSONObject headers = new JSONObject();
if (accountName != null) {
headers.put(RestUtils.Headers.TARGET_ACCOUNT_NAME, accountName);
}
if (accountId != null) {
headers.put(RestUtils.Headers.TARGET_ACCOUNT_ID, accountId);
}
if (containerName != null) {
headers.put(RestUtils.Headers.TARGET_CONTAINER_NAME, containerName);
}
data.put(MockRestRequest.HEADERS_KEY, headers);
RestRequest restRequest = new MockRestRequest(data, null);
restRequest.setArg(RestUtils.InternalKeys.REQUEST_PATH, RequestPath.parse(restRequest, null, null));
return restRequest;
}
use of com.github.ambry.rest.MockRestRequest in project ambry by linkedin.
the class TailoredPeersClusterMap method doBadArgsTest.
// badArgsTest() helpers.
/**
* Does the test where bad args are provided in the request to {@link GetPeersHandler}.
* @param name the name of the host whose peers are required. Can be {@code null} if this param should be omitted.
* @param port the port of the host whose peers are required. Can be {@code null} if this param should be omitted.
* @param expectedErrorCode the {@link RestServiceErrorCode} expected in response.
* @throws Exception
*/
private void doBadArgsTest(String name, String port, RestServiceErrorCode expectedErrorCode) throws Exception {
StringBuilder uri = new StringBuilder(Operations.GET_PEERS + "?");
if (name != null) {
uri.append(GetPeersHandler.NAME_QUERY_PARAM).append("=").append(name);
}
if (port != null) {
if (name != null) {
uri.append("&");
}
uri.append(GetPeersHandler.PORT_QUERY_PARAM).append("=").append(port);
}
JSONObject data = new JSONObject();
data.put(MockRestRequest.REST_METHOD_KEY, RestMethod.GET.name());
data.put(MockRestRequest.URI_KEY, uri.toString());
RestRequest restRequest = new MockRestRequest(data, null);
try {
sendRequestGetResponse(restRequest, new MockRestResponseChannel());
fail("Request should have failed");
} catch (RestServiceException e) {
assertEquals("Unexpected RestServiceErrorCode", expectedErrorCode, e.getErrorCode());
}
}
Aggregations