2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2008-11-08 09:52:59 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'cgi'
|
|
|
|
require 'cgi/session'
|
2008-11-08 10:38:48 -05:00
|
|
|
require 'cgi/session/pstore'
|
2008-11-08 09:52:59 -05:00
|
|
|
require 'stringio'
|
2008-11-22 07:36:19 -05:00
|
|
|
require 'tmpdir'
|
2015-03-12 10:57:33 -04:00
|
|
|
require_relative 'update_env'
|
2008-11-08 09:52:59 -05:00
|
|
|
|
|
|
|
class CGISessionTest < Test::Unit::TestCase
|
2015-03-12 10:57:33 -04:00
|
|
|
include UpdateEnv
|
|
|
|
|
2008-11-08 09:52:59 -05:00
|
|
|
def setup
|
2015-03-12 10:57:33 -04:00
|
|
|
@environ = {}
|
2012-05-08 02:49:59 -04:00
|
|
|
@session_dir = Dir.mktmpdir(%w'session dir')
|
2008-11-08 09:52:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2015-03-12 10:57:33 -04:00
|
|
|
ENV.update(@environ)
|
2008-11-08 09:52:59 -05:00
|
|
|
$stdout = STDOUT
|
2009-07-30 01:34:02 -04:00
|
|
|
FileUtils.rm_rf(@session_dir)
|
2008-11-08 09:52:59 -05:00
|
|
|
end
|
|
|
|
|
2008-11-08 10:38:48 -05:00
|
|
|
def test_cgi_session_filestore
|
2015-03-12 10:57:33 -04:00
|
|
|
update_env(
|
2008-11-08 09:52:59 -05:00
|
|
|
'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',
|
2015-03-12 10:57:33 -04:00
|
|
|
)
|
2008-11-08 10:38:48 -05:00
|
|
|
value1="value1"
|
|
|
|
value2="\x8F\xBC\x8D]"
|
2009-12-22 19:14:48 -05:00
|
|
|
value2.force_encoding("SJIS") if defined?(::Encoding)
|
2008-11-08 09:52:59 -05:00
|
|
|
cgi = CGI.new
|
2008-11-22 07:36:19 -05:00
|
|
|
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir)
|
2008-11-08 10:38:48 -05:00
|
|
|
session["key1"]=value1
|
|
|
|
session["key2"]=value2
|
|
|
|
assert_equal(value1,session["key1"])
|
|
|
|
assert_equal(value2,session["key2"])
|
2008-11-08 09:52:59 -05:00
|
|
|
session.close
|
|
|
|
$stdout = StringIO.new
|
|
|
|
cgi.out{""}
|
|
|
|
|
2015-03-12 10:57:33 -04:00
|
|
|
update_env(
|
2008-11-08 09:52:59 -05:00
|
|
|
'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',
|
2015-03-12 10:57:33 -04:00
|
|
|
)
|
2008-11-08 09:52:59 -05:00
|
|
|
cgi = CGI.new
|
2008-11-22 07:36:19 -05:00
|
|
|
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir)
|
2008-11-08 09:52:59 -05:00
|
|
|
$stdout = StringIO.new
|
2008-11-08 10:38:48 -05:00
|
|
|
assert_equal(value1,session["key1"])
|
|
|
|
assert_equal(value2,session["key2"])
|
2008-11-08 09:52:59 -05:00
|
|
|
session.close
|
|
|
|
|
2008-11-08 10:38:48 -05:00
|
|
|
end
|
|
|
|
def test_cgi_session_pstore
|
2015-03-12 10:57:33 -04:00
|
|
|
update_env(
|
2008-11-08 10:38:48 -05:00
|
|
|
'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',
|
2015-03-12 10:57:33 -04:00
|
|
|
)
|
2008-11-08 10:38:48 -05:00
|
|
|
value1="value1"
|
|
|
|
value2="\x8F\xBC\x8D]"
|
2009-12-22 19:14:48 -05:00
|
|
|
value2.force_encoding("SJIS") if defined?(::Encoding)
|
2008-11-08 10:38:48 -05:00
|
|
|
cgi = CGI.new
|
2008-11-22 07:36:19 -05:00
|
|
|
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"database_manager"=>CGI::Session::PStore)
|
2008-11-08 10:38:48 -05:00
|
|
|
session["key1"]=value1
|
|
|
|
session["key2"]=value2
|
|
|
|
assert_equal(value1,session["key1"])
|
|
|
|
assert_equal(value2,session["key2"])
|
|
|
|
session.close
|
|
|
|
$stdout = StringIO.new
|
|
|
|
cgi.out{""}
|
|
|
|
|
2015-03-12 10:57:33 -04:00
|
|
|
update_env(
|
2008-11-08 10:38:48 -05:00
|
|
|
'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',
|
2015-03-12 10:57:33 -04:00
|
|
|
)
|
2008-11-08 10:38:48 -05:00
|
|
|
cgi = CGI.new
|
2008-11-22 07:36:19 -05:00
|
|
|
session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"database_manager"=>CGI::Session::PStore)
|
2008-11-08 10:38:48 -05:00
|
|
|
$stdout = StringIO.new
|
|
|
|
assert_equal(value1,session["key1"])
|
|
|
|
assert_equal(value2,session["key2"])
|
|
|
|
session.close
|
2008-12-18 07:20:13 -05:00
|
|
|
end
|
|
|
|
def test_cgi_session_specify_session_id
|
2015-03-12 10:57:33 -04:00
|
|
|
update_env(
|
2008-12-18 07:20:13 -05:00
|
|
|
'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',
|
2015-03-12 10:57:33 -04:00
|
|
|
)
|
2008-12-18 07:20:13 -05:00
|
|
|
value1="value1"
|
|
|
|
value2="\x8F\xBC\x8D]"
|
2009-12-22 19:14:48 -05:00
|
|
|
value2.force_encoding("SJIS") if defined?(::Encoding)
|
2008-12-18 07:20:13 -05:00
|
|
|
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)
|
2012-08-14 02:52:41 -04:00
|
|
|
#session_id=session.session_id
|
2008-12-18 07:20:13 -05:00
|
|
|
session.close
|
|
|
|
$stdout = StringIO.new
|
|
|
|
cgi.out{""}
|
2008-11-08 10:38:48 -05:00
|
|
|
|
2015-03-12 10:57:33 -04:00
|
|
|
update_env(
|
2008-12-18 07:20:13 -05:00
|
|
|
'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',
|
2015-03-12 10:57:33 -04:00
|
|
|
)
|
2008-12-18 07:20:13 -05:00
|
|
|
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
|
2015-03-12 10:57:33 -04:00
|
|
|
update_env(
|
2008-12-18 07:20:13 -05:00
|
|
|
'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',
|
2015-03-12 10:57:33 -04:00
|
|
|
)
|
2008-12-18 07:20:13 -05:00
|
|
|
value1="value1"
|
|
|
|
value2="\x8F\xBC\x8D]"
|
2009-12-22 19:14:48 -05:00
|
|
|
value2.force_encoding("SJIS") if defined?(::Encoding)
|
2008-12-18 07:20:13 -05:00
|
|
|
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{""}
|
2008-11-08 10:38:48 -05:00
|
|
|
|
2015-03-12 10:57:33 -04:00
|
|
|
update_env(
|
2008-12-18 07:20:13 -05:00
|
|
|
'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',
|
2015-03-12 10:57:33 -04:00
|
|
|
)
|
2008-12-18 07:20:13 -05:00
|
|
|
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
|
2008-11-08 09:52:59 -05:00
|
|
|
end
|
|
|
|
end
|