### Question:
AuthenticationMethodRetriever implements RegistryEndpointProvider<Optional<RegistryAuthenticator>> { @Override public Optional<RegistryAuthenticator> handleHttpResponseException( ResponseException responseException) throws ResponseException, RegistryErrorException { if (responseException.getStatusCode() != HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) { throw responseException; } String authenticationMethod = responseException.getHeaders().getAuthenticate(); if (authenticationMethod == null) { throw new RegistryErrorExceptionBuilder(getActionDescription(), responseException) .addReason("'WWW-Authenticate' header not found") .build(); } try { return RegistryAuthenticator.fromAuthenticationMethod( authenticationMethod, registryEndpointRequestProperties, userAgent, httpClient); } catch (RegistryAuthenticationFailedException ex) { throw new RegistryErrorExceptionBuilder(getActionDescription(), ex) .addReason("Failed get authentication method from 'WWW-Authenticate' header") .build(); } } AuthenticationMethodRetriever(
      RegistryEndpointRequestProperties registryEndpointRequestProperties,
      @Nullable String userAgent,
      FailoverHttpClient httpClient); @Nullable @Override BlobHttpContent getContent(); @Override List<String> getAccept(); @Override Optional<RegistryAuthenticator> handleResponse(Response response); @Override URL getApiRoute(String apiRouteBase); @Override String getHttpMethod(); @Override String getActionDescription(); @Override Optional<RegistryAuthenticator> handleHttpResponseException(
      ResponseException responseException);  }### Answer:
@Test public void testHandleHttpResponseException_invalidStatusCode() throws RegistryErrorException { Mockito.when(mockResponseException.getStatusCode()).thenReturn(-1); try { testAuthenticationMethodRetriever.handleHttpResponseException(mockResponseException); Assert.fail( "Authentication method retriever should only handle HTTP 401 Unauthorized errors"); } catch (ResponseException ex) { Assert.assertEquals(mockResponseException, ex); } }
@Test public void testHandleHttpResponseException_noHeader() throws ResponseException { Mockito.when(mockResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED); Mockito.when(mockResponseException.getHeaders()).thenReturn(mockHeaders); Mockito.when(mockHeaders.getAuthenticate()).thenReturn(null); try { testAuthenticationMethodRetriever.handleHttpResponseException(mockResponseException); Assert.fail( "Authentication method retriever should fail if 'WWW-Authenticate' header is not found"); } catch (RegistryErrorException ex) { MatcherAssert.assertThat( ex.getMessage(), CoreMatchers.containsString("'WWW-Authenticate' header not found")); } }
@Test public void testHandleHttpResponseException_badAuthenticationMethod() throws ResponseException { String authenticationMethod = "bad authentication method"; Mockito.when(mockResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED); Mockito.when(mockResponseException.getHeaders()).thenReturn(mockHeaders); Mockito.when(mockHeaders.getAuthenticate()).thenReturn(authenticationMethod); try { testAuthenticationMethodRetriever.handleHttpResponseException(mockResponseException); Assert.fail( "Authentication method retriever should fail if 'WWW-Authenticate' header failed to parse"); } catch (RegistryErrorException ex) { MatcherAssert.assertThat( ex.getMessage(), CoreMatchers.containsString( "Failed get authentication method from 'WWW-Authenticate' header")); } }
@Test public void testHandleHttpResponseException_pass() throws RegistryErrorException, ResponseException, MalformedURLException { String authenticationMethod = "Bearer realm=\"https: Mockito.when(mockResponseException.getStatusCode()) .thenReturn(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED); Mockito.when(mockResponseException.getHeaders()).thenReturn(mockHeaders); Mockito.when(mockHeaders.getAuthenticate()).thenReturn(authenticationMethod); RegistryAuthenticator registryAuthenticator = testAuthenticationMethodRetriever.handleHttpResponseException(mockResponseException).get(); Assert.assertEquals( new URL("https: registryAuthenticator.getAuthenticationUrl( null, Collections.singletonMap("someImageName", "someScope"))); }