cgialt 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +137 -0
- data/bench.rb +245 -0
- data/lib/cgialt.rb +43 -0
- data/lib/cgialt/cookie.rb +245 -0
- data/lib/cgialt/core.rb +1463 -0
- data/lib/cgialt/html.rb +1030 -0
- data/lib/cgialt/util.rb +166 -0
- data/setup.rb +1331 -0
- data/test/test_cgi_cookie.rb +108 -0
- data/test/test_cgi_core.rb +305 -0
- data/test/test_cgi_header.rb +178 -0
- data/test/test_cgi_modruby.rb +146 -0
- data/test/test_cgi_multipart.rb +295 -0
- data/test/testdata/file1.html +10 -0
- data/test/testdata/large.png +0 -0
- data/test/testdata/small.png +0 -0
- metadata +63 -0
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
#require 'cgi'
|
3
|
+
require(ENV['CGI'] || 'cgi')
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
|
7
|
+
class CGICookieTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
|
10
|
+
def setup
|
11
|
+
ENV['REQUEST_METHOD'] = 'GET'
|
12
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
%W[REQUEST_METHOD SCRIPT_NAME].each do |name|
|
16
|
+
ENV.delete(name)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def test_cgi_cookie_new_simple
|
22
|
+
cookie = CGI::Cookie.new('name1', 'val1', '&<>"', "\245\340\245\271\245\253")
|
23
|
+
assert_equal('name1', cookie.name)
|
24
|
+
assert_equal(['val1', '&<>"', "\245\340\245\271\245\253"], cookie.value)
|
25
|
+
assert_nil(cookie.domain)
|
26
|
+
assert_nil(cookie.expires)
|
27
|
+
assert_equal('', cookie.path)
|
28
|
+
assert_equal(false, cookie.secure)
|
29
|
+
assert_equal("name1=val1&%26%3C%3E%22&%A5%E0%A5%B9%A5%AB; path=", cookie.to_s)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def test_cgi_cookie_new_complex
|
34
|
+
t = Time.gm(2030, 12, 31, 23, 59, 59)
|
35
|
+
value = ['val1', '&<>"', "\245\340\245\271\245\253"]
|
36
|
+
cookie = CGI::Cookie.new('name'=>'name1',
|
37
|
+
'value'=>value,
|
38
|
+
'path'=>'/cgi-bin/myapp/',
|
39
|
+
'domain'=>'www.example.com',
|
40
|
+
'expires'=>t,
|
41
|
+
'secure'=>true
|
42
|
+
)
|
43
|
+
assert_equal('name1', cookie.name)
|
44
|
+
assert_equal(value, cookie.value)
|
45
|
+
assert_equal('www.example.com', cookie.domain)
|
46
|
+
assert_equal(t, cookie.expires)
|
47
|
+
assert_equal('/cgi-bin/myapp/', cookie.path)
|
48
|
+
assert_equal(true, cookie.secure)
|
49
|
+
assert_equal('name1=val1&%26%3C%3E%22&%A5%E0%A5%B9%A5%AB; domain=www.example.com; path=/cgi-bin/myapp/; expires=Tue, 31 Dec 2030 23:59:59 GMT; secure', cookie.to_s)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def test_cgi_cookie_scriptname
|
54
|
+
cookie = CGI::Cookie.new('name1', 'value1')
|
55
|
+
assert_equal('', cookie.path)
|
56
|
+
cookie = CGI::Cookie.new('name'=>'name1', 'value'=>'value1')
|
57
|
+
assert_equal('', cookie.path)
|
58
|
+
## when ENV['SCRIPT_NAME'] is set, cookie.path is set automatically
|
59
|
+
ENV['SCRIPT_NAME'] = '/cgi-bin/app/example.cgi'
|
60
|
+
cookie = CGI::Cookie.new('name1', 'value1')
|
61
|
+
assert_equal('/cgi-bin/app/', cookie.path)
|
62
|
+
cookie = CGI::Cookie.new('name'=>'name1', 'value'=>'value1')
|
63
|
+
assert_equal('/cgi-bin/app/', cookie.path)
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def test_cgi_cookie_parse
|
68
|
+
## ';' separator
|
69
|
+
cookie_str = 'name1=val1&val2; name2=val2&%26%3C%3E%22;_session_id=12345'
|
70
|
+
cookies = CGI::Cookie.parse(cookie_str)
|
71
|
+
list = [
|
72
|
+
['name1', ['val1', 'val2']],
|
73
|
+
['name2', ['val2', '&<>"']],
|
74
|
+
['_session_id', ['12345']],
|
75
|
+
]
|
76
|
+
list.each do |name, value|
|
77
|
+
cookie = cookies[name]
|
78
|
+
assert_equal(name, cookie.name)
|
79
|
+
assert_equal(value, cookie.value)
|
80
|
+
end
|
81
|
+
## ',' separator
|
82
|
+
cookie_str = 'name1=val1&val2, name2=val2&%26%3C%3E%22,_session_id=12345'
|
83
|
+
cookies = CGI::Cookie.parse(cookie_str)
|
84
|
+
list.each do |name, value|
|
85
|
+
cookie = cookies[name]
|
86
|
+
assert_equal(name, cookie.name)
|
87
|
+
assert_equal(value, cookie.value)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
def test_cgi_cookie_arrayinterface
|
93
|
+
cookie = CGI::Cookie.new('name1', 'a', 'b', 'c')
|
94
|
+
assert_equal('a', cookie[0])
|
95
|
+
assert_equal('c', cookie[2])
|
96
|
+
assert_nil(cookie[3])
|
97
|
+
assert_equal('a', cookie.first)
|
98
|
+
assert_equal('c', cookie.last)
|
99
|
+
assert_equal(['A', 'B', 'C'], cookie.collect{|e| e.upcase})
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
instance_methods.each do |method|
|
105
|
+
private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
|
106
|
+
end if ENV['TEST']
|
107
|
+
|
108
|
+
end
|
@@ -0,0 +1,305 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
#require 'cgi'
|
3
|
+
require(ENV['CGI'] || 'cgi')
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
|
7
|
+
class CGICoreTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
|
10
|
+
def setup
|
11
|
+
#@environ = {
|
12
|
+
# 'SERVER_PROTOCOL' => 'HTTP/1.1',
|
13
|
+
# 'REQUEST_METHOD' => 'GET',
|
14
|
+
# 'SERVER_SOFTWARE' => 'Apache 2.2.0',
|
15
|
+
#}
|
16
|
+
#ENV.update(@environ)
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def teardown
|
21
|
+
@environ.each do |key, val| ENV.delete(key) end
|
22
|
+
$stdout = STDOUT
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def test_cgi_core_params_GET
|
27
|
+
@environ = {
|
28
|
+
'REQUEST_METHOD' => 'GET',
|
29
|
+
'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
|
30
|
+
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
|
31
|
+
'SERVER_SOFTWARE' => 'Apache 2.2.0',
|
32
|
+
'SERVER_PROTOCOL' => 'HTTP/1.1',
|
33
|
+
}
|
34
|
+
ENV.update(@environ)
|
35
|
+
cgi = CGI.new
|
36
|
+
## cgi[]
|
37
|
+
assert_equal('123', cgi['id'])
|
38
|
+
assert_equal('@h =~ /^$/', cgi['str'])
|
39
|
+
## cgi[][], cgi[].first, cgi[].to_ary
|
40
|
+
$stderr = StringIO.new
|
41
|
+
begin
|
42
|
+
assert_equal('123', cgi['id'][0])
|
43
|
+
assert_equal('456', cgi['id'][1])
|
44
|
+
assert_equal('', cgi['id'][2])
|
45
|
+
assert_nil(cgi['id'][3])
|
46
|
+
assert_equal('@h =~ /^$/', cgi['str'][0])
|
47
|
+
assert_nil(cgi['str'][1])
|
48
|
+
assert_equal('123', cgi['id'].first)
|
49
|
+
assert_equal('123', cgi['id'].last) # should be '' ?
|
50
|
+
id1, id2, id3 = cgi['id']
|
51
|
+
assert_equal(['123', '456', ''], [id1, id2, id3])
|
52
|
+
str1, = cgi['str']
|
53
|
+
assert_equal('@h =~ /^$/', str1)
|
54
|
+
assert_not_same(str1, cgi['str']) # necessary?
|
55
|
+
ensure
|
56
|
+
$stderr = STDERR
|
57
|
+
end
|
58
|
+
## cgi.params
|
59
|
+
assert_equal(['123', '456', ''], cgi.params['id'])
|
60
|
+
assert_equal(['@h =~ /^$/'], cgi.params['str'])
|
61
|
+
## cgi.keys
|
62
|
+
assert_equal(['id', 'str'], cgi.keys.sort)
|
63
|
+
## cgi.key?, cgi.has_key?, cgi.include?
|
64
|
+
assert_equal(true, cgi.key?('id'))
|
65
|
+
assert_equal(true, cgi.has_key?('id'))
|
66
|
+
assert_equal(true, cgi.include?('id'))
|
67
|
+
assert_equal(false, cgi.key?('foo'))
|
68
|
+
assert_equal(false, cgi.has_key?('foo'))
|
69
|
+
assert_equal(false, cgi.include?('foo'))
|
70
|
+
## invalid parameter name
|
71
|
+
assert_equal('', cgi['*notfound*']) # [ruby-dev:30740]
|
72
|
+
assert_equal([], cgi.params['*notfound*'])
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
def test_cgi_core_params_POST
|
77
|
+
query_str = 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F'
|
78
|
+
@environ = {
|
79
|
+
'REQUEST_METHOD' => 'POST',
|
80
|
+
'CONTENT_LENGTH' => query_str.length.to_s,
|
81
|
+
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
|
82
|
+
'SERVER_SOFTWARE' => 'Apache 2.2.0',
|
83
|
+
'SERVER_PROTOCOL' => 'HTTP/1.1',
|
84
|
+
}
|
85
|
+
ENV.update(@environ)
|
86
|
+
$stdin = StringIO.new
|
87
|
+
$stdin << query_str
|
88
|
+
$stdin.rewind
|
89
|
+
cgi = CGI.new
|
90
|
+
## cgi[]
|
91
|
+
assert_equal('123', cgi['id'])
|
92
|
+
assert_equal('@h =~ /^$/', cgi['str'])
|
93
|
+
## cgi.params
|
94
|
+
assert_equal(['123', '456', ''], cgi.params['id'])
|
95
|
+
assert_equal(['@h =~ /^$/'], cgi.params['str'])
|
96
|
+
## invalid parameter name
|
97
|
+
assert_equal('', cgi['*notfound*'])
|
98
|
+
assert_equal([], cgi.params['*notfound*'])
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def test_cgi_core_cookie
|
103
|
+
@environ = {
|
104
|
+
'REQUEST_METHOD' => 'GET',
|
105
|
+
'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
|
106
|
+
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
|
107
|
+
'SERVER_SOFTWARE' => 'Apache 2.2.0',
|
108
|
+
'SERVER_PROTOCOL' => 'HTTP/1.1',
|
109
|
+
}
|
110
|
+
ENV.update(@environ)
|
111
|
+
cgi = CGI.new
|
112
|
+
assert_not_nil(cgi.cookies)
|
113
|
+
[ ['_session_id', ['12345'], ],
|
114
|
+
['name1', ['val1', 'val2'], ],
|
115
|
+
].each do |key, expected|
|
116
|
+
cookie = cgi.cookies[key]
|
117
|
+
assert_kind_of(CGI::Cookie, cookie)
|
118
|
+
assert_equal(expected, cookie.value)
|
119
|
+
assert_equal(false, cookie.secure)
|
120
|
+
assert_nil(cookie.expires)
|
121
|
+
assert_nil(cookie.domain)
|
122
|
+
assert_equal('', cookie.path)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
def test_cgi_core_maxcontentlength
|
128
|
+
@environ = {
|
129
|
+
'REQUEST_METHOD' => 'POST',
|
130
|
+
'CONTENT_LENGTH' => (64 * 1024 * 1024).to_s
|
131
|
+
}
|
132
|
+
ENV.update(@environ)
|
133
|
+
ex = assert_raise(StandardError) do
|
134
|
+
cgi = CGI.new
|
135
|
+
end
|
136
|
+
assert_equal("too large post data.", ex.message)
|
137
|
+
end if CGI.const_defined?(:MAX_CONTENT_LENGTH)
|
138
|
+
|
139
|
+
|
140
|
+
def test_cgi_core_out
|
141
|
+
@environ = {
|
142
|
+
'REQUEST_METHOD' => 'GET',
|
143
|
+
'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
|
144
|
+
'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
|
145
|
+
'SERVER_SOFTWARE' => 'Apache 2.2.0',
|
146
|
+
'SERVER_PROTOCOL' => 'HTTP/1.1',
|
147
|
+
}
|
148
|
+
ENV.update(@environ)
|
149
|
+
cgi = CGI.new
|
150
|
+
## jis, euc, sjis string
|
151
|
+
jis_str = "\e$B8+$m!\"?M$,%4%_$N$h$&$@\e(B"
|
152
|
+
euc_str = "\270\253\244\355\241\242\277\315\244\254\245\264\245\337\244\316\244\350\244\246\244\300"
|
153
|
+
sjis_str = "\214\251\202\353\201A\220l\202\252\203S\203~\202\314\202\346\202\244\202\276"
|
154
|
+
## iso-2022-jp
|
155
|
+
options = { 'charset'=>'iso-2022-jp' }
|
156
|
+
$stdout = StringIO.new
|
157
|
+
cgi.out(options) { euc_str }
|
158
|
+
assert_equal('ja', options['language'])
|
159
|
+
actual = $stdout.string
|
160
|
+
expected = "Content-Type: text/html; charset=iso-2022-jp\r\n" +
|
161
|
+
"Content-Length: 28\r\n" +
|
162
|
+
"Content-Language: ja\r\n" +
|
163
|
+
"\r\n" +
|
164
|
+
jis_str
|
165
|
+
assert_equal(expected, actual)
|
166
|
+
## euc-jp
|
167
|
+
options = { 'charset'=>'EUC-JP' }
|
168
|
+
$stdout = StringIO.new
|
169
|
+
cgi.out(options) { euc_str }
|
170
|
+
assert_equal('ja', options['language'])
|
171
|
+
actual = $stdout.string
|
172
|
+
expected = "Content-Type: text/html; charset=EUC-JP\r\n" +
|
173
|
+
"Content-Length: 22\r\n" +
|
174
|
+
"Content-Language: ja\r\n" +
|
175
|
+
"\r\n" +
|
176
|
+
euc_str
|
177
|
+
assert_equal(expected, actual)
|
178
|
+
## shift_jis
|
179
|
+
options = { 'charset'=>'Shift_JIS' }
|
180
|
+
$stdout = StringIO.new
|
181
|
+
cgi.out(options) { euc_str }
|
182
|
+
assert_equal('ja', options['language'])
|
183
|
+
actual = $stdout.string
|
184
|
+
expected = "Content-Type: text/html; charset=Shift_JIS\r\n" +
|
185
|
+
"Content-Length: 22\r\n" +
|
186
|
+
"Content-Language: ja\r\n" +
|
187
|
+
"\r\n" +
|
188
|
+
sjis_str
|
189
|
+
assert_equal(expected, actual)
|
190
|
+
## utf8 (not converted)
|
191
|
+
options = { 'charset'=>'utf8' }
|
192
|
+
$stdout = StringIO.new
|
193
|
+
cgi.out(options) { euc_str }
|
194
|
+
assert_nil(options['language'])
|
195
|
+
actual = $stdout.string
|
196
|
+
expected = "Content-Type: text/html; charset=utf8\r\n" +
|
197
|
+
"Content-Length: 22\r\n" +
|
198
|
+
"\r\n" +
|
199
|
+
euc_str
|
200
|
+
assert_equal(expected, actual)
|
201
|
+
## language is keeped
|
202
|
+
options = { 'charset'=>'Shift_JIS', 'language'=>'en' }
|
203
|
+
$stdout = StringIO.new
|
204
|
+
cgi.out(options) { euc_str }
|
205
|
+
assert_equal('en', options['language'])
|
206
|
+
## HEAD method
|
207
|
+
ENV['REQUEST_METHOD'] = 'HEAD'
|
208
|
+
options = { 'charset'=>'utf8' }
|
209
|
+
$stdout = StringIO.new
|
210
|
+
cgi.out(options) { euc_str }
|
211
|
+
actual = $stdout.string
|
212
|
+
expected = "Content-Type: text/html; charset=utf8\r\n" +
|
213
|
+
"Content-Length: 22\r\n" +
|
214
|
+
"\r\n"
|
215
|
+
assert_equal(expected, actual)
|
216
|
+
end
|
217
|
+
|
218
|
+
|
219
|
+
def test_cgi_core_print
|
220
|
+
@environ = {
|
221
|
+
'REQUEST_METHOD' => 'GET',
|
222
|
+
}
|
223
|
+
ENV.update(@environ)
|
224
|
+
cgi = CGI.new
|
225
|
+
$stdout = StringIO.new
|
226
|
+
str = "foobar"
|
227
|
+
cgi.print(str)
|
228
|
+
expected = str
|
229
|
+
actual = $stdout.string
|
230
|
+
assert_equal(expected, actual)
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
def test_cgi_core_environs
|
235
|
+
@environ = {
|
236
|
+
'REQUEST_METHOD' => 'GET',
|
237
|
+
}
|
238
|
+
ENV.update(@environ)
|
239
|
+
cgi = CGI.new
|
240
|
+
##
|
241
|
+
list1 = %w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
|
242
|
+
PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST
|
243
|
+
REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME
|
244
|
+
SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE
|
245
|
+
HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
|
246
|
+
HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST
|
247
|
+
HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT
|
248
|
+
]
|
249
|
+
list2 = %w[ CONTENT_LENGTH SERVER_PORT ]
|
250
|
+
## string expected
|
251
|
+
list1.each do |name|
|
252
|
+
@environ[name] = "**#{name}**"
|
253
|
+
end
|
254
|
+
ENV.update(@environ)
|
255
|
+
list1.each do |name|
|
256
|
+
method = name.sub(/\AHTTP_/, '').downcase
|
257
|
+
actual = cgi.__send__ method
|
258
|
+
expected = "**#{name}**"
|
259
|
+
assert_equal(expected, actual)
|
260
|
+
end
|
261
|
+
## integer expected
|
262
|
+
ENV['CONTENT_LENGTH'] = '123'
|
263
|
+
ENV['SERVER_PORT'] = '8080'
|
264
|
+
assert_equal(123, cgi.content_length)
|
265
|
+
assert_equal(8080, cgi.server_port)
|
266
|
+
## raw cookie
|
267
|
+
ENV['HTTP_COOKIE'] = 'name1=val1'
|
268
|
+
ENV['HTTP_COOKIE2'] = 'name2=val2'
|
269
|
+
assert_equal('name1=val1', cgi.raw_cookie)
|
270
|
+
assert_equal('name2=val2', cgi.raw_cookie2)
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
def test_cgi_core_htmltype
|
275
|
+
@environ = {
|
276
|
+
'REQUEST_METHOD' => 'GET',
|
277
|
+
}
|
278
|
+
ENV.update(@environ)
|
279
|
+
## no htmltype
|
280
|
+
cgi = CGI.new
|
281
|
+
assert_raise(NoMethodError) do cgi.doctype end
|
282
|
+
## html3
|
283
|
+
cgi = CGI.new('html3')
|
284
|
+
expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
|
285
|
+
assert_equal(expected, cgi.doctype)
|
286
|
+
## html4
|
287
|
+
cgi = CGI.new('html4')
|
288
|
+
expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
|
289
|
+
assert_equal(expected, cgi.doctype)
|
290
|
+
## html4 transitional
|
291
|
+
cgi = CGI.new('html4Tr')
|
292
|
+
expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
|
293
|
+
assert_equal(expected, cgi.doctype)
|
294
|
+
## html4 frameset
|
295
|
+
cgi = CGI.new('html4Fr')
|
296
|
+
expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'
|
297
|
+
assert_equal(expected, cgi.doctype)
|
298
|
+
end
|
299
|
+
|
300
|
+
|
301
|
+
instance_methods.each do |method|
|
302
|
+
private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
|
303
|
+
end if ENV['TEST']
|
304
|
+
|
305
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
#require 'cgi'
|
3
|
+
require(ENV['CGI'] || 'cgi')
|
4
|
+
|
5
|
+
|
6
|
+
class CGIHeaderTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@environ = {
|
11
|
+
'SERVER_PROTOCOL' => 'HTTP/1.1',
|
12
|
+
'REQUEST_METHOD' => 'GET',
|
13
|
+
'SERVER_SOFTWARE' => 'Apache 2.2.0',
|
14
|
+
}
|
15
|
+
ENV.update(@environ)
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def teardown
|
20
|
+
@environ.each do |key, val| ENV.delete(key) end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def test_cgi_header_simple
|
25
|
+
cgi = CGI.new
|
26
|
+
## default content type
|
27
|
+
expected = "Content-Type: text/html\r\n\r\n"
|
28
|
+
actual = cgi.header
|
29
|
+
assert_equal(expected, actual)
|
30
|
+
## content type specified as string
|
31
|
+
expected = "Content-Type: text/xhtml; charset=utf8\r\n\r\n"
|
32
|
+
actual = cgi.header('text/xhtml; charset=utf8')
|
33
|
+
assert_equal(expected, actual)
|
34
|
+
## content type specified as hash
|
35
|
+
expected = "Content-Type: image/png\r\n\r\n"
|
36
|
+
actual = cgi.header('type'=>'image/png')
|
37
|
+
assert_equal(expected, actual)
|
38
|
+
## charset specified
|
39
|
+
expected = "Content-Type: text/html; charset=utf8\r\n\r\n"
|
40
|
+
actual = cgi.header('charset'=>'utf8')
|
41
|
+
assert_equal(expected, actual)
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
def test_cgi_header_complex
|
46
|
+
cgi = CGI.new
|
47
|
+
options = {
|
48
|
+
'type' => 'text/xhtml',
|
49
|
+
'charset' => 'utf8',
|
50
|
+
'status' => 'REDIRECT',
|
51
|
+
'server' => 'webrick',
|
52
|
+
'connection' => 'close',
|
53
|
+
'length' => 123,
|
54
|
+
'language' => 'ja',
|
55
|
+
'expires' => Time.gm(2000, 1, 23, 12, 34, 56),
|
56
|
+
'location' => 'http://www.ruby-lang.org/',
|
57
|
+
}
|
58
|
+
expected = "Status: 302 Found\r\n"
|
59
|
+
expected << "Server: webrick\r\n"
|
60
|
+
expected << "Connection: close\r\n"
|
61
|
+
expected << "Content-Type: text/xhtml; charset=utf8\r\n"
|
62
|
+
expected << "Content-Length: 123\r\n"
|
63
|
+
expected << "Content-Language: ja\r\n"
|
64
|
+
expected << "Expires: Sun, 23 Jan 2000 12:34:56 GMT\r\n"
|
65
|
+
expected << "location: http://www.ruby-lang.org/\r\n"
|
66
|
+
expected << "\r\n"
|
67
|
+
actual = cgi.header(options)
|
68
|
+
assert_equal(expected, actual)
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def test_cgi_header_argerr
|
73
|
+
cgi = CGI.new
|
74
|
+
#expected = NoMethodError # must be ArgumentError
|
75
|
+
if defined?(CGI::RELEASE)
|
76
|
+
expected = ArgumentError # for CGIAlt
|
77
|
+
else
|
78
|
+
expected = NoMethodError # for Ruby1.8
|
79
|
+
end
|
80
|
+
ex = assert_raise(expected) do
|
81
|
+
cgi.header(nil)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def test_cgi_header_cookie
|
87
|
+
cgi = CGI.new
|
88
|
+
cookie1 = CGI::Cookie.new('name1', 'abc', '123')
|
89
|
+
cookie2 = CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true)
|
90
|
+
ctype = "Content-Type: text/html\r\n"
|
91
|
+
sep = "\r\n"
|
92
|
+
c1 = "Set-Cookie: name1=abc&123; path=\r\n"
|
93
|
+
c2 = "Set-Cookie: name2=value2; path=; secure\r\n"
|
94
|
+
## CGI::Cookie object
|
95
|
+
actual = cgi.header('cookie'=>cookie1)
|
96
|
+
expected = ctype + c1 + sep
|
97
|
+
assert_equal(expected, actual)
|
98
|
+
## String
|
99
|
+
actual = cgi.header('cookie'=>cookie2.to_s)
|
100
|
+
expected = ctype + c2 + sep
|
101
|
+
assert_equal(expected, actual)
|
102
|
+
## Array
|
103
|
+
actual = cgi.header('cookie'=>[cookie1, cookie2])
|
104
|
+
expected = ctype + c1 + c2 + sep
|
105
|
+
assert_equal(expected, actual)
|
106
|
+
## Hash
|
107
|
+
actual = cgi.header('cookie'=>{'name1'=>cookie1, 'name2'=>cookie2})
|
108
|
+
expected = ctype + c1 + c2 + sep
|
109
|
+
assert_equal(expected, actual)
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
def test_cgi_header_output_cookies
|
114
|
+
cgi = CGI.new
|
115
|
+
## output cookies
|
116
|
+
cookies = [ CGI::Cookie.new('name1', 'abc', '123'),
|
117
|
+
CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true),
|
118
|
+
]
|
119
|
+
cgi.instance_variable_set('@output_cookies', cookies)
|
120
|
+
expected = "Content-Type: text/html; charset=utf8\r\n"
|
121
|
+
expected << "Set-Cookie: name1=abc&123; path=\r\n"
|
122
|
+
expected << "Set-Cookie: name2=value2; path=; secure\r\n"
|
123
|
+
expected << "\r\n"
|
124
|
+
## header when string
|
125
|
+
actual = cgi.header('text/html; charset=utf8')
|
126
|
+
assert_equal(expected, actual)
|
127
|
+
## _header_for_string
|
128
|
+
actual = cgi.header('type'=>'text/html', 'charset'=>'utf8')
|
129
|
+
assert_equal(expected, actual)
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
def test_cgi_header_nph
|
134
|
+
cgi = CGI.new
|
135
|
+
## 'nph' is true
|
136
|
+
ENV['SERVER_SOFTWARE'] = 'Apache 2.2.0'
|
137
|
+
actual1 = cgi.header('nph'=>true)
|
138
|
+
## when old IIS, NPH-mode is forced
|
139
|
+
ENV['SERVER_SOFTWARE'] = 'IIS/4.0'
|
140
|
+
actual2 = cgi.header
|
141
|
+
actual3 = cgi.header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
|
142
|
+
## newer IIS doesn't require NPH-mode ## [ruby-dev:30537]
|
143
|
+
ENV['SERVER_SOFTWARE'] = 'IIS/5.0'
|
144
|
+
actual4 = cgi.header
|
145
|
+
actual5 = cgi.header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
|
146
|
+
## assertion
|
147
|
+
now = Time.now
|
148
|
+
expected = "HTTP/1.1 200 OK\r\n"
|
149
|
+
expected << "Date: #{CGI.rfc1123_date(now)}\r\n"
|
150
|
+
expected << "Server: Apache 2.2.0\r\n"
|
151
|
+
expected << "Connection: close\r\n"
|
152
|
+
expected << "Content-Type: text/html\r\n"
|
153
|
+
expected << "\r\n"
|
154
|
+
assert_equal(expected, actual1)
|
155
|
+
expected.sub!(/^Server: .*?\r\n/, "Server: IIS/4.0\r\n")
|
156
|
+
assert_equal(expected, actual2)
|
157
|
+
expected.sub!(/^HTTP\/1.1 200 OK\r\n/, "HTTP/1.1 302 Found\r\n")
|
158
|
+
expected.sub!(/\r\n\r\n/, "\r\nlocation: http://www.example.com/\r\n\r\n")
|
159
|
+
assert_equal(expected, actual3)
|
160
|
+
expected = "Content-Type: text/html\r\n"
|
161
|
+
expected << "\r\n"
|
162
|
+
assert_equal(expected, actual4)
|
163
|
+
expected = "Status: 302 Found\r\n"
|
164
|
+
expected << "Content-Type: text/html\r\n"
|
165
|
+
expected << "location: http://www.example.com/\r\n"
|
166
|
+
expected << "\r\n"
|
167
|
+
assert_equal(expected, actual5)
|
168
|
+
ensure
|
169
|
+
ENV.delete('SERVER_SOFTWARE')
|
170
|
+
end
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
instance_methods.each do |method|
|
175
|
+
private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
|
176
|
+
end if ENV['TEST']
|
177
|
+
|
178
|
+
end
|