use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiResponseHandler method testGetRequestCookies.
@Test
public void testGetRequestCookies() throws URISyntaxException, RestLiSyntaxException {
List<HttpCookie> cookies = Arrays.asList(new HttpCookie("cook1", "value1"), new HttpCookie("cook2", "value2"));
RestRequest request = new RestRequestBuilder(new URI("http://www.abc.org/")).setMethod("DONT_CARE").setHeaders(new TreeMap<>(String.CASE_INSENSITIVE_ORDER)).setCookies(CookieUtil.encodeCookies(cookies)).build();
ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
Assert.assertEquals(resourceContext.getRequestCookies(), cookies);
}
use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiRouting method checkResult.
private void checkResult(String uri, ProtocolVersion version, String httpMethod, String restliMethod, ResourceMethod method, Class<?> resourceClass, String methodName, boolean hasBatchKeys, String... expectedPathKeys) throws URISyntaxException, RestLiSyntaxException {
RestRequestBuilder builder = createRequestBuilder(uri, httpMethod, version);
if (restliMethod != null) {
builder.setHeader("X-RestLi-Method", restliMethod);
}
RestRequest request = builder.build();
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
ResourceMethodDescriptor methodDescriptor = _router.process(context);
assertEquals(methodDescriptor.getType(), method);
assertEquals(methodDescriptor.getResourceModel().getResourceClass(), resourceClass);
assertEquals(methodDescriptor.getMethod().getName(), methodName);
// If hasBatchKeys, there are batch keys in the context, and if not, there are none.
assertEquals(hasBatchKeys, context.getPathKeys().getBatchIds() != null);
for (String pathKey : expectedPathKeys) {
assertNotNull(context.getPathKeys().get(pathKey));
}
if (method != null) {
String expectedOperationName;
switch(method) {
case ACTION:
expectedOperationName = "action:" + methodDescriptor.getActionName();
break;
case FINDER:
expectedOperationName = "finder:" + methodDescriptor.getFinderName();
break;
case BATCH_FINDER:
expectedOperationName = "batch_finder:" + methodDescriptor.getBatchFinderName();
break;
default:
expectedOperationName = method.toString().toLowerCase();
}
assertEquals(context.getRawRequestContext().getLocalAttr(R2Constants.OPERATION), expectedOperationName);
}
}
use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiRouting method testRoutingDetailsAssociationBatchGet.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsAssociationBatch")
public void testRoutingDetailsAssociationBatchGet(ProtocolVersion version, String uri) throws Exception {
Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(FollowsAssociativeResource.class);
_router = new RestLiRouter(pathRootResourceMap, new RestLiConfig());
RestRequest request = createRequest(uri, "GET", version);
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
ResourceMethodDescriptor resourceMethodDescriptor = _router.process(context);
assertNotNull(resourceMethodDescriptor);
assertEquals(resourceMethodDescriptor.getType(), ResourceMethod.BATCH_GET);
assertNull(resourceMethodDescriptor.getActionName());
assertNull(resourceMethodDescriptor.getFinderName());
assertEquals(resourceMethodDescriptor.getMethod().getDeclaringClass(), FollowsAssociativeResource.class);
assertEquals(resourceMethodDescriptor.getMethod().getName(), "batchGet");
assertEquals(resourceMethodDescriptor.getMethod().getParameterTypes(), new Class<?>[] { Set.class });
assertEquals(resourceMethodDescriptor.getResourceModel().getName(), "follows");
PathKeys keys = context.getPathKeys();
assertNull(keys.getAsString("followerID"));
assertNull(keys.getAsString("followeeID"));
CompoundKey key1 = new CompoundKey();
key1.append("followerID", 1L);
key1.append("followeeID", 2L);
CompoundKey key2 = new CompoundKey();
key2.append("followerID", 3L);
key2.append("followeeID", 4L);
Set<CompoundKey> expectedBatchKeys = new HashSet<>();
expectedBatchKeys.add(key1);
expectedBatchKeys.add(key2);
assertEquals(keys.getBatchIds().size(), 2);
for (CompoundKey batchKey : keys.<CompoundKey>getBatchIds()) {
assertTrue(expectedBatchKeys.contains(batchKey));
expectedBatchKeys.remove(batchKey);
}
assertEquals(expectedBatchKeys.size(), 0);
}
use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiRouting method expectRoutingExceptionWithStatus.
private void expectRoutingExceptionWithStatus(String uri, ProtocolVersion version, String httpMethod, String restliMethod, HttpStatus status) throws URISyntaxException {
RestRequestBuilder builder = createRequestBuilder(uri, httpMethod, version);
if (restliMethod != null) {
builder.setHeader("X-RestLi-Method", restliMethod);
}
RestRequest request = builder.build();
try {
try {
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
ResourceMethodDescriptor method = _router.process(context);
fail("Expected RoutingException, got: " + method.toString());
} catch (RestLiSyntaxException e) {
throw new RoutingException(e.getMessage(), HttpStatus.S_400_BAD_REQUEST.getCode());
}
} catch (RoutingException e) {
// expected a certain httpStatus code
assertEquals(e.getStatus(), status.getCode());
}
}
use of com.linkedin.restli.internal.server.ResourceContextImpl in project rest.li by linkedin.
the class TestRestLiRouting method testRoutingDetailsSimplePartialUpdate.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "routingDetailsSimple")
public void testRoutingDetailsSimplePartialUpdate(ProtocolVersion version, String uri) throws Exception {
Map<String, ResourceModel> pathRootResourceMap = buildResourceModels(TrendingResource.class);
_router = new RestLiRouter(pathRootResourceMap, new RestLiConfig());
RestRequest request = createRequest(uri, "POST", version);
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
ResourceMethodDescriptor resourceMethodDescriptor = _router.process(context);
assertNotNull(resourceMethodDescriptor);
assertEquals(resourceMethodDescriptor.getType(), ResourceMethod.PARTIAL_UPDATE);
assertNull(resourceMethodDescriptor.getActionName());
assertNull(resourceMethodDescriptor.getFinderName());
assertEquals(resourceMethodDescriptor.getMethod().getDeclaringClass(), TrendingResource.class);
assertEquals(resourceMethodDescriptor.getMethod().getName(), "update");
assertEquals(resourceMethodDescriptor.getMethod().getParameterTypes(), new Class<?>[] { PatchRequest.class });
assertEquals(resourceMethodDescriptor.getResourceModel().getName(), "trending");
PathKeys keys = context.getPathKeys();
assertNull(keys.getBatchIds());
}
Aggregations