### Question:
BasicAuthorizationHeaderParser { public static Credentials parse(String authorizationHeader) throws InvalidAuthorizationHeaderException { if (!authorizationHeader.toLowerCase().startsWith("basic ")) { throw new InvalidAuthorizationHeaderException("Header must start with the word 'Basic' followed by 1 space."); } int indexOfWhitespace = authorizationHeader.indexOf(" "); String rawAuthString = authorizationHeader.substring(indexOfWhitespace + 1); String decodedAuth = null; try { decodedAuth = new String(Base64.getDecoder().decode(rawAuthString.getBytes())); } catch (IllegalArgumentException e) { throw new InvalidAuthorizationHeaderException(e.getMessage()); } if (!decodedAuth.contains(":")) { throw new InvalidAuthorizationHeaderException(String.format( "The username and password should be seperated by a ':' but that character was not found in '%s'.", decodedAuth)); } int indexOfFirstColon = decodedAuth.indexOf(":"); String username = decodedAuth.substring(0, indexOfFirstColon); String password = decodedAuth.substring(indexOfFirstColon + 1); return new Credentials(username, password); }  static Credentials parse(String authorizationHeader);  }### Answer:
@Test public void parseReturnsATokenWhenTheAuthenticationStringIsValid() throws InvalidAuthorizationHeaderException { BasicAuthorizationHeaderParser.Credentials result = BasicAuthorizationHeaderParser.parse("Basic YTpi"); assertThat(result.getUsername(), is("a")); assertThat(result.getPassword(), is("b")); }
@Test public void parseSupportsColonsInPasswords() throws InvalidAuthorizationHeaderException { BasicAuthorizationHeaderParser.Credentials result = null; try { result = BasicAuthorizationHeaderParser.parse(makeHeader("user:test:password")); } finally { assertThat(result.getPassword(), is("test:password")); } }
@Test public void parseSupportsBasicIsSpelledWithLowercaseB() throws InvalidAuthorizationHeaderException { String encodedAuthString = encodeBase64(VALID_AUTH_STRING); String header = String.format("basic %s", encodedAuthString); BasicAuthorizationHeaderParser.Credentials result = BasicAuthorizationHeaderParser.parse(header); assertThat(result, is(not(nullValue()))); }
@Test public void parseSupportsBasicIsSpelledInAllUppercase() throws InvalidAuthorizationHeaderException { String encodedAuthString = encodeBase64(VALID_AUTH_STRING); String header = String.format("BASIC %s", encodedAuthString); BasicAuthorizationHeaderParser.Credentials result = BasicAuthorizationHeaderParser.parse(header); assertThat(result, is(not(nullValue()))); }
@Test public void parseRequiresTheHeaderToStartWithBasic() throws InvalidAuthorizationHeaderException { String encodedAuthString = encodeBase64(VALID_AUTH_STRING); String header = String.format("absic %s", encodedAuthString); expectedException.expect(InvalidAuthorizationHeaderException.class); BasicAuthorizationHeaderParser.parse(header); }
@Test public void parseFailsWhenTheHeaderOnlyHasTheHash() throws InvalidAuthorizationHeaderException { String encodedAuthString = encodeBase64(VALID_AUTH_STRING); String header = String.format("%s", encodedAuthString); expectedException.expect(InvalidAuthorizationHeaderException.class); BasicAuthorizationHeaderParser.parse(header); }
@Test public void parseThrowsAnInvalidAuthorizationHeaderExceptionIfTheAuthenticationStringIsInvalidBase64() throws InvalidAuthorizationHeaderException { expectedException.expect(InvalidAuthorizationHeaderException.class); BasicAuthorizationHeaderParser.parse("Basic Unencoded%AuthString"); }
@Test public void parseThrowsAnInvalidAuthorizationHeaderExceptionIfTheAuthenticationDoesNotContainAColon() throws InvalidAuthorizationHeaderException { expectedException.expect(InvalidAuthorizationHeaderException.class); BasicAuthorizationHeaderParser.parse(makeHeader("InvalidAuthString")); }