2 Replies Latest reply on Mar 30, 2010 3:50 PM by Adrien Adrien

    Jsunit Beta 3 : how to change ip address  for request.getRe

    Adrien Adrien Apprentice

      HI,
      I'll check the country from the request.getRemoteAddr().
      For my test I want to specify specific ip.

      There is a way to change the IP of the request for test it?

      Thanks

        • 1. Re:  Jsunit Beta 3 : how to change ip address  for request.g
          Stan Silvert Master

          The JSFUnit test will use whatever IP is used to start the test. So you could run your test suite multiple times with different IP's.

          Another option is to write a servlet filter that wraps HttpServletRequest and overrides request.getRemoteAddr() for JSF requests.

          This is something I've never tried with JSFUnit, but I'm pretty sure it will work.

          Another possible way to do it is to write your own InitialRequestStrategy. This doc, http://www.jboss.org/community/docs/DOC-10974, tells how to create an InitialRequestStrategy for custom login, but you could also use it to change the IP. Take a look at the code for SimpleInitialRequestStrategy.

          Stan

          • 2. Re:  Jsunit Beta 3 : how to change ip address  for request.g
            Adrien Adrien Apprentice

            Thanks, it's work fine.

             

            I've don't find for InitialRequestStrategy what to do but it's work well with a servlet filter.

             

            If somebody need to change Ip address or anything in HttpServletRequest, here a sample:

             

             

            In web.xml

            <filter>
              <filter-name>ChangeIPFilter</filter-name>
              <filter-class>com.xxx.web.jsf.ChangeIPServletFilter</filter-class>
            </filter>

            <filter-mapping>
              <filter-name>ChangeIPFilter</filter-name>
              <url-pattern>/*</url-pattern>
            </filter-mapping>

             

            Servlet


            import java.io.IOException;

             

            import javax.servlet.Filter;
            import javax.servlet.FilterChain;
            import javax.servlet.FilterConfig;
            import javax.servlet.ServletException;
            import javax.servlet.ServletRequest;
            import javax.servlet.ServletResponse;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletRequestWrapper;

             

            public class ChangeIPServletFilter implements Filter {
                public static String IP_ADDR = "41.23.21.12";

             

                public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {   
                    chain.doFilter(new WrapperHttpServletRequest((HttpServletRequest) request), response);
                   
                }

             

                @Override
                public void destroy() {

             

                }

             

                @Override
                public void init(FilterConfig arg0) throws ServletException {

             

                }

             

                private static class WrapperHttpServletRequest extends HttpServletRequestWrapper {       
                    public WrapperHttpServletRequest(HttpServletRequest request) {
                        super(request);           
                    }
                    public String getRemoteAddr(){   
                        return IP_ADDR;
                    }
                   
                }

             

            }