### Question:
BasicContentFilter extends AbstractSecureContentFilter { public Optional<AbstractAuthenticationToken> extractContent(HttpServletRequest request) { return Optional.ofNullable( request.getHeader(HttpHeaders.AUTHORIZATION) ).filter( header -> header.startsWith(ApimlConstants.BASIC_AUTHENTICATION_PREFIX) ).map( header -> header.replaceFirst(ApimlConstants.BASIC_AUTHENTICATION_PREFIX, "").trim() ) .filter(base64Credentials -> !base64Credentials.isEmpty()) .map(this::mapBase64Credentials); } BasicContentFilter(AuthenticationManager authenticationManager,
                              AuthenticationFailureHandler failureHandler,
                              ResourceAccessExceptionHandler resourceAccessExceptionHandler);  BasicContentFilter(AuthenticationManager authenticationManager,
                              AuthenticationFailureHandler failureHandler,
                              ResourceAccessExceptionHandler resourceAccessExceptionHandler,
                              String[] endpoints); Optional<AbstractAuthenticationToken> extractContent(HttpServletRequest request);  }### Answer:
@Test public void extractContentFromRequestWithValidBasicAuth() { request.addHeader(HttpHeaders.AUTHORIZATION, BASIC_AUTH); Optional<AbstractAuthenticationToken> token = basicContentFilter.extractContent(request); assertTrue(token.isPresent()); assertEquals("user", token.get().getPrincipal()); assertEquals("password", token.get().getCredentials().toString()); }
@Test public void extractContentFromRequestWithNonsenseBasicAuth() { request.addHeader(HttpHeaders.AUTHORIZATION, "Basic dXNlG4m3oFthR0n3syZA=="); Optional<AbstractAuthenticationToken> token = basicContentFilter.extractContent(request); assertTrue(token.isPresent()); assertNull(token.get().getPrincipal()); assertNull(token.get().getCredentials()); }
@Test public void extractContentFromRequestWithNonsense() { request.addHeader(HttpHeaders.AUTHORIZATION, "Duck"); Optional<AbstractAuthenticationToken> token = basicContentFilter.extractContent(request); assertFalse(token.isPresent()); }
@Test public void extractContentFromRequestWithIncompleteBasicAuth() { request.addHeader(HttpHeaders.AUTHORIZATION, "Basic dXNlcj11c2Vy"); Optional<AbstractAuthenticationToken> token = basicContentFilter.extractContent(request); assertTrue(token.isPresent()); assertNull(token.get().getPrincipal()); assertNull(token.get().getCredentials()); }
@Test public void extractContentFromRequestWithEmptyRealm() { request.addHeader(HttpHeaders.AUTHORIZATION, "Basic "); Optional<AbstractAuthenticationToken> token = basicContentFilter.extractContent(request); assertFalse(token.isPresent()); }
@Test public void extractContentFromRequestWithoutAuthHeader() { Optional<AbstractAuthenticationToken> token = basicContentFilter.extractContent(request); assertFalse(token.isPresent()); }