Updated MSpec source to 1c3ee1c8.
[rbx.git] / test / mri / webrick / test_cgi.rb
blob7e3f4ee5f069a29e2f5d7466f5f5eab82c8d0ec5
1 require "webrick"
2 require File.join(File.dirname(__FILE__), "utils.rb")
3 require "test/unit"
4 begin
5   loadpath = $:.dup
6   $:.replace($: | [File.expand_path("../ruby", File.dirname(__FILE__))])
7   require 'envutil'
8 ensure
9   $:.replace(loadpath)
10 end
12 class TestWEBrickCGI < Test::Unit::TestCase
13   def test_cgi
14     accepted = started = stopped = 0
15     requested0 = requested1 = 0
16     config = {
17       :CGIInterpreter => EnvUtil.rubybin,
18       :DocumentRoot => File.dirname(__FILE__),
19       :DirectoryIndex => ["webrick.cgi"],
20     }
21     if RUBY_PLATFORM =~ /mswin32|mingw|cygwin|bccwin32/
22       config[:CGIPathEnv] = ENV['PATH'] # runtime dll may not be in system dir.
23     end
24     TestWEBrick.start_httpserver(config){|server, addr, port|
25       http = Net::HTTP.new(addr, port)
26       req = Net::HTTP::Get.new("/webrick.cgi")
27       http.request(req){|res| assert_equal("/webrick.cgi", res.body)}
28       req = Net::HTTP::Get.new("/webrick.cgi/path/info")
29       http.request(req){|res| assert_equal("/path/info", res.body)}
30       req = Net::HTTP::Get.new("/webrick.cgi/%3F%3F%3F?foo=bar")
31       http.request(req){|res| assert_equal("/???", res.body)}
32       req = Net::HTTP::Get.new("/webrick.cgi/%A4%DB%A4%B2/%A4%DB%A4%B2")
33       http.request(req){|res|
34         assert_equal("/\xA4\xDB\xA4\xB2/\xA4\xDB\xA4\xB2", res.body)}
35       req = Net::HTTP::Get.new("/webrick.cgi?a=1;a=2;b=x")
36       http.request(req){|res| assert_equal("a=1, a=2, b=x", res.body)}
37       req = Net::HTTP::Get.new("/webrick.cgi?a=1&a=2&b=x")
38       http.request(req){|res| assert_equal("a=1, a=2, b=x", res.body)}
40       req = Net::HTTP::Post.new("/webrick.cgi?a=x;a=y;b=1")
41       req["Content-Type"] = "application/x-www-form-urlencoded"
42       http.request(req, "a=1;a=2;b=x"){|res|
43         assert_equal("a=1, a=2, b=x", res.body)}
44       req = Net::HTTP::Post.new("/webrick.cgi?a=x&a=y&b=1")
45       req["Content-Type"] = "application/x-www-form-urlencoded"
46       http.request(req, "a=1&a=2&b=x"){|res|
47         assert_equal("a=1, a=2, b=x", res.body)}
48       req = Net::HTTP::Get.new("/")
49       http.request(req){|res|
50         ary = res.body.to_a
51         assert_match(%r{/$}, ary[0])
52         assert_match(%r{/webrick.cgi$}, ary[1])
53       }
55       req = Net::HTTP::Get.new("/webrick.cgi")
56       req["Cookie"] = "CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001"
57       http.request(req){|res|
58         assert_equal(
59           "CUSTOMER=WILE_E_COYOTE\nPART_NUMBER=ROCKET_LAUNCHER_0001\n",
60           res.body)
61       }
63       req = Net::HTTP::Get.new("/webrick.cgi")
64       cookie =  %{$Version="1"; }
65       cookie << %{Customer="WILE_E_COYOTE"; $Path="/acme"; }
66       cookie << %{Part_Number="Rocket_Launcher_0001"; $Path="/acme"; }
67       cookie << %{Shipping="FedEx"; $Path="/acme"}
68       req["Cookie"] = cookie
69       http.request(req){|res|
70         assert_equal("Customer=WILE_E_COYOTE, Shipping=FedEx",
71                      res["Set-Cookie"])
72         assert_equal("Customer=WILE_E_COYOTE\n" +
73                      "Part_Number=Rocket_Launcher_0001\n" +
74                      "Shipping=FedEx\n", res.body)
75       }
76     }
77   end
78 end