This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Testing and the fly likes Simulation of HTTP Post request with message body Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Simulation of HTTP Post request with message body" Watch "Simulation of HTTP Post request with message body" New topic
Author

Simulation of HTTP Post request with message body

Raj Nagappan
Ranch Hand

Joined: May 26, 2004
Posts: 38
Hi everyone,

I am trying write a junit style test to invoke a Struts 2 action class, using Spring 2.5. I have been given a message body of an HTTP Post request that will be sent to our web site from an external source. This message body is plain text exactly of the Post request itself. I want to write a test against this.

I have been looking at the different Java web testing frameworks like JWebUnit, HtmlUnit and HttpUnit but none seem to do what I want.

I do not want either:

or

What I want is:

where myMessageBody is a plain-text concatenation of all the fields, which my test (Tomcat / Struts / Spring) has to unpack and deal with. Why do I want it this way? Because that is the API specification I am given and what I must program against.

Any ideas?

Raj Nagappan<br />SCJP, SCJD, PhD
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Change the requirement--you're now testing the web server, Struts 2, *and* your code. The purpose of unit testing is to test small, standalone units, that (hopefully) don't depend on too many other things. Besides--you'd have to run that unit test inside a container: Struts 2 uses the container's request processing to access request parameters.

This is almost certainly not what you want to do.
Raj Nagappan
Ranch Hand

Joined: May 26, 2004
Posts: 38
Hi David,

Yes I understand that. But I am specifically given a text file which is exactly what the Post request will look like. I need to test directly against this text file that contains all the fields. I have other unit tests that test isolated parts. But I really need this automated acceptance test that says "send this Post message text to me (to simulate a page transmission over the net) and I will respond correctly". How can I do this?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
You can use the java.net.HttpURLConnection class directly to send the request.


Ping & DNS - my free Android networking tools app
Raj Nagappan
Ranch Hand

Joined: May 26, 2004
Posts: 38
Thanks Ulf I'll give it a try
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'll still need to either run the test against a running container (an integration test), or simulate everything up to the point of the action request. IMO this isn't a good way to do this, since you're basically testing the app server. That's a slippery slope. If you're that uncomfortable with your app server, I'd consider maybe using a different one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simulation of HTTP Post request with message body