void _test(String type, String value) throws ExecutionException, InterruptedException { assertEquals( "content", apply( RequestContextBuilder.from("/", "GET") .accept("application/" + type) .header(type, value) .build()) .getEntity()); assertEquals( "content", apply( RequestContextBuilder.from("/wrappers", "GET") .accept("application/" + type) .header(type, value) .build()) .getEntity()); assertEquals( "content", apply( RequestContextBuilder.from("/list", "GET") .accept("application/" + type) .header(type, value) .header(type, value) .header(type, value) .build()) .getEntity()); }
void _testDefault(String base, String type, String value) throws ExecutionException, InterruptedException { assertEquals( "content", apply( RequestContextBuilder.from(base + "default/null", "GET") .accept("application/" + type) .build()) .getEntity()); assertEquals( "content", apply( RequestContextBuilder.from(base + "default", "GET") .accept("application/" + type) .build()) .getEntity()); assertEquals( "content", apply( RequestContextBuilder.from(base + "default/override", "GET") .accept("application/" + type) .header(type, value) .build()) .getEntity()); }
@Test public void testPathSegsEndList() throws Exception { initiateWebApplication(PathSegsEndList.class); assertEquals( "xyz", app.apply(RequestContextBuilder.from("/x/y/z", "GET").build()).get().getEntity()); assertEquals( "xyz/", app.apply(RequestContextBuilder.from("/x/y/z/", "GET").build()).get().getEntity()); }
@Test public void testSubResourceMethodsWithDifferentTemplates() throws Exception { app = createApplication(SubResourceMethodsWithDifferentTemplates.class); assertEquals( "foo", app.apply(RequestContextBuilder.from("/foo", "GET").build()).get().getEntity()); assertEquals( "bar", app.apply(RequestContextBuilder.from("/bar", "POST").build()).get().getEntity()); }
@Test public void testSubResourceMethodWithSameTemplate() throws Exception { app = createApplication(SubResourceWithSameTemplate.class); assertEquals( "FOO", app.apply(RequestContextBuilder.from("/foo", "GET").build()).get().getEntity()); assertEquals( "BAR", app.apply(RequestContextBuilder.from("/foo/bar", "GET").build()).get().getEntity()); }
@Test public void testDuplicate() throws Exception { initiateWebApplication(Duplicate.class); assertEquals( "foo", app.apply(RequestContextBuilder.from("/foo", "GET").build()).get().getEntity()); assertEquals( "bar", app.apply(RequestContextBuilder.from("/foo/bar", "GET").build()).get().getEntity()); }
@Test public void testSubResourceCapturingGroups() throws Exception { app = createApplication(SubResourceExplicitRegexCapturingGroups.class); assertEquals( "123", app.apply(RequestContextBuilder.from("/123", "GET").build()).get().getEntity()); assertEquals( "123-456-789", app.apply(RequestContextBuilder.from("/123-456-789", "GET").build()).get().getEntity()); }
@Test public void testSubResourceMethods() throws Exception { app = createApplication(SubResourceMethods.class); assertEquals("/", app.apply(RequestContextBuilder.from("/", "GET").build()).get().getEntity()); assertEquals( "/sub", app.apply(RequestContextBuilder.from("/sub", "GET").build()).get().getEntity()); assertEquals( "/sub/sub", app.apply(RequestContextBuilder.from("/sub/sub", "GET").build()).get().getEntity()); }
@Test public void testSubResourceNoSlashMethodWithLimitedTemplate() throws Exception { app = createApplication(SubResourceNoSlashMethodWithLimitedTemplate.class); assertEquals( "topone", app.apply(RequestContextBuilder.from("/top?id=one", "GET").build()).get().getEntity()); assertEquals( "a/b/c/d", app.apply(RequestContextBuilder.from("/top/a/b/c/d", "GET").build()).get().getEntity()); }
@Test public void testSubResourceXXX() throws Exception { app = createApplication(SubResourceXXX.class); assertEquals( "123", app.apply(RequestContextBuilder.from("/123/literal", "GET").build()).get().getEntity()); assertEquals( "123literal789", app.apply(RequestContextBuilder.from("/123/literal/789", "GET").build()).get().getEntity()); }
@Test public void testPathSeg() throws Exception { initiateWebApplication(PathSeg.class); assertEquals( "content", app.apply(RequestContextBuilder.from("/a-b/c-d", "GET").build()).get().getEntity()); assertEquals( "sub-content", app.apply(RequestContextBuilder.from("/a-b/c-d/e-f", "GET").build()).get().getEntity()); }
@Test public void testConsumeWildCardBean() throws Exception { ApplicationHandler app = createApplication(ConsumeWildCardBean.class); assertEquals( "HTML", app.apply(RequestContextBuilder.from("/a/b", "POST").entity("").type("text/html").build()) .get() .getEntity()); assertEquals( "XHTML", app.apply(RequestContextBuilder.from("/a/b", "POST").entity("").type("text/xhtml").build()) .get() .getEntity()); }
private void _test(ApplicationHandler handler, String requestUri, String expected) throws InterruptedException, ExecutionException { final ContainerResponse response = handler.apply(RequestContextBuilder.from(requestUri, "GET").build()).get(); assertEquals(200, response.getStatus()); assertEquals(expected, response.getEntity()); }
@Test public void testStringArgsGet() throws Exception { initiateWebApplication(Resource.class); assertEquals( "content", app.apply(RequestContextBuilder.from("/a/b/c", "GET").build()).get().getEntity()); }
@Test public void testPathSegsList() throws Exception { initiateWebApplication(PathSegsList.class); assertEquals( "xyz-b", app.apply(RequestContextBuilder.from("/x/y/z/edit/b", "GET").build()).get().getEntity()); assertEquals( "//xyz-b", app.apply( RequestContextBuilder.from( "http://localhost/", "http://localhost///x/y/z/edit/b", "GET") .build()) .get() .getEntity()); }
@Test public void testLazyConverter() throws Exception { final ApplicationHandler application = new ApplicationHandler(new ResourceConfig(MyLazyParamProvider.class, Resource.class)); final ContainerResponse response = application.apply(RequestContextBuilder.from("/resource", "GET").build()).get(); assertEquals(400, response.getStatus()); }
@Test public void testSubResources() throws Exception { initiateWebApplication(Root.class); assertEquals( "1234567", app.apply(RequestContextBuilder.from("/1/2/3/4/5/6/7", "GET").build()).get().getEntity()); }
@Test public void testClientAndServerProvider2() throws ExecutionException, InterruptedException { final ResourceConfig resourceConfig = new ResourceConfig(Resource.class, MyServerAndClientContrainedToClientFilter.class); ApplicationHandler handler = new ApplicationHandler(resourceConfig); final ContainerResponse response = handler.apply(RequestContextBuilder.from("/resource", "GET").build()).get(); Assert.assertEquals(200, response.getStatus()); }
@Test public void testSubResource() throws Exception { app = createApplication(SubResourceExplicitRegex.class); assertEquals( "segments: foo", app.apply(RequestContextBuilder.from("/foo", "GET").build()).get().getEntity()); assertEquals( "segments: foo/bar", app.apply(RequestContextBuilder.from("/foo/bar", "GET").build()).get().getEntity()); assertEquals( "digit: 123", app.apply(RequestContextBuilder.from("/digit/123", "GET").build()).get().getEntity()); assertEquals( "anything: foo", app.apply(RequestContextBuilder.from("/digit/foo", "GET").build()).get().getEntity()); }
@Test public void testGetResource() throws Exception { ApplicationHandler app = createApplication(ResourceA.class, ResourceB.class); assertEquals( "B: c", app.apply(RequestContextBuilder.from("/a/b/c", "GET").build()).get().getEntity()); assertEquals( "SR: foo", app.apply(RequestContextBuilder.from("/a/foo", "GET").build()).get().getEntity()); assertEquals( "null", app.apply(RequestContextBuilder.from("/a/is-null", "GET").build()).get().getEntity()); assertEquals( 404, app.apply(RequestContextBuilder.from("/a/non-instantiable", "GET").build()) .get() .getStatus()); }
@Test public void testBadPrimitiveWrapperValue() throws ExecutionException, InterruptedException { final ContainerResponse responseContext = apply( RequestContextBuilder.from("/wrappers", "GET") .accept("application/int") .header("int", "abcdef") .build()); assertEquals(400, responseContext.getStatus()); }
@Test public void testInvalidSubResource() throws ExecutionException, InterruptedException { ApplicationHandler handler = new ApplicationHandler(new ResourceConfig(WrongResource.class)); _test(handler, "/wrong", "ok"); try { final ContainerResponse response = handler.apply(RequestContextBuilder.from("/wrong/locator", "GET").build()).get(); assertEquals(500, response.getStatus()); fail("Should throw exception caused by validation errors of Sub Resource."); } catch (Throwable e) { // ok - Should throw exception caused by validation errors of Sub Resource. } }
@Test public void testResourceAndProviderConstrainedToServer() throws ExecutionException, InterruptedException { final ResourceConfig resourceConfig = new ResourceConfig(ResourceAndProviderConstrainedToServer.class); ApplicationHandler handler = new ApplicationHandler(resourceConfig); final ContainerResponse response = handler .apply(RequestContextBuilder.from("/resource-and-provider-server", "GET").build()) .get(); Assert.assertEquals(200, response.getStatus()); Assert.assertEquals( "called", response.getHeaderString("ResourceAndProviderConstrainedToServer")); }
@Test public void testSubResourceMethodsWithTemplates() throws Exception { app = createApplication(SubResourceMethodsWithTemplates.class); assertEquals("/", app.apply(RequestContextBuilder.from("/", "GET").build()).get().getEntity()); assertEquals( "value", app.apply(RequestContextBuilder.from("/subvalue", "GET").build()).get().getEntity()); assertEquals( "a", app.apply(RequestContextBuilder.from("/sub/a", "GET").build()).get().getEntity()); assertEquals( "value/a", app.apply(RequestContextBuilder.from("/subunlimitedvalue/a", "GET").build()) .get() .getEntity()); assertEquals( "a/b/c/d", app.apply(RequestContextBuilder.from("/subunlimited/a/b/c/d", "GET").build()) .get() .getEntity()); }
@Test public void testFiltersAnnotated() throws ExecutionException, InterruptedException { final ResourceConfig resourceConfig = new ResourceConfig( MyServerFilter.class, MyClientFilter.class, MyServerWrongFilter.class, MyServerFilterWithoutConstraint.class, Resource.class); resourceConfig.registerInstances(new MyServerWrongFilter2(), new MyServerFilter2()); ApplicationHandler handler = new ApplicationHandler(resourceConfig); final ContainerResponse response = handler.apply(RequestContextBuilder.from("/resource", "GET").build()).get(); Assert.assertEquals("called", response.getHeaderString("MyServerFilter")); Assert.assertEquals("called", response.getHeaderString("MyServerFilter2")); Assert.assertEquals("called", response.getHeaderString("MyServerFilterWithoutConstraint")); }
@Test public void testSpecificApplication() throws ExecutionException, InterruptedException { Application app = new Application() { @Override public Set<Class<?>> getClasses() { final HashSet<Class<?>> classes = Sets.newHashSet(); classes.add(Resource.class); classes.add(MyClientFilter.class); classes.add(MyServerWrongFilter.class); return classes; } }; ApplicationHandler handler = new ApplicationHandler(app); final ContainerResponse response = handler.apply(RequestContextBuilder.from("/resource", "GET").build()).get(); Assert.assertEquals(200, response.getStatus()); }
public String test(HttpMethodOverrideFilter.Source... sources) { ResourceConfig rc = new ResourceConfig(Resource.class); HttpMethodOverrideFilter.enableFor(rc, sources); ApplicationHandler handler = new ApplicationHandler(rc); try { ContainerResponse response = handler .apply( RequestContextBuilder.from("", "/?_method=DELETE", "POST") .header("X-HTTP-Method-Override", "PUT") .build()) .get(); if (Response.Status.OK.equals(response.getStatusInfo())) { return (String) response.getEntity(); } else { return "" + response.getStatus(); } } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } }
private UriRoutingContext createContext(String appBaseUri, String requestUri, String method) { return new UriRoutingContext( RequestContextBuilder.from(appBaseUri, requestUri, method).build()); }
@Test public void testPathSegOnSubResource() throws Exception { initiateWebApplication(PathSegOnSubResource.class); assertEquals("x", app.apply(RequestContextBuilder.from("/x", "GET").build()).get().getEntity()); }