### Question:
HttpBasicHelper { public static String[] extractUsernameAndPasswordFromBasicHeader(final HttpServletRequest request) { String username = ""; String password = ""; final String authorizationHeader = getAuthorizationHeader(request); if (authorizationHeader != null && isBasic(authorizationHeader)) { final String base64Token = authorizationHeader.substring(HTTP_BASIC_SCHEME.length()); final String token = new String(Base64.getDecoder().decode(base64Token), StandardCharsets.UTF_8); final int delimiter = token.indexOf(':'); if (delimiter != -1) { username = token.substring(0, delimiter); password = token.substring(delimiter + 1); } } return new String[] { username, password }; } private  HttpBasicHelper(); static String[] extractUsernameAndPasswordFromBasicHeader(final HttpServletRequest request);  }### Answer:
@Test public void extractUsernameAndPasswordFromBasicHeader() { final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); final String encodedUserPassword = Base64.getEncoder().encodeToString("user:password".getBytes(StandardCharsets.UTF_8)); Mockito.when(request.getHeader("Authorization")).thenReturn("Basic " + encodedUserPassword); final String[] credentials = HttpBasicHelper.extractUsernameAndPasswordFromBasicHeader(request); assertThat(credentials).isNotNull(); assertThat(credentials[0]).isEqualTo("user"); assertThat(credentials[1]).isEqualTo("password"); assertThat(credentials[0]).isNotEqualTo(" user"); assertThat(credentials[1]).isNotEqualTo(" password"); }
@Test public void tryToExtractUsernameAndPasswordFromEmptyHeader() { final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getHeader("Authorization")).thenReturn(""); final String[] credentials = HttpBasicHelper.extractUsernameAndPasswordFromBasicHeader(request); assertThat(credentials).isNotNull(); assertThat(credentials[0]).isEqualTo(""); assertThat(credentials[1]).isEqualTo(""); }
@Test public void tryToExtractUsernameAndPasswordFromNullHeader() { final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.when(request.getHeader("Authorization")).thenReturn(null); final String[] credentials = HttpBasicHelper.extractUsernameAndPasswordFromBasicHeader(request); assertThat(credentials).isNotNull(); assertThat(credentials[0]).isEqualTo(""); assertThat(credentials[1]).isEqualTo(""); }