use of org.glassfish.jersey.server.ApplicationHandler in project jersey by jersey.
the class ApplicationFilterTest method testFilterCalledOn200.
@Test
public void testFilterCalledOn200() throws Exception {
final SimpleFilter simpleFilter = new SimpleFilter();
final ResourceConfig resourceConfig = new ResourceConfig(SimpleResource.class).register(simpleFilter);
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
final ContainerResponse response = application.apply(RequestContextBuilder.from("/simple", "GET").build()).get();
assertEquals(200, response.getStatus());
Assert.assertTrue(simpleFilter.called);
}
use of org.glassfish.jersey.server.ApplicationHandler in project jersey by jersey.
the class ApplicationFilterTest method testFilterNotCalledOn404.
@Test
public void testFilterNotCalledOn404() throws Exception {
// not found
final SimpleFilter simpleFilter = new SimpleFilter();
final ResourceConfig resourceConfig = new ResourceConfig(SimpleResource.class).register(simpleFilter);
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
final ContainerResponse response = application.apply(RequestContextBuilder.from("/NOT-FOUND", "GET").build()).get();
assertEquals(404, response.getStatus());
Assert.assertFalse(simpleFilter.called);
}
use of org.glassfish.jersey.server.ApplicationHandler in project jersey by jersey.
the class ApplicationFilterTest method testFilterExceptionHandling.
@Test
public void testFilterExceptionHandling() throws Exception {
final List<ContainerRequestFilter> requestFilterList = new ArrayList<>();
requestFilterList.add(new ExceptionFilter());
final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(requestFilterList, ContainerRequestFilter.class));
final Resource.Builder rb = Resource.builder("test");
rb.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(final ContainerRequestContext request) {
return Response.ok().build();
}
});
resourceConfig.registerResources(rb.build());
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
try {
application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus();
Assert.fail("should throw an exception");
} catch (final Exception e) {
// ok
}
}
use of org.glassfish.jersey.server.ApplicationHandler in project jersey by jersey.
the class ApplicationFilterTest method testSingleResponseFilter.
@Test
public void testSingleResponseFilter() throws Exception {
final AtomicInteger called = new AtomicInteger(0);
final List<ContainerResponseFilter> responseFilterList = new ArrayList<>();
responseFilterList.add(new ContainerResponseFilter() {
@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException {
called.incrementAndGet();
}
});
final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(responseFilterList, ContainerResponseFilter.class));
final Resource.Builder rb = Resource.builder("test");
rb.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(final ContainerRequestContext request) {
return Response.ok().build();
}
});
resourceConfig.registerResources(rb.build());
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
assertEquals(200, application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus());
assertEquals(1, called.intValue());
}
use of org.glassfish.jersey.server.ApplicationHandler in project jersey by jersey.
the class ApplicationFilterTest method testMultipleFiltersWithBindingPriority.
@Test
public void testMultipleFiltersWithBindingPriority() throws Exception {
final Filter1 filter1 = new Filter1();
final Filter10 filter10 = new Filter10();
final Filter100 filter100 = new Filter100();
filter1.setFilters(filter10, filter100);
filter10.setFilters(filter1, filter100);
filter100.setFilters(filter1, filter10);
final List<ContainerRequestFilter> requestFilterList = new ArrayList<>();
requestFilterList.add(filter100);
requestFilterList.add(filter1);
requestFilterList.add(filter10);
final ResourceConfig resourceConfig = new ResourceConfig().register(new ProviderInstanceBindingBinder<>(requestFilterList, ContainerRequestFilter.class));
final Resource.Builder rb = Resource.builder("test");
rb.addMethod("GET").handledBy(new Inflector<ContainerRequestContext, Response>() {
@Override
public Response apply(final ContainerRequestContext request) {
return Response.ok().build();
}
});
resourceConfig.registerResources(rb.build());
final ApplicationHandler application = new ApplicationHandler(resourceConfig);
assertEquals(200, application.apply(RequestContextBuilder.from("/test", "GET").build()).get().getStatus());
}
Aggregations