When HttpUnit failed…

I have been working on HttpUnit for almost two years. The funniest part is, we have used it as a development tool than a testing tool. And for your information, its an awesome tool. I really mean it.

But even HttpUnit failed quite a few times too. I will tell you when. When I was parsing a website, I needed to submit a WebForm which contained a text filed with name “name ;  ; ; ;“. Seems quite funny right? Even I felt the same. But whenever HttpUnit parsed that form, it replaced those characters with blank spaces. I think it has to. Will tell you why.

; – special character for carriage return
; – special character for line feed
; – special character for horizontal tab

Check http://www.asciitable.com

And when I submitted the form, HttpUnit sent the form data as “name++++“, server was not ready to accept it. The server showed us its protest by always sending “The server didn’t find the page you had requested”  page. WTF !!!!

I had tried so many thinks & things to fix it (of course using HttpUnit itself), but nothing worked out. I still don’t know is there any direct way to solve it.

How I solved it 🙂

I tried something what I call “API bridging” to solve this problem. 

Steps:

  • Get the corresponding page response as a WebResponse object
  • Generate the form action URL (You will get it from WebResponse & WebForm objects)
  • Prepare the HTTP POST request string for the form

  (Do it by iterating thru the WebForm object’s parameter list)

  • Get all the cookies from the WebConversation object & create a cookie header string, so that we can set it as a request header.
  • Make a HttpUrlConnection object to the URL
  • Set all the HTTP request headers such as “Content-Length”, “User-Agent” etc
  • Open the output stream of the connection and write the post string
  • Accept the response & get all the cookies set by the server.
  • Set all the cookies to the WebConversation obect using the wc.putCookie(<name>, <value>);

– Continue with HttpUnit

All this stuff worked b’ coz of the fact the HTTP protocol is state-less.

Advertisements