You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Dennis Kempin <de...@googlemail.com> on 2006/06/17 14:20:15 UTC

Testing servlet redirects

Hello,

i am using the JettyTestSetup to test a simple Servlet. This servlet
sometimes calls
getServletContext().getNamedDispatcher("default").forward(request,
response); to redirect requests to the default servlet to serve static
resources.

Well this is what Jetty returns as response when such a redirect happens:
HTTP ERROR: 404 /core/ServletRedirector Not Found
RequestURI=/core/ServletRedirector
Where core is the context path.

It is not important for me to test what the redirect returns, its more
important that the servlet performs the redirect. That is what I want to
test.
Any ideas on how to test that (Testing for this error message would be a
very hacky solution)?

greetings
Dennis :)

RE: Testing JSP with sessions

Posted by karthik <ka...@xius.org>.
Hi

The examples described on the site was confusing and hard for me to digest,
please if u do have a simple samplel "war" and testcase for the same ...

 please post it on the form



with regards
Karthik

-----Original Message-----
From: Kazuhito SUGURI [mailto:suguri.kazuhito@lab.ntt.co.jp]
Sent: Tuesday, June 20, 2006 4:20 PM
To: cactus-user@jakarta.apache.org
Subject: Re: Testing JSP with sessions


Hi Karthik,

In article <EK...@xius.org>,
Tue, 20 Jun 2006 14:52:59 +0530,
"karthik" <ka...@xius.org> wrote:
karthikn>   Can some body on this form ,be kind to spare me some code on
karthikn>   HOWTO test a JSP pages which hare secured by session objects.
karthikn>   Suppoes  i have 100 jsp pages which have session Tracked,
karthikn>   so how to set the sessions and Test such jsp pages using  CACTUS
.

Please take a look at:
	http://jakarta.apache.org/cactus/writing/howto_testcase_jsp.html
        http://jakarta.apache.org/cactus/writing/howto_jsp.html

You can set session attributes in testXXX() method
before you call JSP under test by using one of forward methods.

Hope this helps,
----
Kazuhito SUGURI

---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-user-help@jakarta.apache.org




Re: Testing JSP with sessions

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Karthik,

In article <EK...@xius.org>,
Tue, 20 Jun 2006 14:52:59 +0530,
"karthik" <ka...@xius.org> wrote: 
karthikn>   Can some body on this form ,be kind to spare me some code on 
karthikn>   HOWTO test a JSP pages which hare secured by session objects.
karthikn>   Suppoes  i have 100 jsp pages which have session Tracked,
karthikn>   so how to set the sessions and Test such jsp pages using  CACTUS .

Please take a look at:
	http://jakarta.apache.org/cactus/writing/howto_testcase_jsp.html
        http://jakarta.apache.org/cactus/writing/howto_jsp.html

You can set session attributes in testXXX() method
before you call JSP under test by using one of forward methods.

Hope this helps,
----
Kazuhito SUGURI

Re: Testing servlet redirects

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Karthik,

In article <EK...@xius.org>,
Wed, 21 Jun 2006 18:32:48 +0530,
"karthik" <ka...@xius.org> wrote: 
karthikn>   The example provided on the site is something confusing....

Please point out what confuses you.

karthikn>   If u hava a sample working test case with war file,please
karthikn>   provid me the same. so i can correct my self.

A sample.

    public class SampleTest
        extends ServletTestCase
    {
        public SampleTest(String name)
        {
            super(name);
        }
        public void testJsp()
            throws Exception
        {
            session.setAttribute("testKey", "testValue");
            RequestDispatcher rd = request.getRequestDispatcher("/test.jsp");
            rd.forward(request, response);
        }
        public void endJsp(WebResponse response)
        {
            System.out.println(response.getText());
        }
    }


test.jsp:
    <%= session.getAttribute("testKey") %>


Regards,
----
Kazuhito SUGURI

RE: Testing servlet redirects

Posted by karthik <ka...@xius.org>.
Hi

  The example provided on the site is something confusing....
  If u hava a sample working test case with war file,please
  provid me the same. so i can correct my self.



with regards
Karthik


-----Original Message-----
From: Kazuhito SUGURI [mailto:suguri.kazuhito@lab.ntt.co.jp]
Sent: Tuesday, June 20, 2006 3:19 PM
To: cactus-user@jakarta.apache.org
Subject: Re: Testing servlet redirects


Hi Dennis,

