@VisibleForTesting
  static boolean checkHttpsStrictAndNotProvided(
      HttpServletResponse resp, URI link, YarnConfiguration conf)
      throws IOException {
    String httpsPolicy = conf.get(
        YarnConfiguration.RM_APPLICATION_HTTPS_POLICY,
        YarnConfiguration.DEFAULT_RM_APPLICATION_HTTPS_POLICY);
    boolean required = httpsPolicy.equals("STRICT");
    boolean provided = link.getScheme().equals("https");
    if (required && !provided) {
      resp.setContentType(MimeType.HTML);
      Page p = new Page(resp.getWriter());
      p.html().
          h1("HTTPS must be used").
          h3().
          __(YarnConfiguration.RM_APPLICATION_HTTPS_POLICY,
              "is set to STRICT, which means that the tracking URL ",
              "must be an HTTPS URL, but it is not.").
          __("The tracking URL is: ", link).
          __().
          __();
      return true;
    }
    return false;
  }
@Test
  @Timeout(5000)
  void testCheckHttpsStrictAndNotProvided() throws Exception {
    HttpServletResponse resp = mock(HttpServletResponse.class);
    StringWriter sw = new StringWriter();
    when(resp.getWriter()).thenReturn(new PrintWriter(sw));
    YarnConfiguration conf = new YarnConfiguration();
    final URI httpLink = new URI("http://foo.com");
    final URI httpsLink = new URI("https://foo.com");

    // NONE policy
    conf.set(YarnConfiguration.RM_APPLICATION_HTTPS_POLICY, "NONE");
    assertFalse(WebAppProxyServlet.checkHttpsStrictAndNotProvided(
        resp, httpsLink, conf));
    assertEquals("", sw.toString());
    Mockito.verify(resp, Mockito.times(0)).setContentType(Mockito.any());
    assertFalse(WebAppProxyServlet.checkHttpsStrictAndNotProvided(
        resp, httpLink, conf));
    assertEquals("", sw.toString());
    Mockito.verify(resp, Mockito.times(0)).setContentType(Mockito.any());

    // LENIENT policy
    conf.set(YarnConfiguration.RM_APPLICATION_HTTPS_POLICY, "LENIENT");
    assertFalse(WebAppProxyServlet.checkHttpsStrictAndNotProvided(
        resp, httpsLink, conf));
    assertEquals("", sw.toString());
    Mockito.verify(resp, Mockito.times(0)).setContentType(Mockito.any());
    assertFalse(WebAppProxyServlet.checkHttpsStrictAndNotProvided(
        resp, httpLink, conf));
    assertEquals("", sw.toString());
    Mockito.verify(resp, Mockito.times(0)).setContentType(Mockito.any());

    // STRICT policy
    conf.set(YarnConfiguration.RM_APPLICATION_HTTPS_POLICY, "STRICT");
    assertFalse(WebAppProxyServlet.checkHttpsStrictAndNotProvided(
        resp, httpsLink, conf));
    assertEquals("", sw.toString());
    Mockito.verify(resp, Mockito.times(0)).setContentType(Mockito.any());
    assertTrue(WebAppProxyServlet.checkHttpsStrictAndNotProvided(
        resp, httpLink, conf));
    String s = sw.toString();
    assertTrue(s.contains("HTTPS must be used"),
        "Was expecting an HTML page explaining that an HTTPS tracking"
            + " url must be used but found " + s);
    Mockito.verify(resp, Mockito.times(1)).setContentType(MimeType.HTML);
  }