jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Willie H Armstrong <warms...@csc.com>
Subject Fw: Problem with referer header.
Date Wed, 11 May 2005 21:13:12 GMT




Your assistance is helping me refine the problem.

The code that now shows the error is

/*
 * SampleServletTest.java
 *
 * Created on May 11, 2005, 2:50 PM
 */

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;

public class SampleServletTest
        extends ServletTestCase {
    public SampleServletTest(String name) {
        super(name);
    }

    public void beginReferer(WebRequest request) {
        request.addHeader("Referer", "/test.jsp");
    }

    public void testReferer() {
        String strReferer = request.getHeader("Referer");
        assertEquals("/test.jsp", request.getHeader("Referer"));
    }
}

which gives an error of

junit.framework.ComparisonFailure: expected:</test.jsp> but was:<null>
      at SampleServletTest.testReferer(SampleServletTest.java:28)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      a
      at
org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153)
      at
org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119)
      at
org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93)

my batch file for launching junit is

cd c:\program files\junit3.8.1
java -cp
.;junit.jar;cactus-1.7.jar;aspectjrt-1.2.1.jar;commons-logging-1.0.4.jar;servletapi-2.2.jar;commons-httpclient-2.0.2.jar;httpunit-1.6.jar;f:\csc\Referer\Referer\build\web\WEB-INF\classes\
 junit.swingui.TestRunner

It appears that the servlet portion is actually irrelevant.  Between the
beginXXX and the testXXX, the header is getting "distorted".
Any further ideas?

Thank you,

Bill


Thanks


----- Forwarded by Willie H Armstrong/GTS/CSC on 05/11/2005 04:04 PM -----



Thanks for your reply.

I am running Tomcat 5.0.28 which is the default for the Netbeans IDE which
I am using.  I develop the classes in the IDE, launch tomcat from the IDE,
then run the test from a command prompt.

I will run your code and see if it gives me clean results.  It looks like
you have distilled the essence of the problem.

Regards,
Bill


Willie (Bill) Armstrong
Sun Certified Programmer for the Java 2 Platform 1.4




                      Kazuhito SUGURI

                      <suguri.kazuhito         To:
cactus-user@jakarta.apache.org, Willie H Armstrong/GTS/CSC@CSC

                      @lab.ntt.co.jp>          cc:

                                               Subject: Re: Problem with
referer header.
                      05/11/2005 01:22

                      PM

                      Please respond

                      to "Cactus Users

                      List"






Hi Willie,

I have tried to re-produce your problem, but my test-case does not fail.
I'm trying with tomcat-5.0.28. My test codes are appending:
              SampleServlet.java
              SampleServletTest.java

The SampleServlet is mapped to /refersample in my case.


In article
<OF1CB48276.2A1D71E4-ON86256FFE.004FBF05-86256FFE.0050195A@csc.com>,
Wed, 11 May 2005 09:34:53 -0500,
Willie H Armstrong <warmstr2@csc.com> wrote:
warmstr2> I am having a problem with the Referer header being "distorted"
by the
warmstr2> WebRequest.addHeader() method.  The header name and the header
value
warmstr2> gets changed into: "~~~~~~~" and
"~~~~:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
warmstr2> which I see when examining the Enumeration en in the debugger.
warmstr2>
warmstr2> I am attempting to set two headers in a WebRequest.
warmstr2>
warmstr2> code pieces as follows:
warmstr2>
warmstr2>
warmstr2> // the imports
warmstr2> import org.apache.cactus.ServletTestCase;
warmstr2> import org.apache.cactus.WebResponse;
warmstr2> import org.apache.cactus.WebRequest;
warmstr2>
warmstr2> // "setting up" the request
warmstr2>     public void beginProcessRequestNotReseeding(WebRequest
request) {
warmstr2>         System.out.print("beginProcessRequestNotReseeding");
warmstr2>         request.addHeader("HTTP_SM_USER", strHttpUser);
warmstr2>         request.addHeader("Referer", strRefUrl);
warmstr2>         System.out.println(".....Completed client side begin.");
warmstr2>     }

