2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-07-25 02:18:37 +00:00
|
|
|
|
|
|
|
$:.unshift(File.join(File.dirname(__FILE__), '..', '..', '..'))
|
|
|
|
|
|
|
|
require 'test/unit'
|
|
|
|
require 'rex/proto/http'
|
|
|
|
|
|
|
|
class Rex::Proto::Http::Server::UnitTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
ListenPort = 8090
|
|
|
|
ListenHost = '127.0.0.1'
|
|
|
|
|
|
|
|
SrvKlass = Rex::Proto::Http::Server
|
|
|
|
CliKlass = Rex::Proto::Http::Client
|
|
|
|
|
|
|
|
def test_server
|
|
|
|
begin
|
|
|
|
s = start_srv
|
|
|
|
c = CliKlass.new(ListenHost, ListenPort)
|
|
|
|
|
|
|
|
1.upto(10) {
|
2006-12-19 07:11:55 +00:00
|
|
|
req = c.request_raw('uri' => '/')
|
|
|
|
res = c.send_recv(req)
|
2005-07-25 02:18:37 +00:00
|
|
|
assert_not_nil(res)
|
|
|
|
assert_equal(404, res.code)
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
stop_srv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-25 02:39:14 +00:00
|
|
|
def test_resource
|
|
|
|
begin
|
|
|
|
s = start_srv
|
|
|
|
c = CliKlass.new(ListenHost, ListenPort)
|
|
|
|
|
2005-09-21 04:48:37 +00:00
|
|
|
s.add_resource('/foo',
|
|
|
|
'Proc' => Proc.new { |cli, req|
|
|
|
|
resp = Rex::Proto::Http::Response::OK.new
|
2005-07-25 02:39:14 +00:00
|
|
|
|
2005-09-21 04:48:37 +00:00
|
|
|
resp.body = "Chickens everywhere"
|
|
|
|
|
|
|
|
cli.send_response(resp)
|
|
|
|
})
|
2005-07-25 02:39:14 +00:00
|
|
|
|
|
|
|
1.upto(10) {
|
2006-12-19 07:11:55 +00:00
|
|
|
req = c.request_raw('uri' => '/foo')
|
|
|
|
res = c.send_recv(req)
|
2005-07-25 02:39:14 +00:00
|
|
|
assert_not_nil(res)
|
|
|
|
assert_equal(200, res.code)
|
|
|
|
assert_equal("Chickens everywhere", res.body)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.remove_resource('/foo')
|
2005-11-24 02:02:10 +00:00
|
|
|
|
2006-12-19 07:11:55 +00:00
|
|
|
req = c.request_raw('uri' => '/foo')
|
|
|
|
res = c.send_recv(req)
|
2005-09-21 04:48:37 +00:00
|
|
|
assert_not_nil(res)
|
|
|
|
assert_equal(404, res.code)
|
2005-07-25 02:39:14 +00:00
|
|
|
ensure
|
|
|
|
stop_srv
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-25 02:18:37 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def start_srv
|
|
|
|
self.srv = SrvKlass.new(ListenPort, ListenHost)
|
|
|
|
self.srv.start
|
|
|
|
self.srv
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_srv
|
|
|
|
self.srv.stop if (self.srv)
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :srv
|
|
|
|
|
2008-10-19 21:03:39 +00:00
|
|
|
end
|