use of com.linkedin.r2.message.rest.RestRequestBuilder in project incubator-gobblin by apache.
the class HttpTestUtils method assertEqual.
public static void assertEqual(RestRequestBuilder actual, RestRequestBuilder expect) throws IOException {
// Check entity
ByteString actualEntity = actual.getEntity();
ByteString expectedEntity = expect.getEntity();
if (actualEntity == null) {
Assert.assertTrue(expectedEntity == null);
} else {
Assert.assertEquals(actualEntity.length(), expectedEntity.length());
Assert.assertEquals(actualEntity.asString(StandardCharsets.UTF_8), expectedEntity.asString(StandardCharsets.UTF_8));
}
// Check request
RestRequest actualRequest = actual.build();
RestRequest expectedRequest = expect.build();
Assert.assertEquals(actualRequest.getMethod(), expectedRequest.getMethod());
Assert.assertEquals(actualRequest.getURI().toString(), expectedRequest.getURI().toString());
Map<String, String> actualHeaders = actualRequest.getHeaders();
Map<String, String> expectedHeaders = expectedRequest.getHeaders();
Assert.assertEquals(actualHeaders.size(), expectedHeaders.size());
for (String key : actualHeaders.keySet()) {
Assert.assertEquals(actualHeaders.get(key), expectedHeaders.get(key));
}
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class RetryClientTest method testRestRetryNoAvailableHosts.
@Test
public void testRestRetryNoAvailableHosts() throws Exception {
SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/retry2"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
DynamicClient dynamicClient = new DynamicClient(balancer, null);
RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, false);
URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
RestRequest restRequest = new RestRequestBuilder(uri).build();
DegraderTrackerClientTest.TestCallback<RestResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
client.restRequest(restRequest, restCallback);
assertNull(restCallback.t);
assertNotNull(restCallback.e);
assertTrue(restCallback.e.toString().contains("retryService is in a bad state"));
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class RewriteLoadBalancerClientTest method testWithEverything.
@Test
public void testWithEverything() {
URI uri = URI.create("http://username:password@test.linkedin.com:9876/test");
String serviceName = "HistoryService";
TestClient wrappedClient = new TestClient();
RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
RestRequest restRequest = new RestRequestBuilder(URI.create("d2://HistoryService/getCube?bar=baz#fragId")).build();
Map<String, String> restWireAttrs = new HashMap<>();
TestTransportCallback<RestResponse> restCallback = new TestTransportCallback<>();
client.restRequest(restRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.restRequest.getHeaders(), restRequest.getHeaders());
assertEquals(wrappedClient.restRequest.getEntity(), restRequest.getEntity());
assertEquals(wrappedClient.restRequest.getMethod(), restRequest.getMethod());
assertEquals(wrappedClient.restRequest.getURI(), URI.create("http://username:password@test.linkedin.com:9876/test/getCube?bar=baz#fragId"));
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class RewriteLoadBalancerClientTest method testClient.
@Test(groups = { "small", "back-end" })
public void testClient() throws URISyntaxException {
URI uri = URI.create("http://test.linkedin.com/test");
String serviceName = "HistoryService";
TestClient wrappedClient = new TestClient();
RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
RestRequest restRequest = new RestRequestBuilder(URI.create("d2://HistoryService/getCube")).build();
Map<String, String> restWireAttrs = new HashMap<>();
TestTransportCallback<RestResponse> restCallback = new TestTransportCallback<>();
client.restRequest(restRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.restRequest.getHeaders(), restRequest.getHeaders());
assertEquals(wrappedClient.restRequest.getEntity(), restRequest.getEntity());
assertEquals(wrappedClient.restRequest.getMethod(), restRequest.getMethod());
// check the rewrite
assertEquals(wrappedClient.restRequest.getURI(), URI.create("http://test.linkedin.com/test/getCube"));
assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.
the class TestBackupRequestsClient method testStatsConsumerRemoveOne.
@Test(dataProvider = "isD2Async")
public void testStatsConsumerRemoveOne(boolean isD2Async) throws Exception {
AtomicReference<ServiceProperties> serviceProperties = new AtomicReference<>();
TestBackupRequestsStrategyStatsConsumer statsConsumer = new TestBackupRequestsStrategyStatsConsumer();
serviceProperties.set(createServiceProperties(null));
BackupRequestsClient client = createClient(serviceProperties::get, statsConsumer, isD2Async);
URI uri = URI.create("d2://testService");
RestRequest restRequest = new RestRequestBuilder(uri).setEntity(CONTENT).build();
RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
Future<RestResponse> response = client.restRequest(restRequest, requestContext);
assertEquals(response.get().getStatus(), 200);
List<StatsConsumerEvent> events = statsConsumer.getEvents();
assertEquals(events.size(), 0);
serviceProperties.set(createServiceProperties(Arrays.asList(createBackupRequestsConfiguration(5, "get"), createBackupRequestsConfiguration(1, "batch_get"))));
requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
response = client.restRequest(restRequest, requestContext);
assertEquals(response.get().getStatus(), 200);
events = statsConsumer.getEvents();
assertEquals(events.size(), 2);
assertEquals(events.get(0).isEventAdd(), true);
assertEquals(events.get(0).getService(), SERVICE_NAME);
assertEquals(events.get(0).getOperation(), "get");
BackupRequestsStrategyStatsProvider statsProvider1 = events.get(0).getStatsProvider();
assertNotNull(statsProvider1);
assertEquals(events.get(1).isEventAdd(), true);
assertEquals(events.get(1).getService(), SERVICE_NAME);
assertEquals(events.get(1).getOperation(), "batch_get");
BackupRequestsStrategyStatsProvider statsProvider2 = events.get(1).getStatsProvider();
assertNotNull(statsProvider2);
serviceProperties.set(createServiceProperties(Arrays.asList(createBackupRequestsConfiguration(5, "get"))));
requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.OPERATION, "get");
response = client.restRequest(restRequest, requestContext);
assertEquals(response.get().getStatus(), 200);
events = statsConsumer.getEvents();
assertEquals(events.size(), 3);
assertEquals(events.get(2).isEventAdd(), false);
assertEquals(events.get(2).getService(), SERVICE_NAME);
assertEquals(events.get(2).getOperation(), "batch_get");
BackupRequestsStrategyStatsProvider removedStatsProvider = events.get(2).getStatsProvider();
assertNotNull(removedStatsProvider);
assertSame(statsProvider2, removedStatsProvider);
}
Aggregations