Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Testing the Post request

Reply
Thread Tools

Testing the Post request

 
 
Pete
Guest
Posts: n/a
 
      06-18-2011
Hi,
Below is a bit of code that I intend to use to perform a http "Post"
request to an another application - which is still under development.
I would like to verify the implemention of this method ie it will
perform a http POST request with the xml content to the specified URL.
I want to avoid mocking the HttpClient class. I would like to
effectively see this method perform the a actions and and some stub
application receive the request ( get I presume). Any help, with
example code pls, would be very much appreciated.

Thank You.

Pete

code =>
public void sendResponse() {

String url = "URL";

String xmldata = "xml data";

PostMethod post = new PostMethod(url);

final RequestEntity entity = new StringRequestEntity(xmldata);
post.setRequestEntity(entity);

// Specify content type and encoding
post.setRequestHeader("Content-type", "text/xml;
charset=ISO-8859-1");

// Get HTTP client
HttpClient httpclient = new HttpClient();

// Execute request
try {

int result = httpclient.executeMethod(post);

} catch (Exception e) {
System.out.print("exception encountered " + e.toString());
} finally {
post.releaseConnection();
}
}
 
Reply With Quote
 
 
 
 
Stanimir Stamenkov
Guest
Posts: n/a
 
      06-18-2011
Sat, 18 Jun 2011 04:41:54 -0700 (PDT), /Pete/:

> Below is a bit of code that I intend to use to perform a http "Post"
> request to an another application - which is still under development.
> I would like to verify the implemention of this method ie it will
> perform a http POST request with the xml content to the specified URL.
> I want to avoid mocking the HttpClient class. I would like to
> effectively see this method perform the a actions and and some stub
> application receive the request ( get I presume). Any help, with
> example code pls, would be very much appreciated.


While I don't see anything wrong with mocking the HttpClient to
perform a unit test of you code (i.e. to verify it interacts with
the HttpClient in the intended way), it seems you want an
integration test performing real HTTP communication. For this
you'll need to create (if there's not one already) a test service
receiving and handling the requests. I can't give you specific
example code to build the test service but I guess you could create
it as simple servlet application.

I don't know if creating the test service would be any easier than
creating unit tests, mocking the HttpClient (using Mockito [1] I
have experience with, is quite easy), but in the end you might want
having both type of tests. The integration test however would
require specific test service environment setup and running which is
usually better suited for running by a continuous integration
system, while the unit tests could be run by anyone as part of the
standard build.

--
Stanimir
 
Reply With Quote
 
 
 
 
Pete
Guest
Posts: n/a
 
      06-18-2011
On Jun 18, 3:42*pm, Stanimir Stamenkov <(E-Mail Removed)> wrote:
> Sat, 18 Jun 2011 04:41:54 -0700 (PDT), /Pete/:
>
> > Below is a bit of code that I intend to use to perform a http "Post"
> > request to an another application - which is still under development.
> > I would like to verify the implemention of this method ie it will
> > perform a http POST request with the xml content to the specified URL.
> > I want to avoid mocking the HttpClient class. I would like to
> > effectively see this method perform the a actions and and some stub
> > application receive the request ( get I presume). Any help, with
> > example code pls, would be very much appreciated.

>
> While I don't see anything wrong with mocking the HttpClient to
> perform a unit test of you code (i.e. to verify it interacts with
> the HttpClient in the intended way), it seems you want an
> integration test performing real HTTP communication. *For this
> you'll need to create (if there's not one already) a test service
> receiving and handling the requests. *I can't give you specific
> example code to build the test service but I guess you could create
> it as simple servlet application.
>
> I don't know if creating the test service would be any easier than
> creating unit tests, mocking the HttpClient (using Mockito [1] I
> have experience with, is quite easy), but in the end you might want
> having both type of tests. *The integration test however would
> require specific test service environment setup and running which is
> usually better suited for running by a continuous integration
> system, while the unit tests could be run by anyone as part of the
> standard build.
>
> --
> Stanimir



Hi Stanimir,

Thank you for your response.
My main concern for not mocking the HTTPClient is really that it is
NOT going to give me the confidence I would like of my implementation.
If I had missed to configure something eg "String url = "URL";", when
the function is called nothing would received at the other end -
mocking the HTTPClient is not going to highlight this until I get to
really integrate with the other application. However,if I did
something like your other suggestion - "a test service receiving and
handling the requests" - this would highlight any issues with my
implementation in advance.
This is subject matter is relatively new to me - even my logic of my
function, I got from the internate. So I wonder if you have any
suggestions how I can create a SIMPLE "a test service receiving and
handling the requests" to test my function. Again example would very
much be appreciated.

