2005-12-17 06:46:23 +00:00
#!/usr/bin/env ruby
2005-07-24 20:53:54 +00:00
$: . unshift ( File . join ( File . dirname ( __FILE__ ) , '..' , '..' , '..' ) )
require 'test/unit'
require 'rex/proto/http'
2005-12-02 02:35:30 +00:00
class Rex :: Proto :: Http :: Request :: UnitTest < Test :: Unit :: TestCase
2005-07-24 20:53:54 +00:00
Klass = Rex :: Proto :: Http :: Request
def test_to_s
h = Klass . new
h . headers [ 'Foo' ] = 'Fishing'
h . headers [ 'Chicken' ] = 47
h . auto_cl = true
assert_equal (
2005-12-02 02:35:30 +00:00
" GET / HTTP/1.1 \r \n " +
2005-07-24 20:53:54 +00:00
" Foo: Fishing \r \n " +
" Content-Length: 0 \r \n " +
" Chicken: 47 \r \n \r \n " , h . to_s )
end
def test_from_s
h = Klass . new
h . from_s (
" POST /foo HTTP/1.0 \r \n " +
" Lucifer: Beast \r \n " +
" HoHo: Satan \r \n " +
" Eat: Babies \r \n " +
" \r \n " )
2006-01-05 22:20:28 +00:00
assert_equal ( 'POST' , h . method , 'method' )
assert_equal ( '/foo' , h . uri , 'uri' )
assert_equal ( '1.0' , h . proto , 'proto' )
assert_equal ( 'Babies' , h [ 'Eat' ] , 'header' )
assert_equal ( " POST /foo HTTP/1.0 \r \n " , h . cmd_string , 'cmd_string' )
2005-07-24 20:53:54 +00:00
h . method = 'GET'
2006-01-05 22:20:28 +00:00
assert_equal ( " GET /foo HTTP/1.0 \r \n " , h . cmd_string , 'set method' )
2006-01-27 05:33:08 +00:00
h . uri = '/bar'
2006-01-05 22:20:28 +00:00
assert_equal ( " GET /bar HTTP/1.0 \r \n " , h . cmd_string , 'set uri' )
2006-01-27 05:33:08 +00:00
h . proto = '1.2'
2006-01-05 22:20:28 +00:00
assert_equal ( " GET /bar HTTP/1.2 \r \n " , h . cmd_string , 'set proto' )
2005-07-24 20:53:54 +00:00
end
2006-01-27 05:33:08 +00:00
def test_params
2006-01-23 21:58:53 +00:00
h = Klass . new
h . from_s ( " GET /foo?a=1&b=2 HTTP/1.0 \r \n " +
" Foo: Bar \r \n \r \n " )
assert_equal ( 'GET' , h . method , 'method' )
assert_equal ( '1.0' , h . proto , 'proto' )
assert_equal ( 'Bar' , h [ 'Foo' ] , 'header' )
assert_equal ( '/foo?a=1&b=2' , h . uri , 'uri' )
h . uri_parts [ 'QueryString' ] [ 'c' ] = '3'
assert_equal ( '/foo?a=1&b=2&c=3' , h . uri , 'uri with additional params' )
2006-01-27 05:33:08 +00:00
h . uri_parts [ 'QueryString' ] [ 'd' ] = '='
2006-01-23 21:58:53 +00:00
assert_equal ( '/foo?a=1&b=2&c=3&d=%3d' , h . uri , 'uri with additional params that require escaping' )
2006-01-27 05:33:08 +00:00
srand ( 0 )
h . junk_directories = 1
2006-01-23 23:57:50 +00:00
assert_equal ( '/D/../DnJT/../kXG/../Y/../BmnXu/../foo?a=1&b=2&c=3&d=%3d' , h . uri , 'uri with junk directories' )
2006-01-27 05:33:08 +00:00
h . junk_slashes = 1
2006-01-23 23:57:50 +00:00
assert_equal ( '/lZ//..//J//../zQzFP///..///S/..//Yxzd//../D/OngB/..///gf//..///XjL//..///ciA/..///k/..//..//RPEpq//../fBB/../jm/../uSbA/..//MqUL/..///DnJT///vDMko//..//jXPNV//..///x//..//jA/..//x///..//..//iwdDi/../Nwn//..//AK///..//rXC///..///ij//..///kXG///SujU/..//yb//..//Bk///..//Wro//..//CzQg///../..//uqzo/..//C//..///eirN/..///o/..///T/..///Y/CDs/../E/..//tLWN///../tL/..//Q///..//../QT/../og/../GVfNH//../Y///..///So///../BmnXu/Bf///../YYDSv///..///qMmj/..//gLoU//..//rlc/../..//bTZ/../uUdD//..///nJJp///../uays///..//fJKb//..///foo?a=1&b=2&c=3&d=%3d' , h . uri , 'uri with junk directories' )
2006-01-27 05:33:08 +00:00
2006-01-23 21:58:53 +00:00
h = Klass . new
h . from_s ( " GET /foo?a=1&b=2 HTTP/1.0 \r \n " + " Foo: Bar \r \n \r \n " )
2006-01-27 05:33:08 +00:00
h . junk_params = 1
assert_equal ( " /foo?zerStXB=qweQajx&JsNGmnINHQWPZIj=RHUZCQy&XYEksxXeZUhlXbdhzz=HpxJATk&UwDqBU=EQwvK&oebrfUGJbvjTMSxKih=MkBx&a=1&YkjFGDiohcEa=t&JFhHeIUH=VbsD&UHTfAFbreJT=VlcIruAo&mZKziXgT=z&hsytpEdbRjC=tPkpE&b=2&NetXijJaaWMPiazmuQvoAKL=HeGtePpmrSHcBpCycO&bkfdyudyhM=pQCIzKwabBAFYiPDulrTYGUGczGCccmlFtJkN&fRjtzIZVtlWQZulBFGMaKOIHtF=qDKybZDOSFERFeYDFokxYhShOxHruwhRdMugizXZuyrpuAMJSEHD&MltwtSzxHaxudDKUqBUQq=caXwCmJCspZkaEpKMohlnghajZyYSUecISZYnqcYSDsTtAKDGbjGTiy&mUrAktpChMPhXMFmBKGGmmL=VyyzCMdJzIFrBrPMvMVSZNecspVGkwoaeFPllxfgwQgKMdAdanWTFkT " , h . uri , 'junk params' )
2006-01-23 21:58:53 +00:00
2006-01-23 23:57:50 +00:00
h = Klass . new
h . from_s ( " GET /foo?a=1&b=2 HTTP/1.0 \r \n " + " Foo: Bar \r \n \r \n " )
2006-01-27 05:33:08 +00:00
h . junk_self_referring_directories = 1
assert_equal ( " /./foo?a=1&b=2 " , h . uri , 'junk self referring directories' )
end
2005-07-24 20:53:54 +00:00
end