HTTP traffic hacking….

One of these days… i was trying to get my office rails application integration tested. We did a stupid feature we call Preview(which shows the end user view of and entity, after changes are made to it, and before changes are persisted). The problem needs the admin sessions to be stateful, and were running into a bunch of unforeseen scenarios…
Thats when we decided to do some developer integration testing, which forces server to do all the serialization and unserialzation of uploaded files and stuff….
Now mechanize apparently has some problem with our app…(mongrel may be) and i realized that it was pushing some stupid response especially for mechanize, where as it was perfectly normal for firefox/safari.

I decided to intercept the HTTP traffic to decide who was screwing up…(Mechanize response handler or Mongrel)….

So i decided to use this little trick to intercept the response, and play view it before it reaches mechanize…. And configured mechanize to use this as an HTTP Proxy….

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'net/http'
require 'webrick/httpproxy'
 
s = WEBrick::HTTPProxyServer.new(
  :Port => 4000,
  :ProxyContentHandler => Proc.new{|req,res|
    puts "RESPONSE CALLBACK!!!"
    puts req.request_line, req.raw_header
    puts "Request was #{req.inspect}"
    puts "\n*************************************************\nResponse : "
    puts res.body
  }
)
trap("INT"){ s.shutdown }
s.start

Webrick documentation doesn’t talk about this… so it needs some diving into the source to figure it out….

Pointing mechanize to this proxy is fairly easy…..

Others in the same kinda problem, may find this useful…. Happy hacking….!!!


About this entry