Testing the Post request

P

Pete

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();
}
}
 
S

Stanimir Stamenkov

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.
 
P

Pete

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.


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
 
S

Stanimir Stamenkov

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/HttpServlet.html
 
P

Pete

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....

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
 
M

markspace

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.
 
S

Stanimir Stamenkov

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/tutorial/networking/sockets/
 
P

Pete

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/tutorial/networking/sockets/

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/2010/10/let-me-dump-your-post-free-http-post-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
 
A

Abu Yahya

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/tutorial/networking/sockets/

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/2010/10/let-me-dump-your-post-free-http-post-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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top