use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestRestLiServer method testPostProcessingException.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testPostProcessingException(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
// request for nested projection within string field will generate error
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(buildStatusRecord()).once();
replay(statusResource);
Callback<RestResponse> restResponseCallback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
fail();
}
@Override
public void onError(Throwable e) {
assertTrue(e instanceof RestException);
RestException restException = (RestException) e;
RestResponse restResponse = restException.getResponse();
try {
assertEquals(restResponse.getStatus(), 500);
assertTrue(restResponse.getEntity().length() > 0);
assertEquals(restResponse.getHeader(errorResponseHeaderName), RestConstants.HEADER_VALUE_ERROR);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
} catch (Exception e2) {
fail(e2.toString());
}
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1?fields=text:(invalid)")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build();
_server.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1?fields=text:(invalid)")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString()).build(EntityStreams.emptyStream());
Callback<StreamResponse> callback = new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse streamResponse) {
fail();
}
@Override
public void onError(Throwable e) {
Messages.toRestException((StreamException) e, new Callback<RestException>() {
@Override
public void onError(Throwable e) {
Assert.fail();
}
@Override
public void onSuccess(RestException result) {
restResponseCallback.onError(result);
}
});
}
};
_server.handleRequest(streamRequest, new RequestContext(), callback);
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestRestLiServer method testRestRequestAttachmentsPresent.
@Test
public void testRestRequestAttachmentsPresent() throws Exception {
// This test verifies that a RestRequest sent to the RestLiServer throws an exception if the content type is multipart/related
RestRequest contentTypeMultiPartRelated = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_CONTENT_TYPE, RestConstants.HEADER_VALUE_MULTIPART_RELATED).build();
Callback<RestResponse> callback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
fail();
}
@Override
public void onError(Throwable e) {
assertTrue(e instanceof RestException);
RestException restException = (RestException) e;
RestResponse restResponse = restException.getResponse();
assertEquals(restResponse.getStatus(), 415);
assertTrue(restResponse.getEntity().length() > 0);
assertEquals(restResponse.getEntity().asString(Charset.defaultCharset()), "This server cannot handle requests with a content type of multipart/related");
}
};
_server.handleRequest(contentTypeMultiPartRelated, new RequestContext(), callback);
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestRestLiServer method testRestRequestResponseAttachmentsDesired.
@Test
public void testRestRequestResponseAttachmentsDesired() throws Exception {
// This test verifies that a RestRequest sent to the RestLiServer throws an exception if the accept type
// includes multipart related
RestRequest acceptTypeMultiPartRelated = new RestRequestBuilder(new URI("/statuses/abcd")).setHeader(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED).build();
Callback<RestResponse> callback = new Callback<RestResponse>() {
@Override
public void onSuccess(RestResponse restResponse) {
fail();
}
@Override
public void onError(Throwable e) {
assertTrue(e instanceof RestException);
RestException restException = (RestException) e;
RestResponse restResponse = restException.getResponse();
assertEquals(restResponse.getStatus(), 406);
assertTrue(restResponse.getEntity().length() > 0);
assertEquals(restResponse.getEntity().asString(Charset.defaultCharset()), "This server cannot handle requests with an accept type of multipart/related");
}
};
_server.handleRequest(acceptTypeMultiPartRelated, new RequestContext(), callback);
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestRestLiSymbolTableRequestHandler method testNonGetRequest405.
@Test
public void testNonGetRequest405() {
RestRequest request = new RestRequestBuilder(URI.create("/symbolTable")).setMethod(HttpMethod.POST.name()).build();
CompletableFuture<RestResponse> future = new CompletableFuture<>();
_requestHandler.handleRequest(request, mock(RequestContext.class), new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
future.completeExceptionally(e);
Assert.assertEquals(((RestException) e).getResponse().getStatus(), HttpStatus.S_405_METHOD_NOT_ALLOWED.getCode());
}
@Override
public void onSuccess(RestResponse result) {
future.complete(result);
}
});
Assert.assertTrue(future.isCompletedExceptionally());
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestRestLiSymbolTableRequestHandler method testInvalidAcceptTypeRequest406.
@Test
public void testInvalidAcceptTypeRequest406() {
RestRequest request = new RestRequestBuilder(URI.create("/symbolTable")).setHeader(RestConstants.HEADER_ACCEPT, "application/randomType").build();
CompletableFuture<RestResponse> future = new CompletableFuture<>();
_requestHandler.handleRequest(request, mock(RequestContext.class), new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
future.completeExceptionally(e);
Assert.assertEquals(((RestException) e).getResponse().getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
}
@Override
public void onSuccess(RestResponse result) {
future.complete(result);
}
});
Assert.assertTrue(future.isCompletedExceptionally());
}
Aggregations