Ejemplo n.º 1
0
        /// <summary>Test AmIpFilter</summary>
        /// <exception cref="System.Exception"/>
        public virtual void TestFilter()
        {
            IDictionary <string, string> @params = new Dictionary <string, string>();

            @params[AmIpFilter.ProxyHost]    = proxyHost;
            @params[AmIpFilter.ProxyUriBase] = proxyUri;
            FilterConfig config = new TestAmFilter.DummyFilterConfig(@params);
            // dummy filter
            FilterChain chain      = new _FilterChain_135(this);
            AmIpFilter  testFilter = new AmIpFilter();

            testFilter.Init(config);
            TestAmFilter.HttpServletResponseForTest response = new TestAmFilter.HttpServletResponseForTest
                                                                   (this);
            // Test request should implements HttpServletRequest
            ServletRequest failRequest = Org.Mockito.Mockito.Mock <ServletRequest>();

            try
            {
                testFilter.DoFilter(failRequest, response, chain);
                NUnit.Framework.Assert.Fail();
            }
            catch (ServletException e)
            {
                NUnit.Framework.Assert.AreEqual(ProxyUtils.EHttpHttpsOnly, e.Message);
            }
            // request with HttpServletRequest
            HttpServletRequest request = Org.Mockito.Mockito.Mock <HttpServletRequest>();

            Org.Mockito.Mockito.When(request.GetRemoteAddr()).ThenReturn("redirect");
            Org.Mockito.Mockito.When(request.GetRequestURI()).ThenReturn("/redirect");
            testFilter.DoFilter(request, response, chain);
            // address "redirect" is not in host list
            NUnit.Framework.Assert.AreEqual(302, response.status);
            string redirect = response.GetHeader(ProxyUtils.Location);

            NUnit.Framework.Assert.AreEqual("http://bogus/redirect", redirect);
            // "127.0.0.1" contains in host list. Without cookie
            Org.Mockito.Mockito.When(request.GetRemoteAddr()).ThenReturn("127.0.0.1");
            testFilter.DoFilter(request, response, chain);
            NUnit.Framework.Assert.IsTrue(doFilterRequest.Contains("javax.servlet.http.HttpServletRequest"
                                                                   ));
            // cookie added
            Cookie[] cookies = new Cookie[1];
            cookies[0] = new Cookie(WebAppProxyServlet.ProxyUserCookieName, "user");
            Org.Mockito.Mockito.When(request.GetCookies()).ThenReturn(cookies);
            testFilter.DoFilter(request, response, chain);
            NUnit.Framework.Assert.AreEqual("org.apache.hadoop.yarn.server.webproxy.amfilter.AmIpServletRequestWrapper"
                                            , doFilterRequest);
            // request contains principal from cookie
            NUnit.Framework.Assert.AreEqual("user", servletWrapper.GetUserPrincipal().GetName
                                                ());
            NUnit.Framework.Assert.AreEqual("user", servletWrapper.GetRemoteUser());
            NUnit.Framework.Assert.IsFalse(servletWrapper.IsUserInRole(string.Empty));
        }
Ejemplo n.º 2
0
        /// <exception cref="System.Exception"/>
        public virtual void FilterNullCookies()
        {
            HttpServletRequest request = Org.Mockito.Mockito.Mock <HttpServletRequest>();

            Org.Mockito.Mockito.When(request.GetCookies()).ThenReturn(null);
            Org.Mockito.Mockito.When(request.GetRemoteAddr()).ThenReturn(proxyHost);
            HttpServletResponse          response = Org.Mockito.Mockito.Mock <HttpServletResponse>();
            AtomicBoolean                invoked  = new AtomicBoolean();
            FilterChain                  chain    = new _FilterChain_104(invoked);
            IDictionary <string, string> @params  = new Dictionary <string, string>();

            @params[AmIpFilter.ProxyHost]    = proxyHost;
            @params[AmIpFilter.ProxyUriBase] = proxyUri;
            FilterConfig conf   = new TestAmFilter.DummyFilterConfig(@params);
            Filter       filter = new TestAmFilter.TestAmIpFilter(this);

            filter.Init(conf);
            filter.DoFilter(request, response, chain);
            NUnit.Framework.Assert.IsTrue(invoked.Get());
            filter.Destroy();
        }