use of com.linkedin.r2.transport.common.Server in project rest.li by linkedin.
the class TestRestLiServer method testHandleRequestWithRestLiResponseError.
@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseError(final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(null).once();
replay(statusResource);
Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {
@Override
public void onSuccess(RestLiResponse restLiResponse) {
fail("We should not get a success here. The server should have returned a 404!");
}
@Override
public void onError(Throwable e) {
RestLiResponseException restLiResponseException = (RestLiResponseException) e;
assertEquals(restLiResponseException.getRestLiResponse().getStatus(), HttpStatus.S_404_NOT_FOUND, "We should get a 404 back here!");
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
};
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.handleRequestWithRestLiResponse(request, new RequestContext(), restLiResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
_server.handleRequestWithRestLiResponse(streamRequest, new RequestContext(), restLiResponseCallback);
}
}
use of com.linkedin.r2.transport.common.Server in project rest.li by linkedin.
the class TestRestLiServer method testValidReactiveUnstructuredDataRequest.
@Test(dataProvider = "validClientProtocolVersionDataStreamOnly")
public void testValidReactiveUnstructuredDataRequest(RestLiServer server, ProtocolVersion clientProtocolVersion, String headerConstant) throws URISyntaxException, IOException {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/reactiveFeedDownloads/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
final FeedDownloadResourceReactive resource = getMockResource(FeedDownloadResourceReactive.class);
resource.get(eq(1L), anyObject());
EasyMock.expectLastCall().andDelegateTo(new FeedDownloadResourceReactive()).once();
replay(resource);
@SuppressWarnings("unchecked") Callback<StreamResponse> r2Callback = createMock(Callback.class);
final Capture<StreamResponse> streamResponse = EasyMock.newCapture();
r2Callback.onSuccess(capture(streamResponse));
expectLastCall().once();
replay(r2Callback);
RequestContext requestContext = new RequestContext();
server.handleRequest(streamRequest, requestContext, r2Callback);
verify(resource);
verify(r2Callback);
assertNotNull(streamResponse);
assertEquals(streamResponse.getValue().getHeader(RestConstants.HEADER_CONTENT_TYPE), FeedDownloadResourceReactive.CONTENT_TYPE);
FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
fail("Error inside callback!! Failed to read response data from stream!", e);
}
@Override
public void onSuccess(ByteString result) {
assertEquals(result, FeedDownloadResourceReactive.CONTENT);
}
});
streamResponse.getValue().getEntityStream().setReader(fullEntityReader);
}
use of com.linkedin.r2.transport.common.Server in project rest.li by linkedin.
the class TestInjectMockResourceFactory method testMockInjectionViaServer.
@Test
public void testMockInjectionViaServer() {
final CounterBean counter = new CounterBean();
InjectMockResourceFactory factory = new InjectMockResourceFactory(new SimpleBeanProvider().add("counterBean", counter).add("mySpecialBean", new SpecialBean()));
RestLiConfig config = new RestLiConfig();
config.addResourcePackageNames("com.linkedin.restli.server.mock");
RestLiServer server = new RestLiServer(config, factory);
server.handleRequest(new MockRequest(), new RequestContext(), new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
Assert.fail("expected success response");
}
@Override
public void onSuccess(RestResponse result) {
Assert.assertEquals(counter._counter, 1);
}
});
}
use of com.linkedin.r2.transport.common.Server in project incubator-gobblin by apache.
the class ThrottlingClientTest method test.
@Test
public void test() throws Exception {
ThrottlingPolicyFactory factory = new ThrottlingPolicyFactory();
SharedLimiterKey res1key = new SharedLimiterKey("res1");
Map<String, String> configMap = ImmutableMap.<String, String>builder().put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, ThrottlingPolicyFactory.POLICY_KEY), CountBasedPolicy.FACTORY_ALIAS).put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, CountBasedPolicy.COUNT_KEY), "50").put(BrokerConfigurationKeyGenerator.generateKey(factory, null, null, ThrottlingPolicyFactory.FAIL_ON_UNKNOWN_RESOURCE_ID), "true").build();
ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig();
guiceServletConfig.initialize(ConfigFactory.parseMap(configMap));
Injector injector = guiceServletConfig.getInjector();
EmbeddedRestliServer server = EmbeddedRestliServer.builder().resources(Lists.<Class<? extends BaseResource>>newArrayList(LimiterServerResource.class)).injector(injector).build();
try {
server.startAsync();
server.awaitRunning();
final HttpClientFactory http = new HttpClientFactory();
final Client r2Client = new TransportClientAdapter(http.getClient(Collections.<String, String>emptyMap()));
RestClient restClient = new RestClient(r2Client, server.getURIPrefix());
PermitsGetRequestBuilder getBuilder = new PermitsRequestBuilders().get();
PermitRequest res1request = new PermitRequest();
res1request.setPermits(20);
res1request.setResource(res1key.getResourceLimitedPath());
PermitAllocation allocation = getPermitAllocation(res1request, restClient, getBuilder);
Assert.assertEquals(allocation.getPermits(), new Long(20));
allocation = getPermitAllocation(res1request, restClient, getBuilder);
Assert.assertEquals(allocation.getPermits(), new Long(20));
// out of permits
try {
allocation = getPermitAllocation(res1request, restClient, getBuilder);
Assert.fail();
} catch (RestLiResponseException exc) {
Assert.assertEquals(exc.getStatus(), HttpStatus.S_403_FORBIDDEN.getCode());
}
PermitRequest invalidRequest = new PermitRequest();
invalidRequest.setPermits(20);
invalidRequest.setResource("invalidkey");
try {
allocation = getPermitAllocation(invalidRequest, restClient, getBuilder);
Assert.fail();
} catch (RestLiResponseException exc) {
Assert.assertEquals(exc.getStatus(), 422);
}
} finally {
if (server.isRunning()) {
server.stopAsync();
server.awaitTerminated();
}
}
}
use of com.linkedin.r2.transport.common.Server in project incubator-gobblin by apache.
the class EmbeddedRestliServer method startUp.
@Override
protected void startUp() throws Exception {
RestLiConfig config = new RestLiConfig();
Set<String> resourceClassNames = Sets.newHashSet();
for (Class<? extends BaseResource> resClass : this.resources) {
resourceClassNames.add(resClass.getName());
}
config.addResourceClassNames(resourceClassNames);
config.setServerNodeUri(this.serverUri);
config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler());
config.addFilter(new RestLiValidationFilter());
ResourceFactory factory = new GuiceInjectResourceFactory(this.injector);
TransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, factory));
String acceptedFilters = EncodingType.SNAPPY.getHttpName() + "," + EncodingType.GZIP.getHttpName();
FilterChain filterChain = FilterChains.createRestChain(new ServerCompressionFilter(acceptedFilters));
this.httpServer = Optional.of(new HttpNettyServerFactory(filterChain).createServer(this.port, dispatcher));
this.log.info("Starting the {} embedded server at port {}.", this.name, this.port);
this.httpServer.get().start();
}
Aggregations