Thank You again.

Pete
 
Reply With Quote
 
Stanimir Stamenkov
Guest
Posts: n/a
 
      06-18-2011
Sat, 18 Jun 2011 08:54:20 -0700 (PDT), /Pete/:

> If I had missed to configure something eg "String url = "URL";", when
> the function is called nothing would received at the other end -
> mocking the HTTPClient is not going to highlight this until I get to
> really integrate with the other application.


So, your unit tests would verify the configuration is read from
intended source (possibly mocked) and then supplied to the
HttpClient. Of course you can't protect from human error like
supplying wrong configuration in production environment, but that's
system administration task.

> However,if I did
> something like your other suggestion - "a test service receiving and
> handling the requests" - this would highlight any issues with my
> implementation in advance.
> This is subject matter is relatively new to me - even my logic of my
> function, I got from the internate. So I wonder if you have any
> suggestions how I can create a SIMPLE "a test service receiving and
> handling the requests" to test my function. Again example would very
> much be appreciated.


I've already suggested the test service could be implemented as a
simple servlet [1]. Depending on what this servlet is expected to
do - it might be not that simple. Do you need help with:

1. Creating a servlet;
2. Implementing the business logic of the servlet;
2. Packaging it as web application/module;
3. Deploying it in a servlet container;
4. Setting up an application/servlet container.

?

[1]
http://download.oracle.com/javaee/5/...tpServlet.html

--
Stanimir
 
Reply With Quote
 
Pete
Guest
Posts: n/a
 
      06-18-2011
On Jun 18, 5:33*pm, Stanimir Stamenkov <(E-Mail Removed)> wrote:
> Sat, 18 Jun 2011 08:54:20 -0700 (PDT), /Pete/:
>
> > If I had missed to configure something eg "String url = "URL";", when
> > the function is called nothing would received at the other end -
> > mocking the HTTPClient is not going to highlight this until I get to
> > really integrate with the other application.

>
> So, your unit tests would verify the configuration is read from
> intended source (possibly mocked) and then supplied to the
> HttpClient. *Of course you can't protect from human error like
> supplying wrong configuration in production environment, but that's
> system administration task.
>
> > However,if I did
> > something like your other suggestion - "a test service receiving and
> > handling the requests" - this would highlight any issues with my
> > implementation in advance.
> > This is subject matter is relatively new to me - even my logic of my
> > function, I got from the internate. So I wonder if you have any
> > suggestions how I can create a SIMPLE *"a test service receiving and
> > handling the requests" to test my function. Again example would very
> > much be appreciated.

>
> I've already suggested the test service could be implemented as a
> simple servlet [1]. *Depending on what this servlet is expected to
> do - it might be not that simple. *Do you need help with:
>
> 1. Creating a servlet;
> 2. Implementing the business logic of the servlet;
> 2. Packaging it as web application/module;
> 3. Deploying it in a servlet container;
> 4. Setting up an application/servlet container.
>
> ?
>
> [1]http://download.oracle.com/javaee/5/api/javax/servlet/http/HttpServle....
>
> --
> Stanimir


Hi Stanimir,

I have experience in Java programming but not in J2EE, so working with
HTTP and servlets etc is new to me but at the same time interesting.
So in answer to your question :-

Going with a servlet notion, what should it do ?
I just would like confidence that my sendResponse() has successfully
received the xml data and effectively verifying my sendResponse()
implementation is correct.

Do I need help with: steps 1 to 5?
In short yes. Basically I would complete this by the end of the
weekend.

Incidently, you are obviously very experinced in this field, do you
see any problems with my sendResponse()?

Once again thank you very much.

Pete

 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      06-18-2011
On 6/18/2011 8:54 AM, Pete wrote:

> This is subject matter is relatively new to me - even my logic of my
> function, I got from the internate. So I wonder if you have any
> suggestions how I can create a SIMPLE "a test service receiving and
> handling the requests" to test my function.



I think what you should do is just open a simple socket, read in the
packet, and verify line-by-line that the data is as expected.

This is not exactly what you are asking for, but it is the simplest and
also something I think you should do for the long term. I.e., as a
permanent test harness.

The FIRST thing I think you should do is just to implement the above,
and then print out the packet you receive. Compare it manually with
what you expect, and with any examples you can find online of a POST
packet. That I think will help you understand the most what you ought
to receive, and what you are receiving.

Again, no matter what else you plan to do, I can almost guarantee that
if you are new to this, you'll end up implementing a little packet
printer no matter what, so you might as well do it now.

 
Reply With Quote
 
Stanimir Stamenkov
Guest
Posts: n/a
 
      06-18-2011
