### Question:
RegistryAuthenticator { static Optional<RegistryAuthenticator> fromAuthenticationMethod( String authenticationMethod, RegistryEndpointRequestProperties registryEndpointRequestProperties, @Nullable String userAgent, FailoverHttpClient httpClient) throws RegistryAuthenticationFailedException { if (authenticationMethod.matches("^(?i)(basic) .*")) { return Optional.empty(); } String registryUrl = registryEndpointRequestProperties.getServerUrl(); String imageName = registryEndpointRequestProperties.getImageName(); if (!authenticationMethod.matches("^(?i)(bearer) .*")) { throw newRegistryAuthenticationFailedException( registryUrl, imageName, authenticationMethod, "Bearer"); } Pattern realmPattern = Pattern.compile("realm=\"(.*?)\""); Matcher realmMatcher = realmPattern.matcher(authenticationMethod); if (!realmMatcher.find()) { throw newRegistryAuthenticationFailedException( registryUrl, imageName, authenticationMethod, "realm"); } String realm = realmMatcher.group(1); Pattern servicePattern = Pattern.compile("service=\"(.*?)\""); Matcher serviceMatcher = servicePattern.matcher(authenticationMethod); String service = serviceMatcher.find() ? serviceMatcher.group(1) : registryUrl; return Optional.of( new RegistryAuthenticator( realm, service, registryEndpointRequestProperties, userAgent, httpClient)); } private  RegistryAuthenticator(
      String realm,
      String service,
      RegistryEndpointRequestProperties registryEndpointRequestProperties,
      @Nullable String userAgent,
      FailoverHttpClient httpClient); Authorization authenticatePull(@Nullable Credential credential); Authorization authenticatePush(@Nullable Credential credential);  }### Answer:
@Test public void testFromAuthenticationMethod_basic() throws RegistryAuthenticationFailedException { Assert.assertFalse( RegistryAuthenticator.fromAuthenticationMethod( "Basic realm=\"https: registryEndpointRequestProperties, "user-agent", httpClient) .isPresent()); Assert.assertFalse( RegistryAuthenticator.fromAuthenticationMethod( "BASIC realm=\"https: registryEndpointRequestProperties, "user-agent", httpClient) .isPresent()); Assert.assertFalse( RegistryAuthenticator.fromAuthenticationMethod( "bASIC realm=\"https: registryEndpointRequestProperties, "user-agent", httpClient) .isPresent()); }
@Test public void testFromAuthenticationMethod_noBearer() { try { RegistryAuthenticator.fromAuthenticationMethod( "realm=\"https: registryEndpointRequestProperties, "user-agent", httpClient); Assert.fail("Authentication method without 'Bearer ' or 'Basic ' should fail"); } catch (RegistryAuthenticationFailedException ex) { Assert.assertEquals( "Failed to authenticate with registry someserver/someimage because: 'Bearer' was not found in the 'WWW-Authenticate' header, tried to parse: realm=\"https: ex.getMessage()); } }
@Test public void testFromAuthenticationMethod_noRealm() { try { RegistryAuthenticator.fromAuthenticationMethod( "Bearer scope=\"somescope\"", registryEndpointRequestProperties, "user-agent", httpClient); Assert.fail("Authentication method without 'realm' should fail"); } catch (RegistryAuthenticationFailedException ex) { Assert.assertEquals( "Failed to authenticate with registry someserver/someimage because: 'realm' was not found in the 'WWW-Authenticate' header, tried to parse: Bearer scope=\"somescope\"", ex.getMessage()); } }