Hi Kevin,

----- Original Message -----
From: "Kevin Jones" <[EMAIL PROTECTED]>
To: "Cactus-User" <[EMAIL PROTECTED]>
Sent: Monday, October 08, 2001 3:49 PM
Subject: Sending data to a servlet


> I want to send data to a servlet that the servlet retrieves using
> req.getReader. I know that Cactus doesn't support this directly (and I
know
> there's an open todo :-) ),

hehe ... :)

I've tried to write a unit test for Cactus that would use getReader() as I
didn't see why it would not work ... and it doesn't work ... ! My test is
very simple:

 public void beginGetReader(WebRequest theRequest)
 {
     theRequest.addParameter("test", "some body data");
  }

 public void testGetReader() throws Exception
 {
     String buffer;
     StringBuffer body = new StringBuffer();
     BufferedReader reader = request.getReader();
     while ((buffer = reader.readLine()) != null) {
         body.append(buffer);
     }
     assertEquals("test=some body data", body.toString());
 }

By default, Cactus sends the parameters using the POST method so getReader()
should get the parameter. Ok, I know, if you need to send some raw data
(without the 'xxx = yyy' format) then it wouldn't work but it would be easy
to add a new method for that.

However, the above test fails and body.toString() is empty. Why ? I believe
it is because, internally, Cactus sends other internal parameters in the
http request (such as the name of the test class, the method to call, ...)
and these parameters are extracted by the servlet redirector (using
request.getParameter()) _before_ the testGetReader() is called ... and
Servlet API doc says that for a given request, getParameter() [which must be
using getInputStream() internally] or getReader() can be called but not both
... [for some reason, using Resin, it does not raise an exception, but
returns ""].

.....

This is a major issue as we absolutely need to pass (transparently) some
context data to the servlet redirector. If we want to be able to support
getReader() [and we do want that !], we need to find another way of passing
our internal data .... A solution might be to continue to pass them in the
body and extract them by hand [not using getParameter()] by calling
getReader() [but we will need to be careful not to read on the data for the
test part  - we could also use a serialized java object]. This means there
will be 2 cases: tests that uses getInputStream() and tests that uses
getReader() ...

Question: can we put parameters in the URL when sending data using the POST
method ? In that case, we could maybe use the getQueryString() method (if it
doesn't call getInputStream()) ?

To summarize:
* adding support for sending data to servlets is easy if the code under test
is using getInputStream() to get that data
* adding support for sending data to servlets is difficult if the code under
test is using getReader() to get that data

> so can I do this 'outside' the scope of Cactus
> and still have the tests executed. I.e. can I use an HttpUrlConnection (or
> similar) to talk to the re-direction proxy within my testXXX methods?

hum ...
* the interface between the Cactus client and the Cactus server parts is not
public, meaning it may change in the future (as demonstrated for example by
the above analysis which will probably lead to a change),
* it would not even work for the reason explained above (getParameter() is
called before your getReader() code).

The only solution is to help us on Cactus correct this bug/limitation you
have found ... ! ;-)

Thanks for that !

>
> Kevin Jones
> Developmentor
> www.develop.com
>

-Vincent

--
Vincent Massol, [EMAIL PROTECTED]
OCTO Technology, www.octo.com
Information System Architecture Consulting

Reply via email to