Followings are information and recommendation from me.
These are based on short examination with default settings of Tomat-5.5.12.
But, please note that behavior of a servlet named "default" might
be different for each container and its configuration.

In article <eb...@mail.gmail.com>,
Mon, 19 Jun 2006 23:14:40 +0200,
"Dennis Kempin" <de...@googlemail.com> wrote:
dennis> thank you for your answer. My servlet is mapped to process any
request that
dennis> is made.
dennis> Lets say we have a request on /core/test.html: My servlets checks if
it can
dennis> handle, if not it redirects to the default
dennis> servlet which should serve test.html (which does exist).
dennis> But instead of the contents of test.html i get the described 404
Error which
dennis> says that it cannot find "/core/ServletRedirector".

I think it's hard to test the forwarding by using Cactus
as long as you are using getNamedDispatcher("default").


RequestDispatcherWrapper#forward(), which is used in the test,
calls the original RequestDispatcher with the original HTTP request.
# See Cactus API JavaDoc.

A servier-side test case is invoked from a servlet,
/<context>/ ServletRedirecter. So, the request URI of the original HTTP
request is "/<context>/ServletRedirector".

When the original RequestDispatcher is the default servlet of Tomcat,
it's trying to obtain the resource from "/<context>/ServletRedirector",
that has the possiblity of causing an infinite loop.
# Fortunately, the default servlet responds 404 and stops the loop.

Please try following fragments of servlet and web.xml.
You will find similar behavior with your Cactus tests
when you access to <context>/dummy.

    public DummyServlet
        extends HttpServlet
    {
        protected void doGet(HttpServletRequest request,
                             HttpServletResponse response)
            throws ServletException, IOException
        {

getServletContext().getNamedDispatcher("default").forward(request,
response);
        }
    }

    <servlet>
      <servlet-name>dummy</servlet-name>
      <servlet-class>DummyServlet</servlet-name>
    </servlet>
    <servlet-mapping>
      <servlet-name>dummy</servlet-name>
      <url-pattern>/dummy</url-pattern>
    </servlet-mapping>


I would like to recommend you to use getRequestDispatcher(String)
instead of getNamedDispatcher("default"), it it's possible.
If you can do so, you will be able to simulate any path condition
for a testXXX() by using org.apache.cactus.WebRequest#setURL() method
in beginXXX(), and you can evaluate a forwarded responce in the
corresponding
endXXX() method.
# You should not simulate <context>/ServletRedirector,
# which is the default of Cactus, of cource :-)


Hope this helps,
----
Kazuhito SUGURI

---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-user-help@jakarta.apache.org




Re: Testing servlet redirects

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Dennis,

Followings are information and recommendation from me.
These are based on short examination with default settings of Tomat-5.5.12.
But, please note that behavior of a servlet named "default" might
be different for each container and its configuration.

In article <eb...@mail.gmail.com>,
Mon, 19 Jun 2006 23:14:40 +0200,
"Dennis Kempin" <de...@googlemail.com> wrote: 
dennis> thank you for your answer. My servlet is mapped to process any request that
dennis> is made.
dennis> Lets say we have a request on /core/test.html: My servlets checks if it can
dennis> handle, if not it redirects to the default
dennis> servlet which should serve test.html (which does exist).
dennis> But instead of the contents of test.html i get the described 404 Error which
dennis> says that it cannot find "/core/ServletRedirector".

I think it's hard to test the forwarding by using Cactus
as long as you are using getNamedDispatcher("default").


RequestDispatcherWrapper#forward(), which is used in the test,
calls the original RequestDispatcher with the original HTTP request.
# See Cactus API JavaDoc.

A servier-side test case is invoked from a servlet,
/<context>/ ServletRedirecter. So, the request URI of the original HTTP
request is "/<context>/ServletRedirector".

When the original RequestDispatcher is the default servlet of Tomcat,
it's trying to obtain the resource from "/<context>/ServletRedirector",
that has the possiblity of causing an infinite loop.
# Fortunately, the default servlet responds 404 and stops the loop.

Please try following fragments of servlet and web.xml.
You will find similar behavior with your Cactus tests
when you access to <context>/dummy.

    public DummyServlet
        extends HttpServlet
    {
        protected void doGet(HttpServletRequest request,
                             HttpServletResponse response)
            throws ServletException, IOException
        {
            getServletContext().getNamedDispatcher("default").forward(request, response);
        }
    }

    <servlet>
      <servlet-name>dummy</servlet-name>
      <servlet-class>DummyServlet</servlet-name>
    </servlet>
    <servlet-mapping>
      <servlet-name>dummy</servlet-name>
      <url-pattern>/dummy</url-pattern>
    </servlet-mapping>


