use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestRestLiServer method testApplicationException.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocolVersions")
public void testApplicationException(final ProtocolVersion protocolVersion, final String errorResponseHeaderName, final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andThrow(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Mock Exception")).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);
ErrorResponse responseBody = DataMapUtils.read(restResponse.getEntity().asInputStream(), ErrorResponse.class, restResponse.getHeaders());
assertEquals(responseBody.getMessage(), "Mock Exception");
assertEquals(responseBody.getExceptionClass(), "com.linkedin.restli.server.RestLiServiceException");
assertTrue(responseBody.getStackTrace().startsWith("com.linkedin.restli.server.RestLiServiceException [HTTP Status:500]: Mock Exception"));
assertEquals(responseBody.getStatus().intValue(), 500);
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, protocolVersion.toString()).build();
_server.handleRequest(request, new RequestContext(), restResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).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.RestRequestBuilder in project rest.li by linkedin.
the class TestRouteLookupClient method testRouteLookupClientCallback.
@Test
public void testRouteLookupClientCallback() throws InterruptedException, ExecutionException, TimeoutException {
RouteLookup routeLookup = new SimpleTestRouteLookup();
final D2Client d2Client = new D2ClientBuilder().setZkHosts("localhost:2121").build();
d2Client.start(new FutureCallback<>());
RouteLookupClient routeLookupClient = new RouteLookupClient(d2Client, routeLookup, "WestCoast");
RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("d2://simple_uri")).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<>();
routeLookupClient.restRequest(dummyRestRequest, futureCallback, "5555");
try {
RestResponse response = futureCallback.get(10, TimeUnit.SECONDS);
Assert.fail("Unexpected success, request should have thrown a ServiceUnavailableException");
} catch (Exception e) {
String message = e.getMessage();
if (!message.contains("_serviceName=simple_uriWestCoast5555Foo")) {
Assert.fail("request was not rewritten to point at the d2 service simple_uriWestCoast5555Foo");
}
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class RouteLookupClient method createNewRequestWithNewServiceName.
/**
* Creates a new request identical to the original request, but changes the d2 service name to
* the one passed in.
* @param origRequest original request to copy
* @param newServiceName the new service name to rewrite the request with
* @return a new RestRequest
*/
private static RestRequest createNewRequestWithNewServiceName(RestRequest origRequest, String newServiceName) {
RestRequestBuilder modifiedBuilder = origRequest.builder();
URI originalURI = modifiedBuilder.getURI();
if (!("d2".equals(originalURI.getScheme()))) {
throw new IllegalArgumentException("Unsupported scheme in URI: " + originalURI);
}
UriBuilder modifiedUriBuilder = UriBuilder.fromUri(originalURI);
modifiedUriBuilder.host(newServiceName);
URI resultUri = modifiedUriBuilder.build();
modifiedBuilder.setURI(resultUri);
RestRequest resultRequest = modifiedBuilder.build();
return resultRequest;
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class HealthCheckRequestFactory method buildRestRequest.
/**
* @param method of the HttpRequest ({@link com.linkedin.r2.message.rest.RestMethod})
* @param uri full URI of the request
*/
public RestRequest buildRestRequest(String method, URI uri) {
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
requestBuilder.setMethod(method);
requestBuilder.setHeader("X-RestLi-Protocol-Version", "2.0.0");
return requestBuilder.build();
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class DegraderLoadBalancerTest method testTargetHostHeaderBinding.
@Test
public void testTargetHostHeaderBinding() {
final int NUM_SERVERS = 10;
DegraderLoadBalancerStrategyV3 strategy = getStrategy();
List<DegraderTrackerClient> clients = new ArrayList<>(NUM_SERVERS);
for (int ii = 0; ii < NUM_SERVERS; ++ii) {
clients.add(getClient(URI.create("http://server" + ii + ".testing:9876/foobar")));
}
Map<TrackerClient, Integer> serverCounts = new HashMap<>();
RestRequestBuilder builder = new RestRequestBuilder(URI.create("d2://fooservice"));
final int NUM_REQUESTS = 100;
for (int ii = 0; ii < NUM_REQUESTS; ++ii) {
TrackerClient client = getTrackerClient(strategy, builder.build(), new RequestContext(), 0, clients);
Integer count = serverCounts.get(client);
if (count == null) {
count = 0;
}
serverCounts.put(client, count + 1);
}
// First, check that requests are normally evenly distributed.
Assert.assertEquals(serverCounts.size(), NUM_SERVERS);
serverCounts.clear();
RestRequest request = builder.build();
RequestContext context = new RequestContext();
KeyMapper.TargetHostHints.setRequestContextTargetHost(context, clients.get(0).getUri());
for (int ii = 0; ii < NUM_REQUESTS; ++ii) {
TrackerClient client = getTrackerClient(strategy, request, context, 0, clients);
Integer count = serverCounts.get(client);
if (count == null) {
count = 0;
}
serverCounts.put(client, count + 1);
}
Assert.assertEquals(serverCounts.size(), 1);
Assert.assertEquals(serverCounts.keySet().iterator().next(), clients.get(0));
}
Aggregations