2005-01-10 01:29:58 -05:00
|
|
|
require "webrick"
|
|
|
|
require File.join(File.dirname(__FILE__), "utils.rb")
|
|
|
|
require "test/unit"
|
|
|
|
begin
|
|
|
|
loadpath = $:.dup
|
|
|
|
$:.replace($: | [File.expand_path("../ruby", File.dirname(__FILE__))])
|
|
|
|
require 'envutil'
|
|
|
|
ensure
|
|
|
|
$:.replace(loadpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestWEBrickCGI < Test::Unit::TestCase
|
|
|
|
def test_cgi
|
|
|
|
accepted = started = stopped = 0
|
|
|
|
requested0 = requested1 = 0
|
|
|
|
config = {
|
|
|
|
:CGIInterpreter => EnvUtil.rubybin,
|
|
|
|
:DocumentRoot => File.dirname(__FILE__),
|
2005-05-27 13:16:06 -04:00
|
|
|
:DirectoryIndex => ["webrick.cgi"],
|
2006-02-22 02:27:20 -05:00
|
|
|
:RequestHandler => Proc.new{|req, res|
|
|
|
|
def req.meta_vars
|
|
|
|
meta = super
|
|
|
|
meta["RUBYLIB"] = $:.join(File::PATH_SEPARATOR)
|
|
|
|
return meta
|
|
|
|
end
|
|
|
|
},
|
2005-01-10 01:29:58 -05:00
|
|
|
}
|
2005-09-20 04:35:52 -04:00
|
|
|
if RUBY_PLATFORM =~ /mswin32|mingw|cygwin|bccwin32/
|
|
|
|
config[:CGIPathEnv] = ENV['PATH'] # runtime dll may not be in system dir.
|
|
|
|
end
|
2005-01-10 01:29:58 -05:00
|
|
|
TestWEBrick.start_httpserver(config){|server, addr, port|
|
|
|
|
http = Net::HTTP.new(addr, port)
|
|
|
|
req = Net::HTTP::Get.new("/webrick.cgi")
|
|
|
|
http.request(req){|res| assert_equal("/webrick.cgi", res.body)}
|
|
|
|
req = Net::HTTP::Get.new("/webrick.cgi/path/info")
|
|
|
|
http.request(req){|res| assert_equal("/path/info", res.body)}
|
|
|
|
req = Net::HTTP::Get.new("/webrick.cgi/%3F%3F%3F?foo=bar")
|
|
|
|
http.request(req){|res| assert_equal("/???", res.body)}
|
|
|
|
req = Net::HTTP::Get.new("/webrick.cgi/%A4%DB%A4%B2/%A4%DB%A4%B2")
|
|
|
|
http.request(req){|res|
|
|
|
|
assert_equal("/\xA4\xDB\xA4\xB2/\xA4\xDB\xA4\xB2", res.body)}
|
|
|
|
req = Net::HTTP::Get.new("/webrick.cgi?a=1;a=2;b=x")
|
|
|
|
http.request(req){|res| assert_equal("a=1, a=2, b=x", res.body)}
|
|
|
|
req = Net::HTTP::Get.new("/webrick.cgi?a=1&a=2&b=x")
|
|
|
|
http.request(req){|res| assert_equal("a=1, a=2, b=x", res.body)}
|
|
|
|
|
|
|
|
req = Net::HTTP::Post.new("/webrick.cgi?a=x;a=y;b=1")
|
|
|
|
req["Content-Type"] = "application/x-www-form-urlencoded"
|
|
|
|
http.request(req, "a=1;a=2;b=x"){|res|
|
|
|
|
assert_equal("a=1, a=2, b=x", res.body)}
|
|
|
|
req = Net::HTTP::Post.new("/webrick.cgi?a=x&a=y&b=1")
|
|
|
|
req["Content-Type"] = "application/x-www-form-urlencoded"
|
|
|
|
http.request(req, "a=1&a=2&b=x"){|res|
|
|
|
|
assert_equal("a=1, a=2, b=x", res.body)}
|
2005-05-27 13:16:06 -04:00
|
|
|
req = Net::HTTP::Get.new("/")
|
|
|
|
http.request(req){|res|
|
2006-11-01 19:21:28 -05:00
|
|
|
ary = res.body.lines.to_a
|
2005-05-27 13:16:06 -04:00
|
|
|
assert_match(%r{/$}, ary[0])
|
|
|
|
assert_match(%r{/webrick.cgi$}, ary[1])
|
|
|
|
}
|
2005-10-30 15:40:05 -05:00
|
|
|
|
|
|
|
req = Net::HTTP::Get.new("/webrick.cgi")
|
|
|
|
req["Cookie"] = "CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001"
|
|
|
|
http.request(req){|res|
|
|
|
|
assert_equal(
|
|
|
|
"CUSTOMER=WILE_E_COYOTE\nPART_NUMBER=ROCKET_LAUNCHER_0001\n",
|
|
|
|
res.body)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = Net::HTTP::Get.new("/webrick.cgi")
|
|
|
|
cookie = %{$Version="1"; }
|
|
|
|
cookie << %{Customer="WILE_E_COYOTE"; $Path="/acme"; }
|
|
|
|
cookie << %{Part_Number="Rocket_Launcher_0001"; $Path="/acme"; }
|
|
|
|
cookie << %{Shipping="FedEx"; $Path="/acme"}
|
|
|
|
req["Cookie"] = cookie
|
|
|
|
http.request(req){|res|
|
|
|
|
assert_equal("Customer=WILE_E_COYOTE, Shipping=FedEx",
|
|
|
|
res["Set-Cookie"])
|
|
|
|
assert_equal("Customer=WILE_E_COYOTE\n" +
|
|
|
|
"Part_Number=Rocket_Launcher_0001\n" +
|
|
|
|
"Shipping=FedEx\n", res.body)
|
|
|
|
}
|
2005-01-10 01:29:58 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|