Skip to content

Commit

Permalink
Fixed tests to work under Windows.
Browse files Browse the repository at this point in the history
- Backported #process_based_port functionality form 1.2 to allow concurrency during CI.
- Make /tmp/testfile work with Windows.


git-svn-id: svn://rubyforge.org/var/svn/mongrel/branches/stable_1-1@1033 19e92222-5c0b-0410-8929-a290d50e31e9
  • Loading branch information
luislavena committed Jul 27, 2008
1 parent e6e0a9a commit 3ac4147
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
49 changes: 26 additions & 23 deletions test/test_handlers.rb
Expand Up @@ -34,9 +34,10 @@ def check_status(results, expecting)
class HandlersTest < Test::Unit::TestCase

def setup
@port = process_based_port
stats = Mongrel::StatisticsFilter.new(:sample_rate => 1)

@config = Mongrel::Configurator.new :host => '127.0.0.1', :port => 9998 do
@config = Mongrel::Configurator.new :host => '127.0.0.1', :port => @port do
listener do
uri "/", :handler => SimpleHandler.new
uri "/", :handler => stats
Expand All @@ -49,50 +50,52 @@ def setup
uri "/relative", :handler => Mongrel::DirHandler.new(nil, listing_allowed=false, index_html="none")
end
end

File.open("/tmp/testfile", 'w') do

@test_file = windows? ? "testfile" : "tmp/testfile"

File.open("/#{@test_file}", 'w') do
# Do nothing
end

@config.run
end

def teardown
@config.stop(false, true)
File.delete "/tmp/testfile"
File.delete "/#{@test_file}"
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",
res = hit([ "http://localhost:#{@port}/test",
"http://localhost:#{@port}/dumb",
"http://localhost:#{@port}/404",
"http://localhost:#{@port}/files/rdoc/index.html",
"http://localhost:#{@port}/files/rdoc/nothere.html",
"http://localhost:#{@port}/files/rdoc/",
"http://localhost:#{@port}/files_nodir/rdoc/",
"http://localhost:#{@port}/status",
])
check_status res, String
end

def test_nil_dirhandler
# Camping uses this internally
handler = Mongrel::DirHandler.new(nil, false)
assert handler.can_serve("/tmp/testfile")
handler = Mongrel::DirHandler.new(nil, false)
assert handler.can_serve("/#{@test_file}")
# Not a bug! A nil @file parameter is the only circumstance under which
# we are allowed to serve any existing file
assert handler.can_serve("../../../../../../../../../../tmp/testfile")
assert handler.can_serve("../../../../../../../../../../#{@test_file}")
end

def test_non_nil_dirhandler_is_not_vulnerable_to_path_traversal
# The famous security bug of Mongrel 1.1.2
handler = Mongrel::DirHandler.new("/doc", false)
assert_nil handler.can_serve("/tmp/testfile")
assert_nil handler.can_serve("../../../../../../../../../../tmp/testfile")
assert_nil handler.can_serve("/#{@test_file}")
assert_nil handler.can_serve("../../../../../../../../../../#{@test_file}")
end

def test_deflate
Net::HTTP.start("localhost", 9998) do |h|
Net::HTTP.start("localhost", @port) do |h|
# Test that no accept-encoding returns a non-deflated response
req = h.get("/dumb")
assert(
Expand All @@ -110,14 +113,14 @@ def test_deflate

# 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 = Net::HTTP::Post.new("http://localhost:#{@port}/files/rdoc/")
# req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
# res = hit [["http://localhost:9998/files/rdoc/",req]]
# res = hit [["http://localhost:#{@port}/files/rdoc/",req]]
# check_status res, Net::HTTPNotFound
#end

def test_unregister
@config.listeners["127.0.0.1:9998"].unregister("/")
@config.listeners["127.0.0.1:#{@port}"].unregister("/")
end
end

13 changes: 13 additions & 0 deletions test/testhelp.rb
Expand Up @@ -64,3 +64,16 @@ def hit(uris)

return results
end

# process_based_port provides a port number, usable for TCP and UDP
# connections based on $$ and with a 6000 as base.
# this is required if you perform several builds of mongrel in parallel
# (like continuous integration systems)
def process_based_port
6000 + $$ % 1000
end

# Platform check helper ;-)
def windows?
result = RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/
end

0 comments on commit 3ac4147

Please sign in to comment.