WebAppProxyServlet extends HttpServlet { @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; } WebAppProxyServlet();  static final String PROXY_USER_COOKIE_NAME; }
@Test(timeout=5000) public void testCheckHttpsStrictAndNotProvided() throws Exception { HttpServletResponse resp = Mockito.mock(HttpServletResponse.class); StringWriter sw = new StringWriter(); Mockito.when(resp.getWriter()).thenReturn(new PrintWriter(sw)); YarnConfiguration conf = new YarnConfiguration(); final URI httpLink = new URI("http: final URI httpsLink = new URI("https: 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()); 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()); 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("Was expecting an HTML page explaining that an HTTPS tracking" + " url must be used but found " + s, s.contains("HTTPS must be used")); Mockito.verify(resp, Mockito.times(1)).setContentType(MimeType.HTML); }