@Test
  public void testAutomaticTokenRefresh_badWwwAuthenticateResponse()
      throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException,
          RegistryException {
    String tokenResponse = "HTTP/1.1 200 OK\nContent-Length: 26\n\n{\"token\":\"awesome-token!\"}";
    authServer = new TestWebServer(false, Arrays.asList(tokenResponse), 3);

    List<String> responses =
        Arrays.asList(
            "HTTP/1.1 401 Unauthorized\nContent-Length: 0\nWWW-Authenticate: Bearer realm=\""
                + authServer.getEndpoint()
                + "\"\n\n",
            "HTTP/1.1 401 Unauthorized\nContent-Length: 0\nWWW-Authenticate: Basic realm=foo\n\n",
            "HTTP/1.1 401 Unauthorized\nContent-Length: 0\n\n",
            "HTTP/1.1 200 OK\nContent-Length: 5678\n\n");
    registry = new TestWebServer(false, responses, responses.size(), true);

    RegistryClient registryClient = createRegistryClient(null);
    Assert.assertTrue(registryClient.doPushBearerAuth());

    Optional<BlobDescriptor> digestAndSize = registryClient.checkBlob(digest);
    Assert.assertEquals(5678, digestAndSize.get().getSize());

    // Verify authServer returned bearer token three times (i.e., refreshed twice)
    Assert.assertEquals(3, authServer.getTotalResponsesServed());
    Assert.assertEquals(4, registry.getTotalResponsesServed());

    Mockito.verify(eventHandlers)
        .dispatch(
            logContains("server did not return 'WWW-Authenticate: Bearer' header. Actual: Basic"));
    Mockito.verify(eventHandlers)
        .dispatch(
            logContains("server did not return 'WWW-Authenticate: Bearer' header. Actual: null"));
  }
Tests for {@link RegistryClient}. More comprehensive tests can be found in the integration tests.
jib-core/src/test/java/com/google/cloud/tools/jib/registry/RegistryClientTest.java