### Question:
KeycloakAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { HttpFacade facade = new SimpleHttpFacade(request, response); if (apiRequestMatcher.matches(request) || adapterDeploymentContext.resolveDeployment(facade).isBearerOnly()) { commenceUnauthorizedResponse(request, response); } else { commenceLoginRedirect(request, response); } } KeycloakAuthenticationEntryPoint(AdapterDeploymentContext adapterDeploymentContext);  KeycloakAuthenticationEntryPoint(AdapterDeploymentContext adapterDeploymentContext, RequestMatcher apiRequestMatcher); @Override void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException); void setLoginUri(String loginUri); void setRealm(String realm); static final String DEFAULT_LOGIN_URI; }### Answer:
@Test public void testCommenceWithRedirect() throws Exception { configureBrowserRequest(); authenticationEntryPoint.commence(request, response, null); assertEquals(HttpStatus.FOUND.value(), response.getStatus()); assertEquals(KeycloakAuthenticationEntryPoint.DEFAULT_LOGIN_URI, response.getHeader("Location")); }
@Test public void testCommenceWithRedirectNotRootContext() throws Exception { configureBrowserRequest(); String contextPath = "/foo"; request.setContextPath(contextPath); authenticationEntryPoint.commence(request, response, null); assertEquals(HttpStatus.FOUND.value(), response.getStatus()); assertEquals(contextPath + KeycloakAuthenticationEntryPoint.DEFAULT_LOGIN_URI, response.getHeader("Location")); }
@Test public void testCommenceWithUnauthorizedWithAccept() throws Exception { request.addHeader(HttpHeaders.ACCEPT, "application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); authenticationEntryPoint.commence(request, response, null); assertEquals(HttpStatus.FOUND.value(), response.getStatus()); assertNull(response.getHeader(HttpHeaders.WWW_AUTHENTICATE)); }
@Test public void testCommenceWithCustomRequestMatcher() throws Exception { new KeycloakAuthenticationEntryPoint(adapterDeploymentContext, requestMatcher) .commence(request, response, null); verify(requestMatcher).matches(request); }