use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class GetClusterMapSnapshotHandlerTest method verifyFailureWithMsg.
/**
* Verifies that attempting to get the snapshot of the cluster map fails with {@code msg}.
* @param msg the message in the {@link Exception} that will be thrown.
* @throws Exception
*/
private void verifyFailureWithMsg(String msg) throws Exception {
RestRequest restRequest = createRestRequest();
try {
sendRequestGetResponse(restRequest, new MockRestResponseChannel());
fail("Request should have failed");
} catch (Exception e) {
assertEquals("Unexpected Exception", msg, e.getMessage());
}
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class AmbryIdConverterFactoryTest method testConversion.
/**
* Tests the conversion by the {@code idConverter}.
* @param idConverter the {@link IdConverter} instance to use.
* @param restMethod the {@link RestMethod} of the {@link RestRequest} that will be created.
* @param signedIdMetadata the headers of the {@link RestRequest}.
* @param expectedOutput the expected output from the {@code idConverter}.
* @param input the input string
* @throws Exception
*/
private void testConversion(IdConverter idConverter, RestMethod restMethod, Map<String, String> signedIdMetadata, String expectedOutput, String input) throws Exception {
JSONObject requestData = new JSONObject();
requestData.put(MockRestRequest.REST_METHOD_KEY, restMethod.name());
requestData.put(MockRestRequest.URI_KEY, "/");
RestRequest restRequest = new MockRestRequest(requestData, null);
if (signedIdMetadata != null) {
restRequest.setArg(RestUtils.InternalKeys.SIGNED_ID_METADATA_KEY, signedIdMetadata);
}
IdConversionCallback callback = new IdConversionCallback();
assertEquals("Converted ID does not match expected (Future)", expectedOutput, idConverter.convert(restRequest, input, callback).get());
assertEquals("Converted ID does not match expected (Callback)", expectedOutput, callback.result);
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class AmbrySecurityServiceTest method preProcessRequestTest.
/**
* Tests for {@link AmbrySecurityService#preProcessRequest(RestRequest, Callback)}
* @throws Exception
*/
@Test
public void preProcessRequestTest() throws Exception {
RestMethod[] methods = new RestMethod[] { RestMethod.POST, RestMethod.GET, RestMethod.DELETE, RestMethod.HEAD, RestMethod.OPTIONS, RestMethod.PUT };
for (RestMethod restMethod : methods) {
// add a header that is prohibited
JSONObject headers = new JSONObject();
headers.put(RestUtils.InternalKeys.KEEP_ALIVE_ON_ERROR_HINT, true);
RestRequest restRequest = createRestRequest(restMethod, "/", headers);
try {
securityService.preProcessRequest(restRequest).get(1, TimeUnit.SECONDS);
Assert.fail("Should have failed because the request contains a prohibited header: " + RestUtils.InternalKeys.KEEP_ALIVE_ON_ERROR_HINT);
} catch (ExecutionException e) {
RestServiceException rse = (RestServiceException) Utils.getRootCause(e);
Assert.assertEquals("Should be a bad request", RestServiceErrorCode.BadRequest, rse.getErrorCode());
}
}
// verify request args regarding to tracking is set accordingly
RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null);
securityService.preProcessRequest(restRequest).get();
Assert.assertTrue("The arg with key: ambry-internal-keys-send-tracking-info should be set to true", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_TRACKING_INFO));
Properties properties = new Properties();
properties.setProperty("frontend.attach.tracking.info", "false");
FrontendConfig frontendConfig = new FrontendConfig(new VerifiableProperties(properties));
SecurityService securityServiceWithTrackingDisabled = new AmbrySecurityService(frontendConfig, new FrontendMetrics(new MetricRegistry()), URL_SIGNING_SERVICE_FACTORY.getUrlSigningService(), hostLevelThrottler, QUOTA_MANAGER);
restRequest = createRestRequest(RestMethod.GET, "/", null);
securityServiceWithTrackingDisabled.preProcessRequest(restRequest);
Assert.assertFalse("The arg with key: ambry-internal-keys-send-tracking-info should be set to false", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_TRACKING_INFO));
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class CostPolicyTestUtils method createMockRequestWithMethod.
/**
* Creates a mock {@link RestRequest} object for test.
* @param restMethod {@link RestMethod} of the RestRequest.
* @param bytesReceived number of bytes received in the request.
* @return RestRequest object.
*/
public static RestRequest createMockRequestWithMethod(RestMethod restMethod, String uri, long bytesReceived) throws RestServiceException {
RestRequest restRequest = mock(RestRequest.class);
when(restRequest.getRestMethod()).thenReturn(restMethod);
when(restRequest.getBlobBytesReceived()).thenReturn(bytesReceived);
when(restRequest.getBytesReceived()).thenReturn(bytesReceived);
if (restMethod == RestMethod.POST) {
when(restRequest.getUri()).thenReturn("/");
when(restRequest.getPath()).thenReturn("/");
} else {
when(restRequest.getUri()).thenReturn(uri);
when(restRequest.getPath()).thenReturn(uri);
}
RequestPath requestPath = RequestPath.parse(restRequest, null, "ambry-test");
if (restMethod == RestMethod.PUT && bytesReceived != -1) {
requestPath = RequestPath.parse("/" + Operations.NAMED_BLOB, Collections.emptyMap(), Collections.emptyList(), "ambry-test");
}
Map<String, Object> args = new HashMap<>();
args.put(RestUtils.InternalKeys.REQUEST_PATH, requestPath);
when(restRequest.getArgs()).thenReturn(args);
return restRequest;
}
use of com.github.ambry.rest.RestRequest in project ambry by linkedin.
the class GetStatsReportHandlerTest method handleBadCaseTest.
@Test
public void handleBadCaseTest() throws Exception {
ThrowingBiConsumer<RestRequest, RestServiceErrorCode> testAction = (request, expectedErrorCode) -> {
TestUtils.assertException(RestServiceException.class, () -> sendRequestGetResponse(request, new MockRestResponseChannel()), e -> assertEquals(expectedErrorCode, e.getErrorCode()));
};
RestRequest request = createRestRequest("WRONG_CLUSTER", StatsReportType.ACCOUNT_REPORT.name());
testAction.accept(request, RestServiceErrorCode.NotFound);
request = createRestRequest(null, StatsReportType.ACCOUNT_REPORT.name());
testAction.accept(request, RestServiceErrorCode.MissingArgs);
request = createRestRequest(CLUSTER_NAME, "WRONG_STATS_REPORT_TYPE");
testAction.accept(request, RestServiceErrorCode.BadRequest);
request = createRestRequest(CLUSTER_NAME, null);
testAction.accept(request, RestServiceErrorCode.MissingArgs);
}
Aggregations