I would like to recommend you to use getRequestDispatcher(String)
instead of getNamedDispatcher("default"), it it's possible.
If you can do so, you will be able to simulate any path condition
for a testXXX() by using org.apache.cactus.WebRequest#setURL() method
in beginXXX(), and you can evaluate a forwarded responce in the corresponding
endXXX() method.
# You should not simulate <context>/ServletRedirector,
# which is the default of Cactus, of cource :-)


Hope this helps,
----
Kazuhito SUGURI

Testing JSP with sessions

Posted by karthik <ka...@xius.org>.
Hi 

  Can some body on this form ,be kind to spare me some code on 

  HOWTO test a JSP pages which hare secured by session objects.

  Suppoes  i have 100 jsp pages which have session Tracked,
  
  so how to set the sessions and Test such jsp pages using  CACTUS .




with regards
Karthik



 
  



Re: Testing servlet redirects

Posted by Dennis Kempin <de...@googlemail.com>.
Hi Kazuhito,

thank you for your answer. My servlet is mapped to process any request that
is made.
Lets say we have a request on /core/test.html: My servlets checks if it can
handle, if not it redirects to the default
servlet which should serve test.html (which does exist).
But instead of the contents of test.html i get the described 404 Error which
says that it cannot find "/core/ServletRedirector".
It works fine running the redirect in tomcat and jetty containers without
cactus.

Regards,
Dennis

2006/6/19, Kazuhito SUGURI <su...@lab.ntt.co.jp>:
>
> Hi Dennis,
>
> In article <eb...@mail.gmail.com>,
> Sat, 17 Jun 2006 14:20:15 +0200,
> "Dennis Kempin" <de...@googlemail.com> wrote:
> dennis> i am using the JettyTestSetup to test a simple Servlet. This
> servlet
> dennis> sometimes calls
> dennis> getServletContext().getNamedDispatcher("default").forward(request,
> dennis> response); to redirect requests to the default servlet to serve
> static
> dennis> resources.
> dennis>
> dennis> Well this is what Jetty returns as response when such a redirect
> happens:
> dennis> HTTP ERROR: 404 /core/ServletRedirector Not Found
> dennis> RequestURI=/core/ServletRedirector
> dennis> Where core is the context path.
>
> Do you mean that the 404 response is the expected behavior of
> RequestDispatcher#foward() performed by your servlet?
>
>
> dennis> It is not important for me to test what the redirect returns, its
> more
> dennis> important that the servlet performs the redirect. That is what I
> want to
> dennis> test.
> dennis> Any ideas on how to test that (Testing for this error message
> would be a
> dennis> very hacky solution)?
>
> I don't think the error message is the expected result, however,
> I think you need to evaluate the response at endXXX(WebResponse) method
> to test RequestDispatcher#forward works as expected.
>
> Regards,
> ----
> Kazuhito SUGURI
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org
>
>

Re: Testing servlet redirects

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Dennis,

In article <eb...@mail.gmail.com>,
Sat, 17 Jun 2006 14:20:15 +0200,
"Dennis Kempin" <de...@googlemail.com> wrote: 
dennis> i am using the JettyTestSetup to test a simple Servlet. This servlet
dennis> sometimes calls
dennis> getServletContext().getNamedDispatcher("default").forward(request,
dennis> response); to redirect requests to the default servlet to serve static
dennis> resources.
dennis> 
dennis> Well this is what Jetty returns as response when such a redirect happens:
dennis> HTTP ERROR: 404 /core/ServletRedirector Not Found
dennis> RequestURI=/core/ServletRedirector
dennis> Where core is the context path.

Do you mean that the 404 response is the expected behavior of
RequestDispatcher#foward() performed by your servlet?


dennis> It is not important for me to test what the redirect returns, its more
dennis> important that the servlet performs the redirect. That is what I want to
dennis> test.
dennis> Any ideas on how to test that (Testing for this error message would be a
dennis> very hacky solution)?

I don't think the error message is the expected result, however,
I think you need to evaluate the response at endXXX(WebResponse) method
to test RequestDispatcher#forward works as expected.

Regards,
----
Kazuhito SUGURI