2011-03-10 3 views
4

RestEasy를 사용하여 REST 서버를 개발하고 유닛 테스트에서 서비스를 테스트하기 위해 mock 디스패처 (org.jboss.resteasy.mockMockDispatcherFactory)를 사용하고 있습니다. 내 서비스에는 다이제스트 인증이 필요하며 테스트의 일부로 사용해야합니다.RestEasy에서 조롱 다이제스트 인증

내 서비스는 각각 @Context SecurityContext securityContext 매개 변수를 허용합니다.

배송 방법에 가짜 SecurityContext을 삽입하는 방법이 있습니까? 그렇다면 보안 방법이 제대로 작동하는지 테스트 할 수 있습니까?

답변

2

SecurityContextResteasyProviderFactory의 컨텍스트 데이터 맵에 추가해야합니다.

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()); 
    } 
} 
관련 문제