use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testInternalErrorMessage.
@Test(dataProvider = "restOrStream")
public void testInternalErrorMessage(final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andThrow(new IllegalArgumentException("oops")).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 {
ErrorResponse responseBody = DataMapUtils.read(restResponse.getEntity().asInputStream(), ErrorResponse.class, restResponse.getHeaders());
assertEquals(responseBody.getMessage(), ErrorResponseBuilder.DEFAULT_INTERNAL_ERROR_MESSAGE);
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
} catch (Exception e2) {
fail(e2.toString());
}
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
_server.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.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.RestRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testCustomizedInternalErrorMessage.
@Test(dataProvider = "restOrStream")
public void testCustomizedInternalErrorMessage(final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andThrow(new IllegalArgumentException("oops")).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 {
ErrorResponse responseBody = DataMapUtils.read(restResponse.getEntity().asInputStream(), ErrorResponse.class, restResponse.getHeaders());
assertEquals(responseBody.getMessage(), "kthxbye.");
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
} catch (Exception e2) {
fail(e2.toString());
}
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
_serverWithCustomErrorResponseConfig.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.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);
}
});
}
};
_serverWithCustomErrorResponseConfig.handleRequest(streamRequest, new RequestContext(), callback);
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestRestLiSymbolTableRequestHandler method testReturnOtherSymbolTableNonDefaultAcceptType.
@Test
public void testReturnOtherSymbolTableNonDefaultAcceptType() throws Exception {
SymbolTable symbolTable = new InMemorySymbolTable("TestName", ImmutableList.of("Haha", "Hehe", "Hoho"));
URI uri = URI.create("/symbolTable/OtherTable");
RestRequest request = new RestRequestBuilder(uri).setHeader(RestConstants.HEADER_ACCEPT, ContentType.JSON.getHeaderKey()).build();
when(_symbolTableProvider.getSymbolTable(eq("OtherTable"))).thenReturn(symbolTable);
CompletableFuture<RestResponse> future = new CompletableFuture<>();
_requestHandler.handleRequest(request, mock(RequestContext.class), new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
future.completeExceptionally(e);
}
@Override
public void onSuccess(RestResponse result) {
future.complete(result);
}
});
Assert.assertFalse(future.isCompletedExceptionally());
Assert.assertTrue(future.isDone());
RestResponse response = future.get();
Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), ContentType.JSON.getHeaderKey());
Assert.assertEquals(symbolTable, SymbolTableSerializer.fromByteString(response.getEntity(), ContentType.JSON.getCodec()));
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestRestLiSymbolTableRequestHandler method testSelfSymbolTableNotFound404.
@Test
public void testSelfSymbolTableNotFound404() {
RestRequest request = new RestRequestBuilder(URI.create("/symbolTable")).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_404_NOT_FOUND.getCode());
}
@Override
public void onSuccess(RestResponse result) {
future.complete(result);
}
});
Assert.assertTrue(future.isCompletedExceptionally());
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestRestLiSymbolTableRequestHandler method testOtherSymbolTableNotFound404.
@Test
public void testOtherSymbolTableNotFound404() {
RestRequest request = new RestRequestBuilder(URI.create("/symbolTable/SomeName")).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_404_NOT_FOUND.getCode());
}
@Override
public void onSuccess(RestResponse result) {
future.complete(result);
}
});
Assert.assertTrue(future.isCompletedExceptionally());
}
Aggregations