summaryrefslogtreecommitdiff
path: root/jni/ruby/test/cgi
diff options
context:
space:
mode:
authorJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-16 18:49:26 +0900
committerJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-30 00:39:06 +0900
commitfcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch)
tree64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/test/cgi
Fresh start
Diffstat (limited to 'jni/ruby/test/cgi')
-rw-r--r--jni/ruby/test/cgi/test_cgi_cookie.rb110
-rw-r--r--jni/ruby/test/cgi/test_cgi_core.rb312
-rw-r--r--jni/ruby/test/cgi/test_cgi_header.rb181
-rw-r--r--jni/ruby/test/cgi/test_cgi_modruby.rb146
-rw-r--r--jni/ruby/test/cgi/test_cgi_multipart.rb380
-rw-r--r--jni/ruby/test/cgi/test_cgi_session.rb172
-rw-r--r--jni/ruby/test/cgi/test_cgi_tag_helper.rb353
-rw-r--r--jni/ruby/test/cgi/test_cgi_util.rb104
-rw-r--r--jni/ruby/test/cgi/testdata/file1.html10
-rw-r--r--jni/ruby/test/cgi/testdata/large.pngbin0 -> 156414 bytes
-rw-r--r--jni/ruby/test/cgi/testdata/small.pngbin0 -> 82 bytes
11 files changed, 1768 insertions, 0 deletions
diff --git a/jni/ruby/test/cgi/test_cgi_cookie.rb b/jni/ruby/test/cgi/test_cgi_cookie.rb
new file mode 100644
index 0000000..c1c6a30
--- /dev/null
+++ b/jni/ruby/test/cgi/test_cgi_cookie.rb
@@ -0,0 +1,110 @@
+require 'test/unit'
+require 'cgi'
+require 'stringio'
+
+
+class CGICookieTest < Test::Unit::TestCase
+
+
+ def setup
+ ENV['REQUEST_METHOD'] = 'GET'
+ @str1="\xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93"
+ @str1.force_encoding("UTF-8") if defined?(::Encoding)
+ end
+
+ def teardown
+ %W[REQUEST_METHOD SCRIPT_NAME].each do |name|
+ ENV.delete(name)
+ end
+ end
+
+
+ def test_cgi_cookie_new_simple
+ cookie = CGI::Cookie.new('name1', 'val1', '&<>"', @str1)
+ assert_equal('name1', cookie.name)
+ assert_equal(['val1', '&<>"', @str1], cookie.value)
+ assert_nil(cookie.domain)
+ assert_nil(cookie.expires)
+ assert_equal('', cookie.path)
+ assert_equal(false, cookie.secure)
+ assert_equal("name1=val1&%26%3C%3E%22&%E3%82%86%E3%82%93%E3%82%86%E3%82%93; path=", cookie.to_s)
+ end
+
+
+ def test_cgi_cookie_new_complex
+ t = Time.gm(2030, 12, 31, 23, 59, 59)
+ value = ['val1', '&<>"', "\xA5\xE0\xA5\xB9\xA5\xAB"]
+ value[2].force_encoding("EUC-JP") if defined?(::Encoding)
+ cookie = CGI::Cookie.new('name'=>'name1',
+ 'value'=>value,
+ 'path'=>'/cgi-bin/myapp/',
+ 'domain'=>'www.example.com',
+ 'expires'=>t,
+ 'secure'=>true
+ )
+ assert_equal('name1', cookie.name)
+ assert_equal(value, cookie.value)
+ assert_equal('www.example.com', cookie.domain)
+ assert_equal(t, cookie.expires)
+ assert_equal('/cgi-bin/myapp/', cookie.path)
+ assert_equal(true, cookie.secure)
+ 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)
+ end
+
+
+ def test_cgi_cookie_scriptname
+ cookie = CGI::Cookie.new('name1', 'value1')
+ assert_equal('', cookie.path)
+ cookie = CGI::Cookie.new('name'=>'name1', 'value'=>'value1')
+ assert_equal('', cookie.path)
+ ## when ENV['SCRIPT_NAME'] is set, cookie.path is set automatically
+ ENV['SCRIPT_NAME'] = '/cgi-bin/app/example.cgi'
+ cookie = CGI::Cookie.new('name1', 'value1')
+ assert_equal('/cgi-bin/app/', cookie.path)
+ cookie = CGI::Cookie.new('name'=>'name1', 'value'=>'value1')
+ assert_equal('/cgi-bin/app/', cookie.path)
+ end
+
+
+ def test_cgi_cookie_parse
+ ## ';' separator
+ cookie_str = 'name1=val1&val2; name2=val2&%26%3C%3E%22&%E3%82%86%E3%82%93%E3%82%86%E3%82%93;_session_id=12345'
+ cookies = CGI::Cookie.parse(cookie_str)
+ list = [
+ ['name1', ['val1', 'val2']],
+ ['name2', ['val2', '&<>"',@str1]],
+ ['_session_id', ['12345']],
+ ]
+ list.each do |name, value|
+ cookie = cookies[name]
+ assert_equal(name, cookie.name)
+ assert_equal(value, cookie.value)
+ end
+ ## ',' separator
+ cookie_str = 'name1=val1&val2, name2=val2&%26%3C%3E%22&%E3%82%86%E3%82%93%E3%82%86%E3%82%93,_session_id=12345'
+ cookies = CGI::Cookie.parse(cookie_str)
+ list.each do |name, value|
+ cookie = cookies[name]
+ assert_equal(name, cookie.name)
+ assert_equal(value, cookie.value)
+ end
+ end
+
+
+ def test_cgi_cookie_arrayinterface
+ cookie = CGI::Cookie.new('name1', 'a', 'b', 'c')
+ assert_equal('a', cookie[0])
+ assert_equal('c', cookie[2])
+ assert_nil(cookie[3])
+ assert_equal('a', cookie.first)
+ assert_equal('c', cookie.last)
+ assert_equal(['A', 'B', 'C'], cookie.collect{|e| e.upcase})
+ end
+
+
+
+ instance_methods.each do |method|
+ private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
+ end if ENV['TEST']
+
+end
diff --git a/jni/ruby/test/cgi/test_cgi_core.rb b/jni/ruby/test/cgi/test_cgi_core.rb
new file mode 100644
index 0000000..274d088
--- /dev/null
+++ b/jni/ruby/test/cgi/test_cgi_core.rb
@@ -0,0 +1,312 @@
+require 'test/unit'
+require 'cgi'
+require 'stringio'
+
+
+class CGICoreTest < Test::Unit::TestCase
+
+
+ def setup
+ #@environ = {
+ # 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ # 'REQUEST_METHOD' => 'GET',
+ # 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ #}
+ #ENV.update(@environ)
+ end
+
+
+ def teardown
+ @environ.each do |key, val| ENV.delete(key) end
+ $stdout = STDOUT
+ end
+
+ def test_cgi_parse_illegal_query
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => 'a=111&&b=222&c&d=',
+ 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ assert_equal(["a","b","c","d"],cgi.keys.sort)
+ assert_equal("",cgi["d"])
+ end
+
+ def test_cgi_core_params_GET
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => 'id=123&id=456&id=&id&str=%40h+%3D%7E+%2F%5E%24%2F',
+ 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ ## cgi[]
+ assert_equal('123', cgi['id'])
+ assert_equal('@h =~ /^$/', cgi['str'])
+ ## cgi.params
+ assert_equal(['123', '456', ''], cgi.params['id'])
+ assert_equal(['@h =~ /^$/'], cgi.params['str'])
+ ## cgi.keys
+ assert_equal(['id', 'str'], cgi.keys.sort)
+ ## cgi.key?, cgi.has_key?, cgi.include?
+ assert_equal(true, cgi.key?('id'))
+ assert_equal(true, cgi.has_key?('id'))
+ assert_equal(true, cgi.include?('id'))
+ assert_equal(false, cgi.key?('foo'))
+ assert_equal(false, cgi.has_key?('foo'))
+ assert_equal(false, cgi.include?('foo'))
+ ## invalid parameter name
+ assert_equal('', cgi['*notfound*']) # [ruby-dev:30740]
+ assert_equal([], cgi.params['*notfound*'])
+ end
+
+
+ def test_cgi_core_params_POST
+ query_str = 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F'
+ @environ = {
+ 'REQUEST_METHOD' => 'POST',
+ 'CONTENT_LENGTH' => query_str.length.to_s,
+ 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ $stdin = StringIO.new
+ $stdin << query_str
+ $stdin.rewind
+ cgi = CGI.new
+ ## cgi[]
+ assert_equal('123', cgi['id'])
+ assert_equal('@h =~ /^$/', cgi['str'])
+ ## cgi.params
+ assert_equal(['123', '456', ''], cgi.params['id'])
+ assert_equal(['@h =~ /^$/'], cgi.params['str'])
+ ## invalid parameter name
+ assert_equal('', cgi['*notfound*'])
+ assert_equal([], cgi.params['*notfound*'])
+ end
+
+ def test_cgi_core_params_encoding_check
+ query_str = 'str=%BE%BE%B9%BE'
+ @environ = {
+ 'REQUEST_METHOD' => 'POST',
+ 'CONTENT_LENGTH' => query_str.length.to_s,
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ $stdin = StringIO.new
+ $stdin << query_str
+ $stdin.rewind
+ if defined?(::Encoding)
+ hash={}
+ cgi = CGI.new(:accept_charset=>"UTF-8"){|key,val|hash[key]=val}
+ ## cgi[]
+ assert_equal("\xBE\xBE\xB9\xBE".force_encoding("UTF-8"), cgi['str'])
+ ## cgi.params
+ assert_equal(["\xBE\xBE\xB9\xBE".force_encoding("UTF-8")], cgi.params['str'])
+ ## accept-charset error
+ assert_equal({"str"=>"\xBE\xBE\xB9\xBE".force_encoding("UTF-8")},hash)
+
+ $stdin.rewind
+ assert_raise(CGI::InvalidEncoding) do
+ cgi = CGI.new(:accept_charset=>"UTF-8")
+ end
+
+ $stdin.rewind
+ cgi = CGI.new(:accept_charset=>"EUC-JP")
+ ## cgi[]
+ assert_equal("\xBE\xBE\xB9\xBE".force_encoding("EUC-JP"), cgi['str'])
+ ## cgi.params
+ assert_equal(["\xBE\xBE\xB9\xBE".force_encoding("EUC-JP")], cgi.params['str'])
+ else
+ assert(true)
+ end
+ end
+
+
+ def test_cgi_core_cookie
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
+ 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ assert_not_equal(nil,cgi.cookies)
+ [ ['_session_id', ['12345'], ],
+ ['name1', ['val1', 'val2'], ],
+ ].each do |key, expected|
+ cookie = cgi.cookies[key]
+ assert_kind_of(CGI::Cookie, cookie)
+ assert_equal(expected, cookie.value)
+ assert_equal(false, cookie.secure)
+ assert_nil(cookie.expires)
+ assert_nil(cookie.domain)
+ assert_equal('', cookie.path)
+ end
+ end
+
+
+ def test_cgi_core_maxcontentlength
+ @environ = {
+ 'REQUEST_METHOD' => 'POST',
+ 'CONTENT_LENGTH' => (64 * 1024 * 1024).to_s
+ }
+ ENV.update(@environ)
+ ex = assert_raise(StandardError) do
+ CGI.new
+ end
+ assert_equal("too large post data.", ex.message)
+ end if CGI.const_defined?(:MAX_CONTENT_LENGTH)
+
+
+ def test_cgi_core_out
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
+ 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ ## euc string
+ euc_str = "\270\253\244\355\241\242\277\315\244\254\245\264\245\337\244\316\244\350\244\246\244\300"
+ ## utf8 (not converted)
+ options = { 'charset'=>'utf8' }
+ $stdout = StringIO.new
+ cgi.out(options) { euc_str }
+ assert_nil(options['language'])
+ actual = $stdout.string
+ expected = "Content-Type: text/html; charset=utf8\r\n" +
+ "Content-Length: 22\r\n" +
+ "\r\n" +
+ euc_str
+ if defined?(::Encoding)
+ actual.force_encoding("ASCII-8BIT")
+ expected.force_encoding("ASCII-8BIT")
+ end
+ assert_equal(expected, actual)
+ ## language is keeped
+ options = { 'charset'=>'Shift_JIS', 'language'=>'en' }
+ $stdout = StringIO.new
+ cgi.out(options) { euc_str }
+ assert_equal('en', options['language'])
+ ## HEAD method
+ ENV['REQUEST_METHOD'] = 'HEAD'
+ options = { 'charset'=>'utf8' }
+ $stdout = StringIO.new
+ cgi.out(options) { euc_str }
+ actual = $stdout.string
+ expected = "Content-Type: text/html; charset=utf8\r\n" +
+ "Content-Length: 22\r\n" +
+ "\r\n"
+ assert_equal(expected, actual)
+ end
+
+
+ def test_cgi_core_print
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ $stdout = StringIO.new
+ str = "foobar"
+ cgi.print(str)
+ expected = str
+ actual = $stdout.string
+ assert_equal(expected, actual)
+ end
+
+
+ def test_cgi_core_environs
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ ##
+ list1 = %w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
+ PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST
+ REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME
+ SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE
+ HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
+ HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST
+ HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT
+ ]
+ # list2 = %w[ CONTENT_LENGTH SERVER_PORT ]
+ ## string expected
+ list1.each do |name|
+ @environ[name] = "**#{name}**"
+ end
+ ENV.update(@environ)
+ list1.each do |name|
+ method = name.sub(/\AHTTP_/, '').downcase
+ actual = cgi.__send__ method
+ expected = "**#{name}**"
+ assert_equal(expected, actual)
+ end
+ ## integer expected
+ ENV['CONTENT_LENGTH'] = '123'
+ ENV['SERVER_PORT'] = '8080'
+ assert_equal(123, cgi.content_length)
+ assert_equal(8080, cgi.server_port)
+ ## raw cookie
+ ENV['HTTP_COOKIE'] = 'name1=val1'
+ ENV['HTTP_COOKIE2'] = 'name2=val2'
+ assert_equal('name1=val1', cgi.raw_cookie)
+ assert_equal('name2=val2', cgi.raw_cookie2)
+ end
+
+
+ def test_cgi_core_htmltype_header
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ }
+ ENV.update(@environ)
+ ## no htmltype
+ cgi = CGI.new
+ assert_raise(NoMethodError) do cgi.doctype end
+ assert_equal("Content-Type: text/html\r\n\r\n",cgi.header)
+ ## html3
+ cgi = CGI.new('html3')
+ expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'
+ assert_equal(expected, cgi.doctype)
+ assert_equal("Content-Type: text/html\r\n\r\n",cgi.header)
+ ## html4
+ cgi = CGI.new('html4')
+ expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
+ assert_equal(expected, cgi.doctype)
+ assert_equal("Content-Type: text/html\r\n\r\n",cgi.header)
+ ## html4 transitional
+ cgi = CGI.new('html4Tr')
+ expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
+ assert_equal(expected, cgi.doctype)
+ assert_equal("Content-Type: text/html\r\n\r\n",cgi.header)
+ ## html4 frameset
+ cgi = CGI.new('html4Fr')
+ expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">'
+ assert_equal(expected, cgi.doctype)
+ assert_equal("Content-Type: text/html\r\n\r\n",cgi.header)
+ ## html5
+ cgi = CGI.new('html5')
+ expected = '<!DOCTYPE HTML>'
+ assert_equal(expected, cgi.doctype)
+ assert_match(/^<HEADER><\/HEADER>$/i,cgi.header)
+ end
+
+
+ instance_methods.each do |method|
+ private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
+ end if ENV['TEST']
+
+end
diff --git a/jni/ruby/test/cgi/test_cgi_header.rb b/jni/ruby/test/cgi/test_cgi_header.rb
new file mode 100644
index 0000000..9022301
--- /dev/null
+++ b/jni/ruby/test/cgi/test_cgi_header.rb
@@ -0,0 +1,181 @@
+require 'test/unit'
+require 'cgi'
+require 'time'
+
+
+class CGIHeaderTest < Test::Unit::TestCase
+
+
+ def setup
+ @environ = {
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'REQUEST_METHOD' => 'GET',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ }
+ ENV.update(@environ)
+ end
+
+
+ def teardown
+ @environ.each do |key, val| ENV.delete(key) end
+ end
+
+
+ def test_cgi_http_header_simple
+ cgi = CGI.new
+ ## default content type
+ expected = "Content-Type: text/html\r\n\r\n"
+ actual = cgi.http_header
+ assert_equal(expected, actual)
+ ## content type specified as string
+ expected = "Content-Type: text/xhtml; charset=utf8\r\n\r\n"
+ actual = cgi.http_header('text/xhtml; charset=utf8')
+ assert_equal(expected, actual)
+ ## content type specified as hash
+ expected = "Content-Type: image/png\r\n\r\n"
+ actual = cgi.http_header('type'=>'image/png')
+ assert_equal(expected, actual)
+ ## charset specified
+ expected = "Content-Type: text/html; charset=utf8\r\n\r\n"
+ actual = cgi.http_header('charset'=>'utf8')
+ assert_equal(expected, actual)
+ end
+
+
+ def test_cgi_http_header_complex
+ cgi = CGI.new
+ options = {
+ 'type' => 'text/xhtml',
+ 'charset' => 'utf8',
+ 'status' => 'REDIRECT',
+ 'server' => 'webrick',
+ 'connection' => 'close',
+ 'length' => 123,
+ 'language' => 'ja',
+ 'expires' => Time.gm(2000, 1, 23, 12, 34, 56),
+ 'location' => 'http://www.ruby-lang.org/',
+ }
+ expected = "Status: 302 Found\r\n"
+ expected << "Server: webrick\r\n"
+ expected << "Connection: close\r\n"
+ expected << "Content-Type: text/xhtml; charset=utf8\r\n"
+ expected << "Content-Length: 123\r\n"
+ expected << "Content-Language: ja\r\n"
+ expected << "Expires: Sun, 23 Jan 2000 12:34:56 GMT\r\n"
+ expected << "location: http://www.ruby-lang.org/\r\n"
+ expected << "\r\n"
+ actual = cgi.http_header(options)
+ assert_equal(expected, actual)
+ end
+
+
+ def test_cgi_http_header_argerr
+ cgi = CGI.new
+ expected = ArgumentError
+
+ assert_raise(expected) do
+ cgi.http_header(nil)
+ end
+ end
+
+
+ def test_cgi_http_header_cookie
+ cgi = CGI.new
+ cookie1 = CGI::Cookie.new('name1', 'abc', '123')
+ cookie2 = CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true)
+ ctype = "Content-Type: text/html\r\n"
+ sep = "\r\n"
+ c1 = "Set-Cookie: name1=abc&123; path=\r\n"
+ c2 = "Set-Cookie: name2=value2; path=; secure\r\n"
+ ## CGI::Cookie object
+ actual = cgi.http_header('cookie'=>cookie1)
+ expected = ctype + c1 + sep
+ assert_equal(expected, actual)
+ ## String
+ actual = cgi.http_header('cookie'=>cookie2.to_s)
+ expected = ctype + c2 + sep
+ assert_equal(expected, actual)
+ ## Array
+ actual = cgi.http_header('cookie'=>[cookie1, cookie2])
+ expected = ctype + c1 + c2 + sep
+ assert_equal(expected, actual)
+ ## Hash
+ actual = cgi.http_header('cookie'=>{'name1'=>cookie1, 'name2'=>cookie2})
+ expected = ctype + c1 + c2 + sep
+ assert_equal(expected, actual)
+ end
+
+
+ def test_cgi_http_header_output_cookies
+ cgi = CGI.new
+ ## output cookies
+ cookies = [ CGI::Cookie.new('name1', 'abc', '123'),
+ CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true),
+ ]
+ cgi.instance_variable_set('@output_cookies', cookies)
+ expected = "Content-Type: text/html; charset=utf8\r\n"
+ expected << "Set-Cookie: name1=abc&123; path=\r\n"
+ expected << "Set-Cookie: name2=value2; path=; secure\r\n"
+ expected << "\r\n"
+ ## header when string
+ actual = cgi.http_header('text/html; charset=utf8')
+ assert_equal(expected, actual)
+ ## _header_for_string
+ actual = cgi.http_header('type'=>'text/html', 'charset'=>'utf8')
+ assert_equal(expected, actual)
+ end
+
+
+ def test_cgi_http_header_nph
+ time_start = Time.now.to_i
+ cgi = CGI.new
+ ## 'nph' is true
+ ENV['SERVER_SOFTWARE'] = 'Apache 2.2.0'
+ actual1 = cgi.http_header('nph'=>true)
+ ## when old IIS, NPH-mode is forced
+ ENV['SERVER_SOFTWARE'] = 'IIS/4.0'
+ actual2 = cgi.http_header
+ actual3 = cgi.http_header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
+ ## newer IIS doesn't require NPH-mode ## [ruby-dev:30537]
+ ENV['SERVER_SOFTWARE'] = 'IIS/5.0'
+ actual4 = cgi.http_header
+ actual5 = cgi.http_header('status'=>'REDIRECT', 'location'=>'http://www.example.com/')
+ time_end = Time.now.to_i
+ date = /^Date: ([A-Z][a-z]{2}, \d{2} [A-Z][a-z]{2} \d{4} \d\d:\d\d:\d\d GMT)\r\n/
+ [actual1, actual2, actual3].each do |actual|
+ assert_match(date, actual)
+ assert_includes(time_start..time_end, date =~ actual && Time.parse($1).to_i)
+ actual.sub!(date, "Date: DATE_IS_REMOVED\r\n")
+ end
+ ## assertion
+ expected = "HTTP/1.1 200 OK\r\n"
+ expected << "Date: DATE_IS_REMOVED\r\n"
+ expected << "Server: Apache 2.2.0\r\n"
+ expected << "Connection: close\r\n"
+ expected << "Content-Type: text/html\r\n"
+ expected << "\r\n"
+ assert_equal(expected, actual1)
+ expected.sub!(/^Server: .*?\r\n/, "Server: IIS/4.0\r\n")
+ assert_equal(expected, actual2)
+ expected.sub!(/^HTTP\/1.1 200 OK\r\n/, "HTTP/1.1 302 Found\r\n")
+ expected.sub!(/\r\n\r\n/, "\r\nlocation: http://www.example.com/\r\n\r\n")
+ assert_equal(expected, actual3)
+ expected = "Content-Type: text/html\r\n"
+ expected << "\r\n"
+ assert_equal(expected, actual4)
+ expected = "Status: 302 Found\r\n"
+ expected << "Content-Type: text/html\r\n"
+ expected << "location: http://www.example.com/\r\n"
+ expected << "\r\n"
+ assert_equal(expected, actual5)
+ ensure
+ ENV.delete('SERVER_SOFTWARE')
+ end
+
+
+
+ instance_methods.each do |method|
+ private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
+ end if ENV['TEST']
+
+end
diff --git a/jni/ruby/test/cgi/test_cgi_modruby.rb b/jni/ruby/test/cgi/test_cgi_modruby.rb
new file mode 100644
index 0000000..b0fc442
--- /dev/null
+++ b/jni/ruby/test/cgi/test_cgi_modruby.rb
@@ -0,0 +1,146 @@
+require 'test/unit'
+require 'cgi'
+
+
+class CGIModrubyTest < Test::Unit::TestCase
+
+
+ def setup
+ @environ = {
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'REQUEST_METHOD' => 'GET',
+ #'QUERY_STRING' => 'a=foo&b=bar',
+ }
+ ENV.update(@environ)
+ CGI.class_eval { const_set(:MOD_RUBY, true) }
+ Apache._reset()
+ #@cgi = CGI.new
+ #@req = Apache.request
+ end
+
+
+ def teardown
+ @environ.each do |key, val| ENV.delete(key) end
+ CGI.class_eval { remove_const(:MOD_RUBY) }
+ end
+
+
+ def test_cgi_modruby_simple
+ req = Apache.request
+ cgi = CGI.new
+ assert(req._setup_cgi_env_invoked?)
+ assert(! req._send_http_header_invoked?)
+ actual = cgi.http_header
+ assert_equal('', actual)
+ assert_equal('text/html', req.content_type)
+ assert(req._send_http_header_invoked?)
+ end
+
+
+ def test_cgi_modruby_complex
+ req = Apache.request
+ cgi = CGI.new
+ options = {
+ 'status' => 'FORBIDDEN',
+ 'location' => 'http://www.example.com/',
+ 'type' => 'image/gif',
+ 'content-encoding' => 'deflate',
+ 'cookie' => [ CGI::Cookie.new('name1', 'abc', '123'),
+ CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true),
+ ],
+ }
+ assert(req._setup_cgi_env_invoked?)
+ assert(! req._send_http_header_invoked?)
+ actual = cgi.http_header(options)
+ assert_equal('', actual)
+ assert_equal('image/gif', req.content_type)
+ assert_equal('403 Forbidden', req.status_line)
+ assert_equal(403, req.status)
+ assert_equal('deflate', req.content_encoding)
+ assert_equal('http://www.example.com/', req.headers_out['location'])
+ assert_equal(["name1=abc&123; path=", "name2=value2; path=; secure"],
+ req.headers_out['Set-Cookie'])
+ assert(req._send_http_header_invoked?)
+ end
+
+
+ def test_cgi_modruby_location
+ req = Apache.request
+ cgi = CGI.new
+ options = {
+ 'status' => '200 OK',
+ 'location' => 'http://www.example.com/',
+ }
+ cgi.http_header(options)
+ assert_equal('200 OK', req.status_line) # should be '302 Found' ?
+ assert_equal(302, req.status)
+ assert_equal('http://www.example.com/', req.headers_out['location'])
+ end
+
+
+ def test_cgi_modruby_requestparams
+ req = Apache.request
+ req.args = 'a=foo&b=bar'
+ cgi = CGI.new
+ assert_equal('foo', cgi['a'])
+ assert_equal('bar', cgi['b'])
+ end
+
+
+ instance_methods.each do |method|
+ private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
+ end if ENV['TEST']
+
+end
+
+
+
+## dummy class for mod_ruby
+class Apache #:nodoc:
+
+ def self._reset
+ @request = Request.new
+ end
+
+ def self.request
+ return @request
+ end
+
+ class Request
+
+ def initialize
+ hash = {}
+ def hash.add(name, value)
+ (self[name] ||= []) << value
+ end
+ @http_header = nil
+ @headers_out = hash
+ @status_line = nil
+ @status = nil
+ @content_type = nil
+ @content_encoding = nil
+ end
+ attr_accessor :headers_out, :status_line, :status, :content_type, :content_encoding
+
+ attr_accessor :args
+ #def args
+ # return ENV['QUERY_STRING']
+ #end
+
+ def send_http_header
+ @http_header = '*invoked*'
+ end
+ def _send_http_header_invoked?
+ @http_header ? true : false
+ end
+
+ def setup_cgi_env
+ @cgi_env = '*invoked*'
+ end
+ def _setup_cgi_env_invoked?
+ @cgi_env ? true : false
+ end
+
+ end
+
+end
diff --git a/jni/ruby/test/cgi/test_cgi_multipart.rb b/jni/ruby/test/cgi/test_cgi_multipart.rb
new file mode 100644
index 0000000..1325798
--- /dev/null
+++ b/jni/ruby/test/cgi/test_cgi_multipart.rb
@@ -0,0 +1,380 @@
+require 'test/unit'
+require 'cgi'
+require 'tempfile'
+require 'stringio'
+
+
+##
+## usage:
+## boundary = 'foobar1234' # or nil
+## multipart = MultiPart.new(boundary)
+## multipart.append('name1', 'value1')
+## multipart.append('file1', File.read('file1.html'), 'file1.html')
+## str = multipart.close()
+## str.each_line {|line| p line }
+## ## output:
+## # "--foobar1234\r\n"
+## # "Content-Disposition: form-data: name=\"name1\"\r\n"
+## # "\r\n"
+## # "value1\r\n"
+## # "--foobar1234\r\n"
+## # "Content-Disposition: form-data: name=\"file1\"; filename=\"file1.html\"\r\n"
+## # "Content-Type: text/html\r\n"
+## # "\r\n"
+## # "<html>\n"
+## # "<body><p>Hello</p></body>\n"
+## # "</html>\n"
+## # "\r\n"
+## # "--foobar1234--\r\n"
+##
+class MultiPart
+
+ def initialize(boundary=nil)
+ @boundary = boundary || create_boundary()
+ @buf = ''
+ @buf.force_encoding(::Encoding::ASCII_8BIT) if defined?(::Encoding)
+ end
+ attr_reader :boundary
+
+ def append(name, value, filename=nil, content_type=nil)
+ content_type = detect_content_type(filename) if filename && content_type.nil?
+ s = filename ? "; filename=\"#{filename}\"" : ''
+ buf = @buf
+ buf << "--#{boundary}\r\n"
+ buf << "Content-Disposition: form-data: name=\"#{name}\"#{s}\r\n"
+ buf << "Content-Type: #{content_type}\r\n" if content_type
+ buf << "\r\n"
+ value = value.dup.force_encoding(::Encoding::ASCII_8BIT) if defined?(::Encoding)
+ buf << value
+ buf << "\r\n"
+ return self
+ end
+
+ def close
+ buf = @buf
+ @buf = ''
+ return buf << "--#{boundary}--\r\n"
+ end
+
+ def create_boundary() #:nodoc:
+ return "--boundary#{rand().to_s[2..-1]}"
+ end
+
+ def detect_content_type(filename) #:nodoc:
+ filename =~ /\.(\w+)\z/
+ return MIME_TYPES[$1] || 'application/octet-stream'
+ end
+
+ MIME_TYPES = {
+ 'gif' => 'image/gif',
+ 'jpg' => 'image/jpeg',
+ 'jpeg' => 'image/jpeg',
+ 'png' => 'image/png',
+ 'bmp' => 'image/bmp',
+ 'tif' => 'image/tiff',
+ 'tiff' => 'image/tiff',
+ 'htm' => 'text/html',
+ 'html' => 'text/html',
+ 'xml' => 'text/xml',
+ 'txt' => 'text/plain',
+ 'text' => 'text/plain',
+ 'css' => 'text/css',
+ 'mpg' => 'video/mpeg',
+ 'mpeg' => 'video/mpeg',
+ 'mov' => 'video/quicktime',
+ 'avi' => 'video/x-msvideo',
+ 'mp3' => 'audio/mpeg',
+ 'mid' => 'audio/midi',
+ 'wav' => 'audio/x-wav',
+ 'zip' => 'application/zip',
+ #'tar.gz' => 'application/gtar',
+ 'gz' => 'application/gzip',
+ 'bz2' => 'application/bzip2',
+ 'rtf' => 'application/rtf',
+ 'pdf' => 'application/pdf',
+ 'ps' => 'application/postscript',
+ 'js' => 'application/x-javascript',
+ 'xls' => 'application/vnd.ms-excel',
+ 'doc' => 'application/msword',
+ 'ppt' => 'application/vnd.ms-powerpoint',
+ }
+
+end
+
+
+
+class CGIMultipartTest < Test::Unit::TestCase
+
+ def setup
+ ENV['REQUEST_METHOD'] = 'POST'
+ @tempfiles = []
+ end
+
+ def teardown
+ %w[ REQUEST_METHOD CONTENT_TYPE CONTENT_LENGTH REQUEST_METHOD ].each do |name|
+ ENV.delete(name)
+ end
+ $stdin.close() if $stdin.is_a?(Tempfile)
+ $stdin = STDIN
+ @tempfiles.each {|t|
+ t.close!
+ }
+ end
+
+ def _prepare(data)
+ ## create multipart input
+ multipart = MultiPart.new(defined?(@boundary) ? @boundary : nil)
+ data.each do |hash|
+ multipart.append(hash[:name], hash[:value], hash[:filename])
+ end
+ input = multipart.close()
+ input = yield(input) if block_given?
+ #$stderr.puts "*** debug: input=\n#{input.collect{|line| line.inspect}.join("\n")}"
+ @boundary ||= multipart.boundary
+ ## set environment
+ ENV['CONTENT_TYPE'] = "multipart/form-data; boundary=#{@boundary}"
+ ENV['CONTENT_LENGTH'] = input.length.to_s
+ ENV['REQUEST_METHOD'] = 'POST'
+ ## set $stdin
+ tmpfile = Tempfile.new('test_cgi_multipart')
+ @tempfiles << tmpfile
+ tmpfile.binmode
+ tmpfile << input
+ tmpfile.rewind()
+ $stdin = tmpfile
+ end
+
+ def _test_multipart(cgi_options={})
+ caller(0).find {|s| s =~ /in `test_(.*?)'/ }
+ #testname = $1
+ #$stderr.puts "*** debug: testname=#{testname.inspect}"
+ _prepare(@data)
+ options = {:accept_charset=>"UTF-8"}
+ options.merge! cgi_options
+ cgi = CGI.new(options)
+ expected_names = @data.collect{|hash| hash[:name] }.sort
+ assert_equal(expected_names, cgi.params.keys.sort)
+ threshold = 1024*10
+ @data.each do |hash|
+ name = hash[:name]
+ expected = hash[:value]
+ if hash[:filename] #if file
+ expected_class = @expected_class || (hash[:value].length < threshold ? StringIO : Tempfile)
+ assert(cgi.files.keys.member?(hash[:name]))
+ else
+ expected_class = String
+ assert_equal(expected, cgi[name])
+ assert_equal(false,cgi.files.keys.member?(hash[:name]))
+ end
+ assert_kind_of(expected_class, cgi[name])
+ assert_equal(expected, cgi[name].read())
+ assert_equal(hash[:filename] || '', cgi[name].original_filename) #if hash[:filename]
+ assert_equal(hash[:content_type] || '', cgi[name].content_type) #if hash[:content_type]
+ end
+ ensure
+ if cgi
+ cgi.params.each {|name, vals|
+ vals.each {|val|
+ if val.kind_of?(Tempfile) && val.path
+ val.close!
+ end
+ }
+ }
+ end
+ end
+
+
+ def _read(basename)
+ filename = File.join(File.dirname(__FILE__), 'testdata', basename)
+ s = File.open(filename, 'rb') {|f| f.read() }
+
+ return s
+ end
+
+
+ def test_cgi_multipart_stringio
+ @boundary = '----WebKitFormBoundaryAAfvAII+YL9102cX'
+ @data = [
+ {:name=>'hidden1', :value=>'foobar'},
+ {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"},
+ {:name=>'file1', :value=>_read('file1.html'),
+ :filename=>'file1.html', :content_type=>'text/html'},
+ {:name=>'image1', :value=>_read('small.png'),
+ :filename=>'small.png', :content_type=>'image/png'}, # small image
+ ]
+ @data[1][:value].force_encoding(::Encoding::UTF_8) if defined?(::Encoding)
+ @expected_class = StringIO
+ _test_multipart()
+ end
+
+
+ def test_cgi_multipart_tempfile
+ @boundary = '----WebKitFormBoundaryAAfvAII+YL9102cX'
+ @data = [
+ {:name=>'hidden1', :value=>'foobar'},
+ {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"},
+ {:name=>'file1', :value=>_read('file1.html'),
+ :filename=>'file1.html', :content_type=>'text/html'},
+ {:name=>'image1', :value=>_read('large.png'),
+ :filename=>'large.png', :content_type=>'image/png'}, # large image
+ ]
+ @data[1][:value].force_encoding(::Encoding::UTF_8) if defined?(::Encoding)
+ @expected_class = Tempfile
+ _test_multipart()
+ end
+
+
+ def _set_const(klass, name, value)
+ old = nil
+ klass.class_eval do
+ old = const_get(name)
+ remove_const(name)
+ const_set(name, value)
+ end
+ return old
+ end
+
+
+ def test_cgi_multipart_maxmultipartlength
+ @data = [
+ {:name=>'image1', :value=>_read('large.png'),
+ :filename=>'large.png', :content_type=>'image/png'}, # large image
+ ]
+ begin
+ ex = assert_raise(StandardError) do
+ _test_multipart(:max_multipart_length=>2 * 1024) # set via simple scalar
+ end
+ assert_equal("too large multipart data.", ex.message)
+ ensure
+ end
+ end
+
+
+ def test_cgi_multipart_maxmultipartlength_lambda
+ @data = [
+ {:name=>'image1', :value=>_read('large.png'),
+ :filename=>'large.png', :content_type=>'image/png'}, # large image
+ ]
+ begin
+ ex = assert_raise(StandardError) do
+ _test_multipart(:max_multipart_length=>lambda{2*1024}) # set via lambda
+ end
+ assert_equal("too large multipart data.", ex.message)
+ ensure
+ end
+ end
+
+
+ def test_cgi_multipart_maxmultipartcount
+ @data = [
+ {:name=>'file1', :value=>_read('file1.html'),
+ :filename=>'file1.html', :content_type=>'text/html'},
+ ]
+ item = @data.first
+ 500.times { @data << item }
+ #original = _set_const(CGI, :MAX_MULTIPART_COUNT, 128)
+ begin
+ ex = assert_raise(StandardError) do
+ _test_multipart()
+ end
+ assert_equal("too many parameters.", ex.message)
+ ensure
+ #_set_const(CGI, :MAX_MULTIPART_COUNT, original)
+ end
+ end if CGI.const_defined?(:MAX_MULTIPART_COUNT)
+
+
+ def test_cgi_multipart_badbody ## [ruby-dev:28470]
+ @data = [
+ {:name=>'file1', :value=>_read('file1.html'),
+ :filename=>'file1.html', :content_type=>'text/html'},
+ ]
+ _prepare(@data) do |input|
+ input2 = input.sub(/--(\r\n)?\z/, "\r\n")
+ assert input2 != input
+ #p input2
+ input2
+ end
+ ex = assert_raise(EOFError) do
+ CGI.new(:accept_charset=>"UTF-8")
+ end
+ assert_equal("bad content body", ex.message)
+ #
+ _prepare(@data) do |input|
+ input2 = input.sub(/--(\r\n)?\z/, "")
+ assert input2 != input
+ #p input2
+ input2
+ end
+ ex = assert_raise(EOFError) do
+ CGI.new(:accept_charset=>"UTF-8")
+ end
+ assert_equal("bad content body", ex.message)
+ end
+
+
+ def test_cgi_multipart_quoteboundary ## [JVN#84798830]
+ @boundary = '(.|\n)*'
+ @data = [
+ {:name=>'hidden1', :value=>'foobar'},
+ {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"},
+ {:name=>'file1', :value=>_read('file1.html'),
+ :filename=>'file1.html', :content_type=>'text/html'},
+ {:name=>'image1', :value=>_read('small.png'),
+ :filename=>'small.png', :content_type=>'image/png'}, # small image
+ ]
+ @data[1][:value].force_encoding("UTF-8")
+ _prepare(@data)
+ cgi = CGI.new(:accept_charset=>"UTF-8")
+ assert_equal('file1.html', cgi['file1'].original_filename)
+ end
+
+ def test_cgi_multipart_boundary_10240 # [Bug #3866]
+ @boundary = 'AaB03x'
+ @data = [
+ {:name=>'file', :value=>"b"*10134,
+ :filename=>'file.txt', :content_type=>'text/plain'},
+ {:name=>'foo', :value=>"bar"},
+ ]
+ _prepare(@data)
+ cgi = CGI.new(:accept_charset=>"UTF-8")
+ assert_equal(cgi['foo'], 'bar')
+ assert_equal(cgi['file'].read, 'b'*10134)
+ cgi['file'].close! if cgi['file'].kind_of? Tempfile
+ end
+
+ def test_cgi_multipart_without_tempfile
+ assert_in_out_err([], <<-'EOM')
+ require 'cgi'
+ require 'stringio'
+ ENV['REQUEST_METHOD'] = 'POST'
+ ENV['CONTENT_TYPE'] = 'multipart/form-data; boundary=foobar1234'
+ body = <<-BODY
+--foobar1234
+Content-Disposition: form-data: name=\"name1\"
+
+value1
+--foobar1234
+Content-Disposition: form-data: name=\"file1\"; filename=\"file1.html\"
+Content-Type: text/html
+
+<html>
+<body><p>Hello</p></body>
+</html>
+
+--foobar1234--
+BODY
+ body.gsub!(/\n/, "\r\n")
+ ENV['CONTENT_LENGTH'] = body.size.to_s
+ $stdin = StringIO.new(body)
+ CGI.new
+ EOM
+ end
+
+ ###
+
+ self.instance_methods.each do |method|
+ private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
+ end if ENV['TEST']
+
+end
diff --git a/jni/ruby/test/cgi/test_cgi_session.rb b/jni/ruby/test/cgi/test_cgi_session.rb
new file mode 100644
index 0000000..8bd5177
--- /dev/null
+++ b/jni/ruby/test/cgi/test_cgi_session.rb
@@ -0,0 +1,172 @@
+require 'test/unit'
+require 'cgi'
+require 'cgi/session'
+require 'cgi/session/pstore'
+require 'stringio'
+require 'tmpdir'
+
+class CGISessionTest < Test::Unit::TestCase
+ def setup
+ @session_dir = Dir.mktmpdir(%w'session dir')
+ end
+
+ def teardown
+ @environ.each do |key, val| ENV.delete(key) end
+ $stdout = STDOUT
+ FileUtils.rm_rf(@session_dir)
+ end
+
+ def test_cgi_session_filestore
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
+ # 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ value1="value1"
+ value2="\x8F\xBC\x8D]"
+ value2.force_encoding("SJIS") if defined?(::Encoding)
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>@session_dir)
+ session["key1"]=value1
+ session["key2"]=value2
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+ $stdout = StringIO.new
+ cgi.out{""}
+
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'HTTP_COOKIE' => "_session_id=#{session_id}",
+ 'QUERY_STRING' => "_session_id=#{session.session_id}",
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>@session_dir)
+ $stdout = StringIO.new
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+
+ end
+ def test_cgi_session_pstore
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
+ # 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ value1="value1"
+ value2="\x8F\xBC\x8D]"
+ value2.force_encoding("SJIS") if defined?(::Encoding)
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"database_manager"=>CGI::Session::PStore)
+ session["key1"]=value1
+ session["key2"]=value2
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+ $stdout = StringIO.new
+ cgi.out{""}
+
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'HTTP_COOKIE' => "_session_id=#{session_id}",
+ 'QUERY_STRING' => "_session_id=#{session.session_id}",
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"database_manager"=>CGI::Session::PStore)
+ $stdout = StringIO.new
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+ end
+ def test_cgi_session_specify_session_id
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
+ # 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ value1="value1"
+ value2="\x8F\xBC\x8D]"
+ value2.force_encoding("SJIS") if defined?(::Encoding)
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"session_id"=>"foo")
+ session["key1"]=value1
+ session["key2"]=value2
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ assert_equal("foo",session.session_id)
+ #session_id=session.session_id
+ session.close
+ $stdout = StringIO.new
+ cgi.out{""}
+
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'HTTP_COOKIE' => "_session_id=#{session_id}",
+ 'QUERY_STRING' => "_session_id=#{session.session_id}",
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>@session_dir)
+ $stdout = StringIO.new
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ assert_equal("foo",session.session_id)
+ session.close
+ end
+ def test_cgi_session_specify_session_key
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ # 'QUERY_STRING' => 'id=123&id=456&id=&str=%40h+%3D%7E+%2F%5E%24%2F',
+ # 'HTTP_COOKIE' => '_session_id=12345; name1=val1&val2;',
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ value1="value1"
+ value2="\x8F\xBC\x8D]"
+ value2.force_encoding("SJIS") if defined?(::Encoding)
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"session_key"=>"bar")
+ session["key1"]=value1
+ session["key2"]=value2
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session_id=session.session_id
+ session.close
+ $stdout = StringIO.new
+ cgi.out{""}
+
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ 'HTTP_COOKIE' => "bar=#{session_id}",
+ # 'QUERY_STRING' => "bar=#{session.session_id}",
+ 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ }
+ ENV.update(@environ)
+ cgi = CGI.new
+ session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"session_key"=>"bar")
+ $stdout = StringIO.new
+ assert_equal(value1,session["key1"])
+ assert_equal(value2,session["key2"])
+ session.close
+ end
+end
diff --git a/jni/ruby/test/cgi/test_cgi_tag_helper.rb b/jni/ruby/test/cgi/test_cgi_tag_helper.rb
new file mode 100644
index 0000000..eb3c20a
--- /dev/null
+++ b/jni/ruby/test/cgi/test_cgi_tag_helper.rb
@@ -0,0 +1,353 @@
+require 'test/unit'
+require 'cgi'
+require 'stringio'
+
+
+class CGITagHelperTest < Test::Unit::TestCase
+
+
+ def setup
+ #@environ = {
+ # 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ # 'REQUEST_METHOD' => 'GET',
+ # 'SERVER_SOFTWARE' => 'Apache 2.2.0',
+ #}
+ #ENV.update(@environ)
+ end
+
+
+ def teardown
+ @environ.each do |key, val| ENV.delete(key) end
+ $stdout = STDOUT
+ end
+
+
+ def test_cgi_tag_helper_html3
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ }
+ ENV.update(@environ)
+ ## html3
+ cgi = CGI.new('html3')
+ assert_equal('<A HREF=""></A>',cgi.a)
+ assert_equal('<A HREF="bar"></A>',cgi.a('bar'))
+ assert_equal('<A HREF="">foo</A>',cgi.a{'foo'})
+ assert_equal('<A HREF="bar">foo</A>',cgi.a('bar'){'foo'})
+ assert_equal('<TT></TT>',cgi.tt)
+ assert_equal('<TT></TT>',cgi.tt('bar'))
+ assert_equal('<TT>foo</TT>',cgi.tt{'foo'})
+ assert_equal('<TT>foo</TT>',cgi.tt('bar'){'foo'})
+ assert_equal('<I></I>',cgi.i)
+ assert_equal('<I></I>',cgi.i('bar'))
+ assert_equal('<I>foo</I>',cgi.i{'foo'})
+ assert_equal('<I>foo</I>',cgi.i('bar'){'foo'})
+ assert_equal('<B></B>',cgi.b)
+ assert_equal('<B></B>',cgi.b('bar'))
+ assert_equal('<B>foo</B>',cgi.b{'foo'})
+ assert_equal('<B>foo</B>',cgi.b('bar'){'foo'})
+ assert_equal('<U></U>',cgi.u)
+ assert_equal('<U></U>',cgi.u('bar'))
+ assert_equal('<U>foo</U>',cgi.u{'foo'})
+ assert_equal('<U>foo</U>',cgi.u('bar'){'foo'})
+ assert_equal('<STRIKE></STRIKE>',cgi.strike)
+ assert_equal('<STRIKE></STRIKE>',cgi.strike('bar'))
+ assert_equal('<STRIKE>foo</STRIKE>',cgi.strike{'foo'})
+ assert_equal('<STRIKE>foo</STRIKE>',cgi.strike('bar'){'foo'})
+ assert_equal('<BIG></BIG>',cgi.big)
+ assert_equal('<BIG></BIG>',cgi.big('bar'))
+ assert_equal('<BIG>foo</BIG>',cgi.big{'foo'})
+ assert_equal('<BIG>foo</BIG>',cgi.big('bar'){'foo'})
+ assert_equal('<SMALL></SMALL>',cgi.small)
+ assert_equal('<SMALL></SMALL>',cgi.small('bar'))
+ assert_equal('<SMALL>foo</SMALL>',cgi.small{'foo'})
+ assert_equal('<SMALL>foo</SMALL>',cgi.small('bar'){'foo'})
+ assert_equal('<SUB></SUB>',cgi.sub)
+ assert_equal('<SUB></SUB>',cgi.sub('bar'))
+ assert_equal('<SUB>foo</SUB>',cgi.sub{'foo'})
+ assert_equal('<SUB>foo</SUB>',cgi.sub('bar'){'foo'})
+ assert_equal('<SUP></SUP>',cgi.sup)
+ assert_equal('<SUP></SUP>',cgi.sup('bar'))
+ assert_equal('<SUP>foo</SUP>',cgi.sup{'foo'})
+ assert_equal('<SUP>foo</SUP>',cgi.sup('bar'){'foo'})
+ assert_equal('<EM></EM>',cgi.em)
+ assert_equal('<EM></EM>',cgi.em('bar'))
+ assert_equal('<EM>foo</EM>',cgi.em{'foo'})
+ assert_equal('<EM>foo</EM>',cgi.em('bar'){'foo'})
+ assert_equal('<STRONG></STRONG>',cgi.strong)
+ assert_equal('<STRONG></STRONG>',cgi.strong('bar'))
+ assert_equal('<STRONG>foo</STRONG>',cgi.strong{'foo'})
+ assert_equal('<STRONG>foo</STRONG>',cgi.strong('bar'){'foo'})
+ assert_equal('<DFN></DFN>',cgi.dfn)
+ assert_equal('<DFN></DFN>',cgi.dfn('bar'))
+ assert_equal('<DFN>foo</DFN>',cgi.dfn{'foo'})
+ assert_equal('<DFN>foo</DFN>',cgi.dfn('bar'){'foo'})
+ assert_equal('<CODE></CODE>',cgi.code)
+ assert_equal('<CODE></CODE>',cgi.code('bar'))
+ assert_equal('<CODE>foo</CODE>',cgi.code{'foo'})
+ assert_equal('<CODE>foo</CODE>',cgi.code('bar'){'foo'})
+ assert_equal('<SAMP></SAMP>',cgi.samp)
+ assert_equal('<SAMP></SAMP>',cgi.samp('bar'))
+ assert_equal('<SAMP>foo</SAMP>',cgi.samp{'foo'})
+ assert_equal('<SAMP>foo</SAMP>',cgi.samp('bar'){'foo'})
+ assert_equal('<KBD></KBD>',cgi.kbd)
+ assert_equal('<KBD></KBD>',cgi.kbd('bar'))
+ assert_equal('<KBD>foo</KBD>',cgi.kbd{'foo'})
+ assert_equal('<KBD>foo</KBD>',cgi.kbd('bar'){'foo'})
+ assert_equal('<VAR></VAR>',cgi.var)
+ assert_equal('<VAR></VAR>',cgi.var('bar'))
+ assert_equal('<VAR>foo</VAR>',cgi.var{'foo'})
+ assert_equal('<VAR>foo</VAR>',cgi.var('bar'){'foo'})
+ assert_equal('<CITE></CITE>',cgi.cite)
+ assert_equal('<CITE></CITE>',cgi.cite('bar'))
+ assert_equal('<CITE>foo</CITE>',cgi.cite{'foo'})
+ assert_equal('<CITE>foo</CITE>',cgi.cite('bar'){'foo'})
+ assert_equal('<FONT></FONT>',cgi.font)
+ assert_equal('<FONT></FONT>',cgi.font('bar'))
+ assert_equal('<FONT>foo</FONT>',cgi.font{'foo'})
+ assert_equal('<FONT>foo</FONT>',cgi.font('bar'){'foo'})
+ assert_equal('<ADDRESS></ADDRESS>',cgi.address)
+ assert_equal('<ADDRESS></ADDRESS>',cgi.address('bar'))
+ assert_equal('<ADDRESS>foo</ADDRESS>',cgi.address{'foo'})
+ assert_equal('<ADDRESS>foo</ADDRESS>',cgi.address('bar'){'foo'})
+ assert_equal('<DIV></DIV>',cgi.div)
+ assert_equal('<DIV></DIV>',cgi.div('bar'))
+ assert_equal('<DIV>foo</DIV>',cgi.div{'foo'})
+ assert_equal('<DIV>foo</DIV>',cgi.div('bar'){'foo'})
+ assert_equal('<CENTER></CENTER>',cgi.center)
+ assert_equal('<CENTER></CENTER>',cgi.center('bar'))
+ assert_equal('<CENTER>foo</CENTER>',cgi.center{'foo'})
+ assert_equal('<CENTER>foo</CENTER>',cgi.center('bar'){'foo'})
+ assert_equal('<MAP></MAP>',cgi.map)
+ assert_equal('<MAP></MAP>',cgi.map('bar'))
+ assert_equal('<MAP>foo</MAP>',cgi.map{'foo'})
+ assert_equal('<MAP>foo</MAP>',cgi.map('bar'){'foo'})
+ assert_equal('<APPLET></APPLET>',cgi.applet)
+ assert_equal('<APPLET></APPLET>',cgi.applet('bar'))
+ assert_equal('<APPLET>foo</APPLET>',cgi.applet{'foo'})
+ assert_equal('<APPLET>foo</APPLET>',cgi.applet('bar'){'foo'})
+ assert_equal('<PRE></PRE>',cgi.pre)
+ assert_equal('<PRE></PRE>',cgi.pre('bar'))
+ assert_equal('<PRE>foo</PRE>',cgi.pre{'foo'})
+ assert_equal('<PRE>foo</PRE>',cgi.pre('bar'){'foo'})
+ assert_equal('<XMP></XMP>',cgi.xmp)
+ assert_equal('<XMP></XMP>',cgi.xmp('bar'))
+ assert_equal('<XMP>foo</XMP>',cgi.xmp{'foo'})
+ assert_equal('<XMP>foo</XMP>',cgi.xmp('bar'){'foo'})
+ assert_equal('<LISTING></LISTING>',cgi.listing)
+ assert_equal('<LISTING></LISTING>',cgi.listing('bar'))
+ assert_equal('<LISTING>foo</LISTING>',cgi.listing{'foo'})
+ assert_equal('<LISTING>foo</LISTING>',cgi.listing('bar'){'foo'})
+ assert_equal('<DL></DL>',cgi.dl)
+ assert_equal('<DL></DL>',cgi.dl('bar'))
+ assert_equal('<DL>foo</DL>',cgi.dl{'foo'})
+ assert_equal('<DL>foo</DL>',cgi.dl('bar'){'foo'})
+ assert_equal('<OL></OL>',cgi.ol)
+ assert_equal('<OL></OL>',cgi.ol('bar'))
+ assert_equal('<OL>foo</OL>',cgi.ol{'foo'})
+ assert_equal('<OL>foo</OL>',cgi.ol('bar'){'foo'})
+ assert_equal('<UL></UL>',cgi.ul)
+ assert_equal('<UL></UL>',cgi.ul('bar'))
+ assert_equal('<UL>foo</UL>',cgi.ul{'foo'})
+ assert_equal('<UL>foo</UL>',cgi.ul('bar'){'foo'})
+ assert_equal('<DIR></DIR>',cgi.dir)
+ assert_equal('<DIR></DIR>',cgi.dir('bar'))
+ assert_equal('<DIR>foo</DIR>',cgi.dir{'foo'})
+ assert_equal('<DIR>foo</DIR>',cgi.dir('bar'){'foo'})
+ assert_equal('<MENU></MENU>',cgi.menu)
+ assert_equal('<MENU></MENU>',cgi.menu('bar'))
+ assert_equal('<MENU>foo</MENU>',cgi.menu{'foo'})
+ assert_equal('<MENU>foo</MENU>',cgi.menu('bar'){'foo'})
+ assert_equal('<SELECT></SELECT>',cgi.select)
+ assert_equal('<SELECT></SELECT>',cgi.select('bar'))
+ assert_equal('<SELECT>foo</SELECT>',cgi.select{'foo'})
+ assert_equal('<SELECT>foo</SELECT>',cgi.select('bar'){'foo'})
+ assert_equal('<TABLE></TABLE>',cgi.table)
+ assert_equal('<TABLE></TABLE>',cgi.table('bar'))
+ assert_equal('<TABLE>foo</TABLE>',cgi.table{'foo'})
+ assert_equal('<TABLE>foo</TABLE>',cgi.table('bar'){'foo'})
+ assert_equal('<TITLE></TITLE>',cgi.title)
+ assert_equal('<TITLE></TITLE>',cgi.title('bar'))
+ assert_equal('<TITLE>foo</TITLE>',cgi.title{'foo'})
+ assert_equal('<TITLE>foo</TITLE>',cgi.title('bar'){'foo'})
+ assert_equal('<STYLE></STYLE>',cgi.style)
+ assert_equal('<STYLE></STYLE>',cgi.style('bar'))
+ assert_equal('<STYLE>foo</STYLE>',cgi.style{'foo'})
+ assert_equal('<STYLE>foo</STYLE>',cgi.style('bar'){'foo'})
+ assert_equal('<SCRIPT></SCRIPT>',cgi.script)
+ assert_equal('<SCRIPT></SCRIPT>',cgi.script('bar'))
+ assert_equal('<SCRIPT>foo</SCRIPT>',cgi.script{'foo'})
+ assert_equal('<SCRIPT>foo</SCRIPT>',cgi.script('bar'){'foo'})
+ assert_equal('<H1></H1>',cgi.h1)
+ assert_equal('<H1></H1>',cgi.h1('bar'))
+ assert_equal('<H1>foo</H1>',cgi.h1{'foo'})
+ assert_equal('<H1>foo</H1>',cgi.h1('bar'){'foo'})
+ assert_equal('<H2></H2>',cgi.h2)
+ assert_equal('<H2></H2>',cgi.h2('bar'))
+ assert_equal('<H2>foo</H2>',cgi.h2{'foo'})
+ assert_equal('<H2>foo</H2>',cgi.h2('bar'){'foo'})
+ assert_equal('<H3></H3>',cgi.h3)
+ assert_equal('<H3></H3>',cgi.h3('bar'))
+ assert_equal('<H3>foo</H3>',cgi.h3{'foo'})
+ assert_equal('<H3>foo</H3>',cgi.h3('bar'){'foo'})
+ assert_equal('<H4></H4>',cgi.h4)
+ assert_equal('<H4></H4>',cgi.h4('bar'))
+ assert_equal('<H4>foo</H4>',cgi.h4{'foo'})
+ assert_equal('<H4>foo</H4>',cgi.h4('bar'){'foo'})
+ assert_equal('<H5></H5>',cgi.h5)
+ assert_equal('<H5></H5>',cgi.h5('bar'))
+ assert_equal('<H5>foo</H5>',cgi.h5{'foo'})
+ assert_equal('<H5>foo</H5>',cgi.h5('bar'){'foo'})
+ assert_equal('<H6></H6>',cgi.h6)
+ assert_equal('<H6></H6>',cgi.h6('bar'))
+ assert_equal('<H6>foo</H6>',cgi.h6{'foo'})
+ assert_equal('<H6>foo</H6>',cgi.h6('bar'){'foo'})
+ assert_match(/^<TEXTAREA .*><\/TEXTAREA>$/,cgi.textarea)
+ assert_match(/COLS="70"/,cgi.textarea)
+ assert_match(/ROWS="10"/,cgi.textarea)
+ assert_match(/NAME=""/,cgi.textarea)
+ assert_match(/^<TEXTAREA .*><\/TEXTAREA>$/,cgi.textarea("bar"))
+ assert_match(/COLS="70"/,cgi.textarea("bar"))
+ assert_match(/ROWS="10"/,cgi.textarea("bar"))
+ assert_match(/NAME="bar"/,cgi.textarea("bar"))
+ assert_match(/^<TEXTAREA .*>foo<\/TEXTAREA>$/,cgi.textarea{"foo"})
+ assert_match(/COLS="70"/,cgi.textarea{"foo"})
+ assert_match(/ROWS="10"/,cgi.textarea{"foo"})
+ assert_match(/NAME=""/,cgi.textarea{"foo"})
+ assert_match(/^<TEXTAREA .*>foo<\/TEXTAREA>$/,cgi.textarea("bar"){"foo"})
+ assert_match(/COLS="70"/,cgi.textarea("bar"){"foo"})
+ assert_match(/ROWS="10"/,cgi.textarea("bar"){"foo"})
+ assert_match(/NAME="bar"/,cgi.textarea("bar"){"foo"})
+ assert_match(/^<FORM .*><\/FORM>$/,cgi.form)
+ assert_match(/METHOD="post"/,cgi.form)
+ assert_match(/ENCTYPE="application\/x-www-form-urlencoded"/,cgi.form)
+ assert_match(/^<FORM .*><\/FORM>$/,cgi.form("bar"))
+ assert_match(/METHOD="bar"/,cgi.form("bar"))
+ assert_match(/ENCTYPE="application\/x-www-form-urlencoded"/,cgi.form("bar"))
+ assert_match(/^<FORM .*>foo<\/FORM>$/,cgi.form{"foo"})
+ assert_match(/METHOD="post"/,cgi.form{"foo"})
+ assert_match(/ENCTYPE="application\/x-www-form-urlencoded"/,cgi.form{"foo"})
+ assert_match(/^<FORM .*>foo<\/FORM>$/,cgi.form("bar"){"foo"})
+ assert_match(/METHOD="bar"/,cgi.form("bar"){"foo"})
+ assert_match(/ENCTYPE="application\/x-www-form-urlencoded"/,cgi.form("bar"){"foo"})
+ assert_equal('<BLOCKQUOTE></BLOCKQUOTE>',cgi.blockquote)
+ assert_equal('<BLOCKQUOTE CITE="bar"></BLOCKQUOTE>',cgi.blockquote('bar'))
+ assert_equal('<BLOCKQUOTE>foo</BLOCKQUOTE>',cgi.blockquote{'foo'})
+ assert_equal('<BLOCKQUOTE CITE="bar">foo</BLOCKQUOTE>',cgi.blockquote('bar'){'foo'})
+ assert_equal('<CAPTION></CAPTION>',cgi.caption)
+ assert_equal('<CAPTION ALIGN="bar"></CAPTION>',cgi.caption('bar'))
+ assert_equal('<CAPTION>foo</CAPTION>',cgi.caption{'foo'})
+ assert_equal('<CAPTION ALIGN="bar">foo</CAPTION>',cgi.caption('bar'){'foo'})
+ assert_equal('<IMG SRC="" ALT="">',cgi.img)
+ assert_equal('<IMG SRC="bar" ALT="">',cgi.img('bar'))
+ assert_equal('<IMG SRC="" ALT="">',cgi.img{'foo'})
+ assert_equal('<IMG SRC="bar" ALT="">',cgi.img('bar'){'foo'})
+ assert_equal('<BASE HREF="">',cgi.base)
+ assert_equal('<BASE HREF="bar">',cgi.base('bar'))
+ assert_equal('<BASE HREF="">',cgi.base{'foo'})
+ assert_equal('<BASE HREF="bar">',cgi.base('bar'){'foo'})
+ assert_equal('<BASEFONT>',cgi.basefont)
+ assert_equal('<BASEFONT>',cgi.basefont('bar'))
+ assert_equal('<BASEFONT>',cgi.basefont{'foo'})
+ assert_equal('<BASEFONT>',cgi.basefont('bar'){'foo'})
+ assert_equal('<BR>',cgi.br)
+ assert_equal('<BR>',cgi.br('bar'))
+ assert_equal('<BR>',cgi.br{'foo'})
+ assert_equal('<BR>',cgi.br('bar'){'foo'})
+ assert_equal('<AREA>',cgi.area)
+ assert_equal('<AREA>',cgi.area('bar'))
+ assert_equal('<AREA>',cgi.area{'foo'})
+ assert_equal('<AREA>',cgi.area('bar'){'foo'})
+ assert_equal('<LINK>',cgi.link)
+ assert_equal('<LINK>',cgi.link('bar'))
+ assert_equal('<LINK>',cgi.link{'foo'})
+ assert_equal('<LINK>',cgi.link('bar'){'foo'})
+ assert_equal('<PARAM>',cgi.param)
+ assert_equal('<PARAM>',cgi.param('bar'))
+ assert_equal('<PARAM>',cgi.param{'foo'})
+ assert_equal('<PARAM>',cgi.param('bar'){'foo'})
+ assert_equal('<HR>',cgi.hr)
+ assert_equal('<HR>',cgi.hr('bar'))
+ assert_equal('<HR>',cgi.hr{'foo'})
+ assert_equal('<HR>',cgi.hr('bar'){'foo'})
+ assert_equal('<INPUT>',cgi.input)
+ assert_equal('<INPUT>',cgi.input('bar'))
+ assert_equal('<INPUT>',cgi.input{'foo'})
+ assert_equal('<INPUT>',cgi.input('bar'){'foo'})
+ assert_equal('<ISINDEX>',cgi.isindex)
+ assert_equal('<ISINDEX>',cgi.isindex('bar'))
+ assert_equal('<ISINDEX>',cgi.isindex{'foo'})
+ assert_equal('<ISINDEX>',cgi.isindex('bar'){'foo'})
+ assert_equal('<META>',cgi.meta)
+ assert_equal('<META>',cgi.meta('bar'))
+ assert_equal('<META>',cgi.meta{'foo'})
+ assert_equal('<META>',cgi.meta('bar'){'foo'})
+ assert_equal('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>',cgi.html)
+ assert_equal('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>foo</HTML>',cgi.html{'foo'})
+ assert_equal('<HEAD>',cgi.head)
+ assert_equal('<HEAD>foo</HEAD>',cgi.head{'foo'})
+ assert_equal('<BODY>',cgi.body)
+ assert_equal('<BODY>foo</BODY>',cgi.body{'foo'})
+ assert_equal('<P>',cgi.p)
+ assert_equal('<P>foo</P>',cgi.p{'foo'})
+ assert_equal('<PLAINTEXT>',cgi.plaintext)
+ assert_equal('<PLAINTEXT>foo</PLAINTEXT>',cgi.plaintext{'foo'})
+ assert_equal('<DT>',cgi.dt)
+ assert_equal('<DT>foo</DT>',cgi.dt{'foo'})
+ assert_equal('<DD>',cgi.dd)
+ assert_equal('<DD>foo</DD>',cgi.dd{'foo'})
+ assert_equal('<LI>',cgi.li)
+ assert_equal('<LI>foo</LI>',cgi.li{'foo'})
+ assert_equal('<OPTION>',cgi.option)
+ assert_equal('<OPTION>foo</OPTION>',cgi.option{'foo'})
+ assert_equal('<TR>',cgi.tr)
+ assert_equal('<TR>foo</TR>',cgi.tr{'foo'})
+ assert_equal('<TH>',cgi.th)
+ assert_equal('<TH>foo</TH>',cgi.th{'foo'})
+ assert_equal('<TD>',cgi.td)
+ assert_equal('<TD>foo</TD>',cgi.td{'foo'})
+ str=cgi.checkbox_group("foo",["aa","bb"],["cc","dd"])
+ assert_match(/^<INPUT .*VALUE="aa".*>bb<INPUT .*VALUE="cc".*>dd$/,str)
+ assert_match(/^<INPUT .*TYPE="checkbox".*>bb<INPUT .*TYPE="checkbox".*>dd$/,str)
+ assert_match(/^<INPUT .*NAME="foo".*>bb<INPUT .*NAME="foo".*>dd$/,str)
+ str=cgi.radio_group("foo",["aa","bb"],["cc","dd"])
+ assert_match(/^<INPUT .*VALUE="aa".*>bb<INPUT .*VALUE="cc".*>dd$/,str)
+ assert_match(/^<INPUT .*TYPE="radio".*>bb<INPUT .*TYPE="radio".*>dd$/,str)
+ assert_match(/^<INPUT .*NAME="foo".*>bb<INPUT .*NAME="foo".*>dd$/,str)
+ str=cgi.checkbox_group("foo",["aa","bb"],["cc","dd",true])
+ assert_match(/^<INPUT .*VALUE="aa".*>bb<INPUT .*VALUE="cc".*>dd$/,str)
+ assert_match(/^<INPUT .*TYPE="checkbox".*>bb<INPUT .*TYPE="checkbox".*>dd$/,str)
+ assert_match(/^<INPUT .*NAME="foo".*>bb<INPUT .*NAME="foo".*>dd$/,str)
+ assert_match(/^<INPUT .*>bb<INPUT .*CHECKED.*>dd$/,str)
+ assert_match(/<INPUT .*TYPE="text".*>/,cgi.text_field(:name=>"name",:value=>"value"))
+ str=cgi.radio_group("foo",["aa","bb"],["cc","dd",false])
+ assert_match(/^<INPUT .*VALUE="aa".*>bb<INPUT .*VALUE="cc".*>dd$/,str)
+ assert_match(/^<INPUT .*TYPE="radio".*>bb<INPUT .*TYPE="radio".*>dd$/,str)
+ assert_match(/^<INPUT .*NAME="foo".*>bb<INPUT .*NAME="foo".*>dd$/,str)
+ end
+
+=begin
+ def test_cgi_tag_helper_html4
+ ## html4
+ cgi = CGI.new('html4')
+ ## html4 transitional
+ cgi = CGI.new('html4Tr')
+ ## html4 frameset
+ cgi = CGI.new('html4Fr')
+ end
+=end
+
+ def test_cgi_tag_helper_html5
+ @environ = {
+ 'REQUEST_METHOD' => 'GET',
+ }
+ ENV.update(@environ)
+ ## html5
+ cgi = CGI.new('html5')
+ assert_equal('<HEADER></HEADER>',cgi.header)
+ assert_equal('<FOOTER></FOOTER>',cgi.footer)
+ assert_equal('<ARTICLE></ARTICLE>',cgi.article)
+ assert_equal('<SECTION></SECTION>',cgi.section)
+ assert_equal('<!DOCTYPE HTML><HTML BLA="TEST"></HTML>',cgi.html("BLA"=>"TEST"){})
+ end
+
+end
diff --git a/jni/ruby/test/cgi/test_cgi_util.rb b/jni/ruby/test/cgi/test_cgi_util.rb
new file mode 100644
index 0000000..802379d
--- /dev/null
+++ b/jni/ruby/test/cgi/test_cgi_util.rb
@@ -0,0 +1,104 @@
+require 'test/unit'
+require 'cgi'
+require 'stringio'
+
+
+class CGIUtilTest < Test::Unit::TestCase
+ include CGI::Util
+
+ def setup
+ ENV['REQUEST_METHOD'] = 'GET'
+ @str1="&<>\" \xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93"
+ @str1.force_encoding("UTF-8") if defined?(::Encoding)
+ end
+
+ def teardown
+ %W[REQUEST_METHOD SCRIPT_NAME].each do |name|
+ ENV.delete(name)
+ end
+ end
+
+
+ def test_cgi_escape
+ assert_equal('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93', CGI::escape(@str1))
+ assert_equal('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93'.ascii_only?, CGI::escape(@str1).ascii_only?) if defined?(::Encoding)
+ end
+
+ def test_cgi_escape_with_invalid_byte_sequence
+ assert_nothing_raised(ArgumentError) do
+ assert_equal('%C0%3C%3C', CGI::escape("\xC0\<\<".force_encoding("UTF-8")))
+ end
+ end
+
+ def test_cgi_escape_preserve_encoding
+ assert_equal(Encoding::US_ASCII, CGI::escape("\xC0\<\<".force_encoding("US-ASCII")).encoding)
+ assert_equal(Encoding::ASCII_8BIT, CGI::escape("\xC0\<\<".force_encoding("ASCII-8BIT")).encoding)
+ assert_equal(Encoding::UTF_8, CGI::escape("\xC0\<\<".force_encoding("UTF-8")).encoding)
+ end
+
+ def test_cgi_unescape
+ assert_equal(@str1, CGI::unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93'))
+ assert_equal(@str1.encoding, CGI::unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93').encoding) if defined?(::Encoding)
+ assert_equal("\u{30E1 30E2 30EA 691C 7D22}", CGI.unescape("\u{30E1 30E2 30EA}%E6%A4%9C%E7%B4%A2"))
+ end
+
+ def test_cgi_unescape_preserve_encoding
+ assert_equal(Encoding::US_ASCII, CGI::unescape("%C0%3C%3C".force_encoding("US-ASCII")).encoding)
+ assert_equal(Encoding::ASCII_8BIT, CGI::unescape("%C0%3C%3C".force_encoding("ASCII-8BIT")).encoding)
+ assert_equal(Encoding::UTF_8, CGI::unescape("%C0%3C%3C".force_encoding("UTF-8")).encoding)
+ end
+
+ def test_cgi_pretty
+ assert_equal("<HTML>\n <BODY>\n </BODY>\n</HTML>\n",CGI::pretty("<HTML><BODY></BODY></HTML>"))
+ assert_equal("<HTML>\n\t<BODY>\n\t</BODY>\n</HTML>\n",CGI::pretty("<HTML><BODY></BODY></HTML>","\t"))
+ end
+
+ def test_cgi_escapeHTML
+ assert_equal(CGI::escapeHTML("'&\"><"),"&#39;&amp;&quot;&gt;&lt;")
+ end
+
+ def test_cgi_unescapeHTML
+ assert_equal(CGI::unescapeHTML("&#39;&amp;&quot;&gt;&lt;"),"'&\"><")
+ end
+
+ def test_cgi_unescapeHTML_uppercasecharacter
+ assert_equal(CGI::unescapeHTML("&#x3042;&#x3044;&#X3046;"),"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86")
+ end
+
+ def test_cgi_include_escape
+ assert_equal('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93', escape(@str1))
+ end
+
+ def test_cgi_include_escapeHTML
+ assert_equal(escapeHTML("'&\"><"),"&#39;&amp;&quot;&gt;&lt;")
+ end
+
+ def test_cgi_include_h
+ assert_equal(h("'&\"><"),"&#39;&amp;&quot;&gt;&lt;")
+ end
+
+ def test_cgi_include_unescape
+ assert_equal(@str1, unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93'))
+ assert_equal(@str1.encoding, unescape('%26%3C%3E%22+%E3%82%86%E3%82%93%E3%82%86%E3%82%93').encoding) if defined?(::Encoding)
+ assert_equal("\u{30E1 30E2 30EA 691C 7D22}", unescape("\u{30E1 30E2 30EA}%E6%A4%9C%E7%B4%A2"))
+ end
+
+ def test_cgi_include_unescapeHTML
+ assert_equal(unescapeHTML("&#39;&amp;&quot;&gt;&lt;"),"'&\"><")
+ end
+
+ def test_cgi_escapeElement
+ assert_equal("<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt;", escapeElement('<BR><A HREF="url"></A>', "A", "IMG"))
+ assert_equal("<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt;", escapeElement('<BR><A HREF="url"></A>', ["A", "IMG"]))
+ assert_equal("<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt;", escape_element('<BR><A HREF="url"></A>', "A", "IMG"))
+ assert_equal("<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt;", escape_element('<BR><A HREF="url"></A>', ["A", "IMG"]))
+ end
+
+
+ def test_cgi_unescapeElement
+ assert_equal('&lt;BR&gt;<A HREF="url"></A>', unescapeElement(escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG"))
+ assert_equal('&lt;BR&gt;<A HREF="url"></A>', unescapeElement(escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"]))
+ assert_equal('&lt;BR&gt;<A HREF="url"></A>', unescape_element(escapeHTML('<BR><A HREF="url"></A>'), "A", "IMG"))
+ assert_equal('&lt;BR&gt;<A HREF="url"></A>', unescape_element(escapeHTML('<BR><A HREF="url"></A>'), ["A", "IMG"]))
+ end
+end
diff --git a/jni/ruby/test/cgi/testdata/file1.html b/jni/ruby/test/cgi/testdata/file1.html
new file mode 100644
index 0000000..2ceaf6b
--- /dev/null
+++ b/jni/ruby/test/cgi/testdata/file1.html
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+ <head>
+ <title>ムスカ大佐のひとりごと</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF8">
+ </head>
+ <body>
+ <p>バカどもにはちょうどいい目くらましだ。</p>
+ </body>
+</html>
diff --git a/jni/ruby/test/cgi/testdata/large.png b/jni/ruby/test/cgi/testdata/large.png
new file mode 100644
index 0000000..d716396
--- /dev/null
+++ b/jni/ruby/test/cgi/testdata/large.png
Binary files differ
diff --git a/jni/ruby/test/cgi/testdata/small.png b/jni/ruby/test/cgi/testdata/small.png
new file mode 100644
index 0000000..753d58e
--- /dev/null
+++ b/jni/ruby/test/cgi/testdata/small.png
Binary files differ