Sat, 18 Jun 2011 10:40:00 -0700 (PDT), /Pete/:

> I have experience in Java programming but not in J2EE, so working with
> HTTP and servlets etc is new to me but at the same time interesting.
> So in answer to your question :-
>[...]
> Do I need help with: steps 1 to 5?
> In short yes. Basically I would complete this by the end of the
> weekend.


Given your time restriction, you may better take the approach
suggested by markspace in another reply - write a simple socket
server, reading the HTTP request, verifying what you need. I guess
you'll also need to send out a manually crafted HTTP response back
from you socket service.

You may read the Java Tutorial trail on sockets, if you need further
help with that:

http://download.oracle.com/javase/tu...rking/sockets/

--
Stanimir
 
Reply With Quote
 
Pete
Guest
Posts: n/a
 
      06-18-2011
On Jun 18, 9:36*pm, Stanimir Stamenkov <(E-Mail Removed)> wrote:
> Sat, 18 Jun 2011 10:40:00 -0700 (PDT), /Pete/:
>
> > I have experience in Java programming but not in J2EE, so working with
> > HTTP and servlets etc is new to me but at the same time interesting.
> > So in answer to your question :-
> >[...]
> > Do I need help with: steps 1 to 5?
> > In short yes. Basically I would complete this by the end of the
> > weekend.

>
> Given your time restriction, you may better take the approach
> suggested by markspace in another reply - write a simple socket
> server, reading the HTTP request, verifying what you need. *I guess
> you'll also need to send out a manually crafted HTTP response back
> from you socket service.
>
> You may read the Java Tutorial trail on sockets, if you need further
> help with that:
>
> http://download.oracle.com/javase/tu...rking/sockets/
>
> --
> Stanimir


Stanimir, markspace

Thank you for your help.
It is clear I have a learning curve to overcome on this subject. I
started to follow the link provided by Stanimar and look towards a
test harness as suggested by markspace. I felt I had very tight time
constraint and would not finish in time and did further google
searches.

I came across this site
http://www.henrycipolla.com/blog/201...t-test-server/
Just what i wanted. Ran my program and the site verifed my
implementation - relief.

However, the link is interesting reading which I will pursue. As
markspace mentioned, this experice is good and to build on.

So thank you both for your time and help.

Pete
 
Reply With Quote
 
Abu Yahya
Guest
Posts: n/a
 
      06-19-2011
On 6/19/2011 4:03 AM, Pete wrote:
> On Jun 18, 9:36 pm, Stanimir Stamenkov<(E-Mail Removed)> wrote:
>> Sat, 18 Jun 2011 10:40:00 -0700 (PDT), /Pete/:
>>
>>> I have experience in Java programming but not in J2EE, so working with
>>> HTTP and servlets etc is new to me but at the same time interesting.
>>> So in answer to your question :-
>>> [...]
>>> Do I need help with: steps 1 to 5?
>>> In short yes. Basically I would complete this by the end of the
>>> weekend.

>>
>> Given your time restriction, you may better take the approach
>> suggested by markspace in another reply - write a simple socket
>> server, reading the HTTP request, verifying what you need. I guess
>> you'll also need to send out a manually crafted HTTP response back
>> from you socket service.
>>
>> You may read the Java Tutorial trail on sockets, if you need further
>> help with that:
>>
>> http://download.oracle.com/javase/tu...rking/sockets/
>>
>> --
>> Stanimir

>
> Stanimir, markspace
>
> Thank you for your help.
> It is clear I have a learning curve to overcome on this subject. I
> started to follow the link provided by Stanimar and look towards a
> test harness as suggested by markspace. I felt I had very tight time
> constraint and would not finish in time and did further google
> searches.
>
> I came across this site
> http://www.henrycipolla.com/blog/201...t-test-server/
> Just what i wanted. Ran my program and the site verifed my
> implementation - relief.
>
> However, the link is interesting reading which I will pursue. As
> markspace mentioned, this experice is good and to build on.
>


While you learn, it's also a good idea to get used with tools for packet
analysis such as Wireshark and Fiddler. Wireshark allows you to capture
and analyze a wide range of network data, while Fiddler can help you
easily handcraft your own HTTP requests.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
testing testing neville Computer Support 2 05-27-2005 09:57 AM
testing testing 123 daniel edwards Computer Support 4 05-20-2004 10:36 PM
Post post post. Shel-hed Computer Support 2 11-08-2003 07:41 AM
Re: Accessing Request.InputStream / Request.BinaryRead *as the request is occuring*: How??? Brian Birtle ASP .Net 2 10-16-2003 02:11 PM
testing--news2004--testing Boomer Computer Support 3 09-24-2003 06:54 PM



Advertisments