在RestEasy中模拟摘要认证

我正在使用RestEasy开发REST服务器并使用模拟调度程序(
org.jboss.resteasy.mockMockDispatcherFactory
)在我的单元测试中测试服务。我的服务需要摘要式身份验证,我会将其作为测试的一部分。 我的每个服务都接受一个
@Context SecurityContext securityContext
参数。 有没有办法在调度员中注入假的
SecurityContext
,以便我可以测试我的安全方法是否正常运行?     
已邀请:
您必须将
SecurityContext
添加到
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());
    }
}
    

要回复问题请先登录注册