In my code, corresponds to SampleServletTest#beginReferer().

warmstr2> // sending the request
warmstr2>
warmstr2>    public void testProcessRequestNotReseeding() {
warmstr2>         System.out.println("testProcessRequestNotReseeding");
warmstr2>         assertFalse("Credential was set to true",
warmstr2> Credentialer.isReseeding());
warmstr2>         Enumeration en = request.getHeaderNames();
warmstr2>         try {
warmstr2>             servlet.processRequest(request, response);
warmstr2>         } catch (javax.servlet.ServletException jsse) {
warmstr2>             // do nothing
warmstr2>         } catch (IOException jioe) {
warmstr2>             // do nothing
warmstr2>         }
warmstr2>         Credentialer.setReseeding(false);
warmstr2>         System.out.println(".....Completed.");
warmstr2>     }

In my code, corresponds to SampleServletTest#testReferer().


warmstr2> // adding a Basic Authentication to  header then dispatching it
to
warmstr2> Sender servlet
warmstr2>
warmstr2> .... called by testProcessRequestNotReseeding() ........
warmstr2>
warmstr2>     protected void processRequest(HttpServletRequest request,
warmstr2> HttpServletResponse response)
warmstr2>     throws ServletException, IOException {
warmstr2> .......
warmstr2>             String strUser = request.getHeader("HTTP_SM_USER");
warmstr2>             String strPW = Credentialer.getPassword(strUser);
warmstr2>             // put BasicAuthurization data in request header
warmstr2>             try {
warmstr2>                 response.setHeader("Authorization", "Basic " +
warmstr2> makeAuthedString(strUser, strPW));
warmstr2>             } catch (EncoderException acnee){
warmstr2>                 acnee.printStackTrace();
warmstr2>                 // TODO make an HTML Message of failure);
warmstr2>             }
warmstr2>             // send request to Refer url
warmstr2>
request.getRequestDispatcher("/Sender").forward(request,
warmstr2> response);
warmstr2>     }

In my code, corresponds to SampleServlet#processRequest().
At this method, the request is dispatched to "/refersample",
i.e. I'm expecting SampleServlet#doGet() will be called.


warmstr2> // the Sender servlet does redirect to back to the referer.
warmstr2> ... the final servlet method called
warmstr2>
warmstr2>     protected void processRequest(HttpServletRequest request,
warmstr2> HttpServletResponse response)
warmstr2>     throws ServletException, IOException {
warmstr2>       String strRefURL = request.getHeader("Referer");
warmstr2>       response.sendRedirect(strRefURL);
warmstr2>     }

In my code, corresponds to SampleServlet#doGet(). At this point,
request.getHeader("Referer") of my code has non-null value,
"/test.jsp", and of cource, 302 response code is received at
SampleServletTest#endReferer().


warmstr2> The String strRefUrl is set to null so the sendRedirect of course
fails.

How could we re-produce the problem?

Regards,
----
Kazuhito SUGURI
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SampleServlet
    extends HttpServlet
{
    public void doGet(HttpServletRequest request,
                               HttpServletResponse response)
             throws ServletException, IOException
    {
        System.out.println("Referer: " + request.getHeader("Referer"));
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void processRequest(HttpServletRequest request,
                                  HttpServletResponse response)
             throws ServletException, IOException
    {
        response.setHeader("TEST", "testHeader");
        request.getRequestDispatcher("/refersample").forward(request,
response);
    }
}
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;

public class SampleServletTest
    extends ServletTestCase
{
    public SampleServletTest(String name)
    {
             super(name);
    }

    public void beginReferer(WebRequest request)
    {
             request.addHeader("Referer", "/test.jsp");
    }

    public void testReferer()
             throws Exception
    {
        assertEquals("/test.jsp", request.getHeader("Referer"));

             SampleServlet servlet = new SampleServlet();
        servlet.processRequest(request, response);
    }

    public void endReferer(WebResponse response)
        throws Exception
    {
        assertEquals(302, response.getStatusCode());
    }
}

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


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



Mime
View raw message