OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / test / cgi / test_cgi_header.rb
1 require 'test/unit'
2 require 'cgi'
3
4
5 class CGIHeaderTest < Test::Unit::TestCase
6
7
8   def setup
9     @environ = {
10       'SERVER_PROTOCOL' => 'HTTP/1.1',
11       'REQUEST_METHOD'  => 'GET',
12       'SERVER_SOFTWARE' => 'Apache 2.2.0',
13     }
14     ENV.update(@environ)
15   end
16
17
18   def teardown
19     @environ.each do |key, val| ENV.delete(key) end
20   end
21
22
23   def test_cgi_header_simple
24     cgi = CGI.new
25     ## default content type
26     expected = "Content-Type: text/html\r\n\r\n"
27     actual = cgi.header
28     assert_equal(expected, actual)
29     ## content type specified as string
30     expected = "Content-Type: text/xhtml; charset=utf8\r\n\r\n"
31     actual = cgi.header('text/xhtml; charset=utf8')
32     assert_equal(expected, actual)
33     ## content type specified as hash
34     expected = "Content-Type: image/png\r\n\r\n"
35     actual = cgi.header('type'=>'image/png')
36     assert_equal(expected, actual)
37     ## charset specified
38     expected = "Content-Type: text/html; charset=utf8\r\n\r\n"
39     actual = cgi.header('charset'=>'utf8')
40     assert_equal(expected, actual)
41   end
42
43
44   def test_cgi_header_complex
45     cgi = CGI.new
46     options = {
47       'type'       => 'text/xhtml',
48       'charset'    => 'utf8',
49       'status'     => 'REDIRECT',
50       'server'     => 'webrick',
51       'connection' => 'close',
52       'length'     => 123,
53       'language'   => 'ja',
54       'expires'    => Time.gm(2000, 1, 23, 12, 34, 56),
55       'location'   => 'http://www.ruby-lang.org/',
56     }
57     expected =  "Status: 302 Found\r\n"
58     expected << "Server: webrick\r\n"
59     expected << "Connection: close\r\n"
60     expected << "Content-Type: text/xhtml; charset=utf8\r\n"
61     expected << "Content-Length: 123\r\n"
62     expected << "Content-Language: ja\r\n"
63     expected << "Expires: Sun, 23 Jan 2000 12:34:56 GMT\r\n"
64     expected << "location: http://www.ruby-lang.org/\r\n"
65     expected << "\r\n"
66     actual = cgi.header(options)
67     assert_equal(expected, actual)
68   end
69
70
71   def test_cgi_header_argerr
72     cgi = CGI.new
73     #expected = NoMethodError  # must be ArgumentError
74     if RUBY_VERSION>="1.9.0"
75       expected = ArgumentError   # for CGIAlt
76     else
77       expected = NoMethodError   # for Ruby1.8
78     end
79     ex = assert_raise(expected) do
80       cgi.header(nil)
81     end
82   end
83
84
85   def test_cgi_header_cookie
86     cgi = CGI.new
87     cookie1 = CGI::Cookie.new('name1', 'abc', '123')
88     cookie2 = CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true)
89     ctype = "Content-Type: text/html\r\n"
90     sep   = "\r\n"
91     c1    = "Set-Cookie: name1=abc&123; path=\r\n"
92     c2    = "Set-Cookie: name2=value2; path=; secure\r\n"
93     ## CGI::Cookie object
94     actual = cgi.header('cookie'=>cookie1)
95     expected = ctype + c1 + sep
96     assert_equal(expected, actual)
97     ## String
98     actual = cgi.header('cookie'=>cookie2.to_s)
99     expected = ctype + c2 + sep
100     assert_equal(expected, actual)
101     ## Array
102     actual = cgi.header('cookie'=>[cookie1, cookie2])
103     expected = ctype + c1 + c2 + sep
104     assert_equal(expected, actual)
105     ## Hash
106     actual = cgi.header('cookie'=>{'name1'=>cookie1, 'name2'=>cookie2})
107     expected = ctype + c1 + c2 + sep
108     assert_equal(expected, actual)
109   end
110
111
112   def test_cgi_header_output_cookies
113     cgi = CGI.new
114     ## output cookies
115     cookies = [ CGI::Cookie.new('name1', 'abc', '123'),
116                 CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true),
117               ]
118     cgi.instance_variable_set('@output_cookies', cookies)
119     expected =  "Content-Type: text/html; charset=utf8\r\n"
120     expected << "Set-Cookie: name1=abc&123; path=\r\n"
121     expected << "Set-Cookie: name2=value2; path=; secure\r\n"
122     expected << "\r\n"
123     ## header when string
124     actual = cgi.header('text/html; charset=utf8')
125     assert_equal(expected, actual)
126     ## _header_for_string
127     actual = cgi.header('type'=>'text/html', 'charset'=>'utf8')
128     assert_equal(expected, actual)
129   end
130
131
132   def test_cgi_header_nph
133     cgi = CGI.new
134     ## 'nph' is true
135     ENV['SERVER_SOFTWARE'] = 'Apache 2.2.0'
136     actual1 = cgi.header('nph'=>true)
137     ## when old IIS, NPH-mode is forced
138     ENV['SERVER_SOFTWARE'] = 'IIS/4.0'
139     actual2 = cgi.header
140     actual3 = cgi.header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
141     ## newer IIS doesn't require NPH-mode   ## [ruby-dev:30537]
142     ENV['SERVER_SOFTWARE'] = 'IIS/5.0'
143     actual4 = cgi.header
144     actual5 = cgi.header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
145     ## assertion
146     now = Time.now
147     expected =  "HTTP/1.1 200 OK\r\n"
148     expected << "Date: #{CGI.rfc1123_date(now)}\r\n"
149     expected << "Server: Apache 2.2.0\r\n"
150     expected << "Connection: close\r\n"
151     expected << "Content-Type: text/html\r\n"
152     expected << "\r\n"
153     assert_equal(expected, actual1)
154     expected.sub!(/^Server: .*?\r\n/, "Server: IIS/4.0\r\n")
155     assert_equal(expected, actual2)
156     expected.sub!(/^HTTP\/1.1 200 OK\r\n/, "HTTP/1.1 302 Found\r\n")
157     expected.sub!(/\r\n\r\n/, "\r\nlocation: http://www.example.com/\r\n\r\n")
158     assert_equal(expected, actual3)
159     expected =  "Content-Type: text/html\r\n"
160     expected << "\r\n"
161     assert_equal(expected, actual4)
162     expected =  "Status: 302 Found\r\n"
163     expected << "Content-Type: text/html\r\n"
164     expected << "location: http://www.example.com/\r\n"
165     expected << "\r\n"
166     assert_equal(expected, actual5)
167   ensure
168     ENV.delete('SERVER_SOFTWARE')
169   end
170
171
172
173   instance_methods.each do |method|
174     private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
175   end if ENV['TEST']
176
177 end