@Test
public void testProxyRestriction() throws Exception {

  ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(null, null);
  Properties ambariProperties = new Properties();
  Configuration configuration = new Configuration(ambariProperties);
  Assert.assertEquals(
      Configuration.PROXY_ALLOWED_HOST_PORTS.getDefaultValue(),
      configuration.getProxyHostAndPorts());
  ViewURLStreamProvider.HostPortRestrictionHandler hprh =
      viewURLStreamProvider.new HostPortRestrictionHandler(
          configuration.getProxyHostAndPorts());
  Assert.assertFalse(hprh.proxyCallRestricted());
  Assert.assertTrue(hprh.allowProxy("host1.com", null));
  Assert.assertTrue(hprh.allowProxy(null, null));
  Assert.assertTrue(hprh.allowProxy("host1.com", " "));
  Assert.assertTrue(hprh.allowProxy("host1.com ", " "));
  Assert.assertTrue(hprh.allowProxy(" host1.com ", "8080"));
  ambariProperties = new Properties();
  ambariProperties.setProperty(Configuration.PROXY_ALLOWED_HOST_PORTS.getKey(), "");
  configuration = new Configuration(ambariProperties);
  hprh =
      viewURLStreamProvider.new HostPortRestrictionHandler(
          configuration.getProxyHostAndPorts());
  Assert.assertFalse(hprh.proxyCallRestricted());
  Assert.assertTrue(hprh.allowProxy("host1.com", null));
  Assert.assertTrue(hprh.allowProxy(null, null));
  Assert.assertTrue(hprh.allowProxy("host1.com", " "));
  Assert.assertTrue(hprh.allowProxy("host1.com ", " "));
  Assert.assertTrue(hprh.allowProxy(" host1.com ", "8080"));
  ambariProperties = new Properties();
  ambariProperties.setProperty(Configuration.PROXY_ALLOWED_HOST_PORTS.getKey(), "host1.com:*");
  configuration = new Configuration(ambariProperties);
  hprh =
      viewURLStreamProvider.new HostPortRestrictionHandler(
          configuration.getProxyHostAndPorts());
  Assert.assertTrue(hprh.proxyCallRestricted());
  Assert.assertTrue(hprh.allowProxy("host1.com", null));
  Assert.assertTrue(hprh.allowProxy(null, null));
  Assert.assertTrue(hprh.allowProxy("host1.com", "20"));
  Assert.assertFalse(hprh.allowProxy("host2.com ", " "));
  Assert.assertFalse(hprh.allowProxy(" host2.com ", "8080"));
  ambariProperties = new Properties();
  ambariProperties.setProperty(Configuration.PROXY_ALLOWED_HOST_PORTS.getKey(), " host1.com:80 ,host2.org:443, host2.org:22");
  configuration = new Configuration(ambariProperties);
  hprh =
      viewURLStreamProvider.new HostPortRestrictionHandler(
          configuration.getProxyHostAndPorts());
  Assert.assertTrue(hprh.proxyCallRestricted());
  Assert.assertTrue(hprh.allowProxy("host1.com", "80"));
  Assert.assertFalse(hprh.allowProxy("host1.com", "20"));
  Assert.assertFalse(hprh.allowProxy("host2.org", "404"));
  Assert.assertFalse(hprh.allowProxy("host2.com", "22"));
  ViewContext viewContext = createNiceMock(ViewContext.class);
  expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn(
      " host1.com:80 ,host2.org:443, host2.org:22");
  replay(viewContext);
  viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null);
  Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed("http://host1.com/tt"));
  Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed("https://host2.org/tt"));
  Assert.assertFalse(viewURLStreamProvider.isProxyCallAllowed("https://host2.org:444/tt"));
  viewContext = createNiceMock(ViewContext.class);
  expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn("c6401.ambari.apache.org:8088");
  replay(viewContext);
  viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null);
  Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed(
      "http://c6401.ambari.apache.org:8088/ws/v1/cluster/get-node-labels"));
  viewContext = createNiceMock(ViewContext.class);
  expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn("*:8088");
  replay(viewContext);
  viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null);
  Assert.assertFalse(viewURLStreamProvider.isProxyCallAllowed(
      "http://c6401.ambari.apache.org:8088/ws/v1/cluster/get-node-labels"));
  viewContext = createNiceMock(ViewContext.class);
  expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn("c6401.ambari.apache.org:*");
  replay(viewContext);
  viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null);
  Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed(
      "http://c6401.ambari.apache.org:8088/ws/v1/cluster/get-node-labels"));
  viewContext = createNiceMock(ViewContext.class);
  expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn(
      "c6401.ambari.apache.org:80,c6401.ambari.apache.org:443");
  replay(viewContext);
  viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null);
  Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed(
      "http://c6401.ambari.apache.org/ws/v1/cluster/get-node-labels"));
  Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed(
      "https://c6401.ambari.apache.org/ws/v1/cluster/get-node-labels"));
}