1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/test/test_handlers.rb
zedshaw ddd5c9a46c Ruby license applied to all files
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@267 19e92222-5c0b-0410-8929-a290d50e31e9
2006-06-30 20:42:12 +00:00

93 lines
2.8 KiB
Ruby

# Copyright (c) 2005 Zed A. Shaw
# You can redistribute it and/or modify it under the same terms as Ruby.
#
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
# for more information.
require 'test/unit'
require 'net/http'
require 'mongrel'
require 'timeout'
require File.dirname(__FILE__) + "/testhelp.rb"
class SimpleHandler < Mongrel::HttpHandler
def process(request, response)
response.start do |head,out|
head["Content-Type"] = "text/html"
results = "<html><body>Your request:<br /><pre>#{request.params.to_yaml}</pre><a href=\"/files\">View the files.</a></body></html>"
out << results
end
end
end
class DumbHandler < Mongrel::HttpHandler
def process(request, response)
response.start do |head,out|
head["Content-Type"] = "text/html"
out.write("test")
end
end
end
def check_status(results, expecting)
results.each do |res|
assert(res.kind_of?(expecting), "Didn't get #{expecting}, got: #{res.class}")
end
end
class HandlersTest < Test::Unit::TestCase
def setup
stats = Mongrel::StatisticsFilter.new(:sample_rate => 1)
@config = Mongrel::Configurator.new :host => '127.0.0.1', :port => 9998 do
listener do
uri "/", :handler => SimpleHandler.new
uri "/", :handler => stats
uri "/404", :handler => Mongrel::Error404Handler.new("Not found")
uri "/dumb", :handler => Mongrel::DeflateFilter.new(:always_deflate => true)
uri "/dumb", :handler => DumbHandler.new, :in_front => true
uri "/files", :handler => Mongrel::DirHandler.new("doc")
uri "/files_nodir", :handler => Mongrel::DirHandler.new("doc",listing_allowed=false, index_html="none")
uri "/status", :handler => Mongrel::StatusHandler.new(:stats_filter => stats)
end
end
@config.run
end
def teardown
@config.stop
end
def test_more_web_server
res = hit([ "http://localhost:9998/test",
"http://localhost:9998/dumb",
"http://localhost:9998/404",
"http://localhost:9998/files/rdoc/index.html",
"http://localhost:9998/files/rdoc/nothere.html",
"http://localhost:9998/files/rdoc/",
"http://localhost:9998/files_nodir/rdoc/",
"http://localhost:9998/status",
])
check_status res, String
end
def test_deflate_access
req = Net::HTTP::Get.new("http://localhost:9998/dumb")
end
# TODO: find out why this fails on win32 but nowhere else
#
#def test_posting_fails_dirhandler
# req = Net::HTTP::Post.new("http://localhost:9998/files/rdoc/")
# req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
# res = hit [["http://localhost:9998/files/rdoc/",req]]
# check_status res, Net::HTTPNotFound
#end
def test_unregister
@config.listeners["127.0.0.1:9998"].unregister("/")
end
end