AssertionOptionsEndpointFilter extends AbstractOptionsEndpointFilter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { FilterInvocation fi = new FilterInvocation(request, response, chain); if (!processFilter(fi.getRequest())) { chain.doFilter(request, response); return; } try { PublicKeyCredentialRequestOptions assertionOptions = assertionOptionsProvider.getAssertionOptions(fi.getRequest(), getAuthentication()); writeResponse(fi.getResponse(), assertionOptions); } catch (RuntimeException e) { logger.debug(e); writeErrorResponse(fi.getResponse(), e); } } AssertionOptionsEndpointFilter(AssertionOptionsProvider assertionOptionsProvider, ObjectConverter objectConverter); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); static final String FILTER_URL; }
@Test public void doFilter_test() throws IOException, ServletException { AssertionOptionsProvider optionsProvider = mock(AssertionOptionsProvider.class); PublicKeyCredentialRequestOptions assertionOptions = new PublicKeyCredentialRequestOptions(null, null, null, null, null,null); when(optionsProvider.getAssertionOptions(any(), any())).thenReturn(assertionOptions); AssertionOptionsEndpointFilter optionsEndpointFilter = new AssertionOptionsEndpointFilter(optionsProvider, objectConverter); AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl(); optionsEndpointFilter.setTrustResolver(trustResolver); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI(AssertionOptionsEndpointFilter.FILTER_URL); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain filterChain = new MockFilterChain(); optionsEndpointFilter.doFilter(request, response, filterChain); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); }
@Test public void doFilter_with_unmatched_url_test() throws IOException, ServletException { AssertionOptionsProvider optionsProvider = mock(AssertionOptionsProvider.class); AssertionOptionsEndpointFilter optionsEndpointFilter = new AssertionOptionsEndpointFilter(optionsProvider, objectConverter); AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl(); optionsEndpointFilter.setTrustResolver(trustResolver); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI("/unmatched_url"); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain filterChain = new MockFilterChain(); optionsEndpointFilter.doFilter(request, response, filterChain); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); }
@Test public void doFilter_with_error_test() throws IOException, ServletException { AssertionOptionsProvider optionsProvider = mock(AssertionOptionsProvider.class); doThrow(new RuntimeException()).when(optionsProvider).getAssertionOptions(any(), any()); AssertionOptionsEndpointFilter optionsEndpointFilter = new AssertionOptionsEndpointFilter(optionsProvider, objectConverter); AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl(); optionsEndpointFilter.setTrustResolver(trustResolver); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI(AssertionOptionsEndpointFilter.FILTER_URL); MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain filterChain = new MockFilterChain(); optionsEndpointFilter.doFilter(request, response, filterChain); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.value()); }