Skip to content

Commit

Permalink
Merge pull request #128 from KJTsanaktsidis/ktsanaktsidis/fix_webrick…
Browse files Browse the repository at this point in the history
…_cgi_test

Fix WEBrick::TestFileHandler#test_short_filename test not working on mswin
  • Loading branch information
KJTsanaktsidis authored Dec 27, 2023
2 parents 15e5ca9 + 3704b62 commit 1d156e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
32 changes: 5 additions & 27 deletions test/webrick/test_cgi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,8 @@ def teardown
super
end

def start_cgi_server(log_tester=TestWEBrick::DefaultLogTester, &block)
config = {
:CGIInterpreter => TestWEBrick::RubyBin,
:DocumentRoot => File.dirname(__FILE__),
:DirectoryIndex => ["webrick.cgi"],
:RequestCallback => Proc.new{|req, res|
def req.meta_vars
meta = super
meta["RUBYLIB"] = $:.join(File::PATH_SEPARATOR)
meta[RbConfig::CONFIG['LIBPATHENV']] = ENV[RbConfig::CONFIG['LIBPATHENV']] if RbConfig::CONFIG['LIBPATHENV']
return meta
end
},
}
if RUBY_PLATFORM =~ /mswin|mingw|cygwin|bccwin32/
config[:CGIPathEnv] = ENV['PATH'] # runtime dll may not be in system dir.
end
TestWEBrick.start_httpserver(config, log_tester){|server, addr, port, log|
block.call(server, addr, port, log)
}
end

def test_cgi
start_cgi_server{|server, addr, port, log|
TestWEBrick.start_cgi_server{|server, addr, port, log|
http = Net::HTTP.new(addr, port)
req = Net::HTTP::Get.new("/webrick.cgi")
http.request(req){|res| assert_equal("/webrick.cgi", res.body, log.call)}
Expand Down Expand Up @@ -98,7 +76,7 @@ def test_bad_request
log_tester = lambda {|log, access_log|
assert_match(/BadRequest/, log.join)
}
start_cgi_server(log_tester) {|server, addr, port, log|
TestWEBrick.start_cgi_server({}, log_tester) {|server, addr, port, log|
sock = TCPSocket.new(addr, port)
begin
sock << "POST /webrick.cgi HTTP/1.0" << CRLF
Expand All @@ -115,7 +93,7 @@ def test_bad_request
end

def test_cgi_env
start_cgi_server do |server, addr, port, log|
TestWEBrick.start_cgi_server do |server, addr, port, log|
http = Net::HTTP.new(addr, port)
req = Net::HTTP::Get.new("/webrick.cgi/dumpenv")
req['proxy'] = 'http://example.com/'
Expand All @@ -137,7 +115,7 @@ def test_bad_uri
assert_equal(1, log.length)
assert_match(/ERROR bad URI/, log[0])
}
start_cgi_server(log_tester) {|server, addr, port, log|
TestWEBrick.start_cgi_server({}, log_tester) {|server, addr, port, log|
res = TCPSocket.open(addr, port) {|sock|
sock << "GET /#{CtrlSeq}#{CRLF}#{CRLF}"
sock.close_write
Expand All @@ -155,7 +133,7 @@ def test_bad_header
assert_equal(1, log.length)
assert_match(/ERROR bad header/, log[0])
}
start_cgi_server(log_tester) {|server, addr, port, log|
TestWEBrick.start_cgi_server({}, log_tester) {|server, addr, port, log|
res = TCPSocket.open(addr, port) {|sock|
sock << "GET / HTTP/1.0#{CRLF}#{CtrlSeq}#{CRLF}#{CRLF}"
sock.close_write
Expand Down
9 changes: 2 additions & 7 deletions test/webrick/test_filehandler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,15 @@ def test_unwise_in_path
def test_short_filename
return if File.executable?(__FILE__) # skip on strange file system

config = {
:CGIInterpreter => TestWEBrick::RubyBin,
:DocumentRoot => File.dirname(__FILE__),
:CGIPathEnv => ENV['PATH'],
}
log_tester = lambda {|log, access_log|
log = log.reject {|s| /ERROR `.*\' not found\./ =~ s }
log = log.reject {|s| /WARN the request refers nondisclosure name/ =~ s }
assert_equal([], log)
}
TestWEBrick.start_httpserver(config, log_tester) do |server, addr, port, log|
TestWEBrick.start_cgi_server({}, log_tester) do |server, addr, port, log|
http = Net::HTTP.new(addr, port)
if windows?
root = config[:DocumentRoot].tr("/", "\\")
root = File.dirname(__FILE__).tr("/", "\\")
fname = IO.popen(%W[dir /x #{root}\\webrick_long_filename.cgi], encoding: "binary", &:read)
fname.sub!(/\A.*$^$.*$^$/m, '')
if fname
Expand Down
20 changes: 20 additions & 0 deletions test/webrick/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,24 @@ def start_httpserver(config={}, log_tester=DefaultLogTester, &block)
def start_httpproxy(config={}, log_tester=DefaultLogTester, &block)
start_server(WEBrick::HTTPProxyServer, config, log_tester, &block)
end

def start_cgi_server(config={}, log_tester=TestWEBrick::DefaultLogTester, &block)
config = {
:CGIInterpreter => TestWEBrick::RubyBin,
:DocumentRoot => File.dirname(__FILE__),
:DirectoryIndex => ["webrick.cgi"],
:RequestCallback => Proc.new{|req, res|
def req.meta_vars
meta = super
meta["RUBYLIB"] = $:.join(File::PATH_SEPARATOR)
meta[RbConfig::CONFIG['LIBPATHENV']] = ENV[RbConfig::CONFIG['LIBPATHENV']] if RbConfig::CONFIG['LIBPATHENV']
return meta
end
},
}.merge(config)
if RUBY_PLATFORM =~ /mswin|mingw|cygwin|bccwin32/
config[:CGIPathEnv] = ENV['PATH'] # runtime dll may not be in system dir.
end
start_server(WEBrick::HTTPServer, config, log_tester, &block)
end
end

0 comments on commit 1d156e3

Please sign in to comment.