@Test
  public void testAutomaticTokenRefresh_refreshLimit()
      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), 5);

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

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

    try {
      registryClient.checkBlob(digest);
      Assert.fail("Should have given up refreshing after 4 attempts");
    } catch (RegistryUnauthorizedException ex) {
      Assert.assertEquals(401, ex.getHttpResponseException().getStatusCode());
      Assert.assertEquals(5, authServer.getTotalResponsesServed());
      // 1 response asking to do bearer auth + 4 unauth responses for 4 refresh attempts + 1 final
      // unauth response propagated as RegistryUnauthorizedException here
      Assert.assertEquals(6, registry.getTotalResponsesServed());
    }
  }
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