Repository: GoogleContainerTools/jib
File: jib-core/src/test/java/com/google/cloud/tools/jib/http/FailoverHttpClientTest.java (Java)

Code before:
        "Cannot verify server at https://url:2. Attempting again with no TLS verification.");
  }

  @Test
  public void testRetries() throws IOException {
    final HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 0), 64);
    final AtomicReference<HttpHandler> current = new AtomicReference<>();
    server.createContext("/").setHandler(exchange -> current.get().handle(exchange));
    final AtomicBoolean failed = new AtomicBoolean();
    final List<LogEvent> events = new ArrayList<>();

    // simulate a failure
    current.set(ex -> {
      if (failed.compareAndSet(false, true)) {
        // simulate a success after this (first) failure
        current.set(exch -> {
          exch.sendResponseHeaders(200, 0);
          exch.close();
        });

        // here is the failure - no response sent (IOException for the client)
        ex.close();
        return;
      }
      // make the test fail
      ex.sendResponseHeaders(123, 0);
      ex.close();
    });

    try {
      server.start();
      assertEquals(
              200,
              new FailoverHttpClient(true, true, events::add)
                      .post(new URL("http://localhost:" + server.getAddress().getPort() + "/test"),
                              Request.builder()
                                      .setBody(new BlobHttpContent(Blobs.from("foo"), "text/plain"))
                                      .build())
                      .getStatusCode());
    } finally {
      server.stop(0);
    }
    assertTrue(failed.get());
    assertEquals(1, events.size());

    final LogEvent warn = events.iterator().next();
    assertEquals(LogEvent.Level.WARN, warn.getLevel());
    assertEquals(
            "POST http://localhost:" + server.getAddress().getPort() + "/test failed and will be retried",
            warn.getMessage());
  }

Review comment (bug):
I don't think this works as you intended. 123 actually causes IOException. OTOH, 400 works.

There are some minor style errors, and given above, I'd have the code to look like this: 
```java
import static com.google.common.truth.Truth.assertThat;

public void testRetries() throws IOException {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 1);
    AtomicBoolean failed = new AtomicBoolean();
    server
        .createContext("/")
        .setHandler(
            exchange ->
                exchange.sendResponseHeaders(failed.compareAndSet(false, true) ? 123 : 200, 0));
    try {
      server.start();
      int port = server.getAddress().getPort();
      List<LogEvent> events = new ArrayList<>();
      int returnCode =
          new FailoverHttpClient(true, true, events::add)
              .get(new URL("http://localhost:" + port), Request.builder().build())
              .getStatusCode();
      assertThat(returnCode).isEqualTo(200);
      assertThat(failed.get()).isTrue();
      assertThat(events)
          .containsExactly(
              LogEvent.warn("GET http://localhost:" + port + " failed and will be retried"));
    } finally {
      server.stop(0);
    }
}
```

Code after:
        "Cannot verify server at https://url:2. Attempting again with no TLS verification.");
  }

  @Test
  public void testRetries() throws IOException {
    final HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 0), 64);
    final AtomicReference<HttpHandler> current = new AtomicReference<>();
    server.createContext("/").setHandler(exchange -> current.get().handle(exchange));
    final AtomicBoolean failed = new AtomicBoolean();
    final List<LogEvent> events = new ArrayList<>();

    // simulate a failure
    current.set(
        ex -> {
          if (failed.compareAndSet(false, true)) {
            // simulate a success after this (first) failure
            current.set(
                exch -> {
                  exch.sendResponseHeaders(200, 0);
                  exch.close();
                });

            // here is the failure - no response sent (IOException for the client)
            ex.close();
            return;
          }
          // make the test fail
          ex.sendResponseHeaders(423, 0);
          ex.close();
        });

    try {
      server.start();
      assertEquals(
          200,
          new FailoverHttpClient(true, true, events::add)
              .post(
                  new URL("http://localhost:" + server.getAddress().getPort() + "/test"),
                  Request.builder()
                      .setBody(new BlobHttpContent(Blobs.from("foo"), "text/plain"))
                      .build())
              .getStatusCode());
    } finally {
      server.stop(0);
    }
    assertTrue(failed.get());
    assertEquals(1, events.size());

    final LogEvent warn = events.iterator().next();
    assertEquals(LogEvent.Level.WARN, warn.getLevel());
    assertEquals(