Sincere Digest Authentication in RestEasy

I use RestEasy to develop a REST server and use the layout manager ( org.jboss.resteasy.mockMockDispatcherFactory ) to test the service in my unit tests. My service requires digest authentication, and I would do this part of my testing.

Each of my services accepts the @Context SecurityContext securityContext parameter.

Is there a way to insert a fake SecurityContext into the dispatcher so that I can verify that my security methods are functioning correctly?

+4
source share
1 answer

You must add the SecurityContext to the context data map in the ResteasyProviderFactory .

 public class SecurityContextTest { @Path("/") public static class Service { @Context SecurityContext context; @GET public String get(){ return context.getAuthenticationScheme(); } } public static class FakeSecurityContext extends ServletSecurityContext { public FakeSecurityContext() { super(null); } @Override public String getAuthenticationScheme() { return "unit-test-scheme"; } } @Test public void securityContextTest() throws Exception { Dispatcher dispatcher = MockDispatcherFactory.createDispatcher(); dispatcher.getRegistry().addSingletonResource(new Service()); ResteasyProviderFactory.getContextDataMap().put(SecurityContext.class, new FakeSecurityContext()); MockHttpRequest request = MockHttpRequest.get("/"); MockHttpResponse response = new MockHttpResponse(); dispatcher.invoke(request, response); assertEquals("unit-test-scheme", response.getContentAsString()); } } 
+2
source

Source: https://habr.com/ru/post/1343206/


All Articles