This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simulation of HTTP Post request with message body

 
Raj Nagappan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the java.net.HttpURLConnection class directly to send the request.
 
Raj Nagappan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf I'll give it a try
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic