5 class CGIHeaderTest < Test::Unit::TestCase
10 'SERVER_PROTOCOL' => 'HTTP/1.1',
11 'REQUEST_METHOD' => 'GET',
12 'SERVER_SOFTWARE' => 'Apache 2.2.0',
19 @environ.each do |key, val| ENV.delete(key) end
23 def test_cgi_header_simple
25 ## default content type
26 expected = "Content-Type: text/html\r\n\r\n"
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)
38 expected = "Content-Type: text/html; charset=utf8\r\n\r\n"
39 actual = cgi.header('charset'=>'utf8')
40 assert_equal(expected, actual)
44 def test_cgi_header_complex
47 'type' => 'text/xhtml',
49 'status' => 'REDIRECT',
50 'server' => 'webrick',
51 'connection' => 'close',
54 'expires' => Time.gm(2000, 1, 23, 12, 34, 56),
55 'location' => 'http://www.ruby-lang.org/',
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"
66 actual = cgi.header(options)
67 assert_equal(expected, actual)
71 def test_cgi_header_argerr
73 #expected = NoMethodError # must be ArgumentError
74 if RUBY_VERSION>="1.9.0"
75 expected = ArgumentError # for CGIAlt
77 expected = NoMethodError # for Ruby1.8
79 ex = assert_raise(expected) do
85 def test_cgi_header_cookie
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"
91 c1 = "Set-Cookie: name1=abc&123; path=\r\n"
92 c2 = "Set-Cookie: name2=value2; path=; secure\r\n"
94 actual = cgi.header('cookie'=>cookie1)
95 expected = ctype + c1 + sep
96 assert_equal(expected, actual)
98 actual = cgi.header('cookie'=>cookie2.to_s)
99 expected = ctype + c2 + sep
100 assert_equal(expected, actual)
102 actual = cgi.header('cookie'=>[cookie1, cookie2])
103 expected = ctype + c1 + c2 + sep
104 assert_equal(expected, actual)
106 actual = cgi.header('cookie'=>{'name1'=>cookie1, 'name2'=>cookie2})
107 expected = ctype + c1 + c2 + sep
108 assert_equal(expected, actual)
112 def test_cgi_header_output_cookies
115 cookies = [ CGI::Cookie.new('name1', 'abc', '123'),
116 CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true),
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"
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)
132 def test_cgi_header_nph
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'
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'
144 actual5 = cgi.header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
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"
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"
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"
166 assert_equal(expected, actual5)
168 ENV.delete('SERVER_SOFTWARE')
173 instance_methods.each do |method|
174 private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']