egalite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/README.md +91 -0
- data/auth/basic.rb +32 -0
- data/blank.rb +53 -0
- data/egalite.rb +742 -0
- data/errorconsole.rb +77 -0
- data/examples/simple/example.rb +39 -0
- data/examples/simple/pages/test.html +15 -0
- data/examples/simple_db/example_db.rb +103 -0
- data/examples/simple_db/pages/edit.html +6 -0
- data/helper.rb +251 -0
- data/keitai/keitai.rb +107 -0
- data/keitai/ketai.rb +11 -0
- data/keitai/rack/ketai/carrier/abstract.rb +131 -0
- data/keitai/rack/ketai/carrier/au.rb +78 -0
- data/keitai/rack/ketai/carrier/docomo.rb +80 -0
- data/keitai/rack/ketai/carrier/emoji/ausjisstrtoemojiid.rb +1391 -0
- data/keitai/rack/ketai/carrier/emoji/docomosjisstrtoemojiid.rb +759 -0
- data/keitai/rack/ketai/carrier/emoji/emojidata.rb +836 -0
- data/keitai/rack/ketai/carrier/emoji/softbankutf8strtoemojiid.rb +1119 -0
- data/keitai/rack/ketai/carrier/emoji/softbankwebcodetoutf8str.rb +499 -0
- data/keitai/rack/ketai/carrier/iphone.rb +8 -0
- data/keitai/rack/ketai/carrier/softbank.rb +82 -0
- data/keitai/rack/ketai/carrier.rb +17 -0
- data/keitai/rack/ketai/middleware.rb +24 -0
- data/m17n.rb +193 -0
- data/rack/auth/abstract/handler.rb +37 -0
- data/rack/auth/abstract/request.rb +37 -0
- data/rack/auth/basic.rb +58 -0
- data/rack/auth/digest/md5.rb +124 -0
- data/rack/auth/digest/nonce.rb +51 -0
- data/rack/auth/digest/params.rb +55 -0
- data/rack/auth/digest/request.rb +40 -0
- data/rack/builder.rb +80 -0
- data/rack/cascade.rb +41 -0
- data/rack/chunked.rb +49 -0
- data/rack/commonlogger.rb +49 -0
- data/rack/conditionalget.rb +47 -0
- data/rack/config.rb +15 -0
- data/rack/content_length.rb +29 -0
- data/rack/content_type.rb +23 -0
- data/rack/deflater.rb +96 -0
- data/rack/directory.rb +157 -0
- data/rack/etag.rb +32 -0
- data/rack/file.rb +92 -0
- data/rack/handler/cgi.rb +62 -0
- data/rack/handler/evented_mongrel.rb +8 -0
- data/rack/handler/fastcgi.rb +89 -0
- data/rack/handler/lsws.rb +63 -0
- data/rack/handler/mongrel.rb +90 -0
- data/rack/handler/scgi.rb +59 -0
- data/rack/handler/swiftiplied_mongrel.rb +8 -0
- data/rack/handler/thin.rb +18 -0
- data/rack/handler/webrick.rb +73 -0
- data/rack/handler.rb +88 -0
- data/rack/head.rb +19 -0
- data/rack/lint.rb +567 -0
- data/rack/lobster.rb +65 -0
- data/rack/lock.rb +16 -0
- data/rack/logger.rb +20 -0
- data/rack/methodoverride.rb +27 -0
- data/rack/mime.rb +208 -0
- data/rack/mock.rb +190 -0
- data/rack/nulllogger.rb +18 -0
- data/rack/recursive.rb +61 -0
- data/rack/reloader.rb +109 -0
- data/rack/request.rb +273 -0
- data/rack/response.rb +150 -0
- data/rack/rewindable_input.rb +103 -0
- data/rack/runtime.rb +27 -0
- data/rack/sendfile.rb +144 -0
- data/rack/server.rb +271 -0
- data/rack/session/abstract/id.rb +140 -0
- data/rack/session/cookie.rb +90 -0
- data/rack/session/memcache.rb +119 -0
- data/rack/session/pool.rb +100 -0
- data/rack/showexceptions.rb +349 -0
- data/rack/showstatus.rb +106 -0
- data/rack/static.rb +38 -0
- data/rack/urlmap.rb +55 -0
- data/rack/utils.rb +662 -0
- data/rack.rb +81 -0
- data/route.rb +231 -0
- data/sendmail.rb +222 -0
- data/sequel_helper.rb +20 -0
- data/session.rb +132 -0
- data/stringify_hash.rb +63 -0
- data/support.rb +35 -0
- data/template.rb +287 -0
- data/test/french.html +13 -0
- data/test/french_msg.html +3 -0
- data/test/m17n.txt +30 -0
- data/test/mobile.html +15 -0
- data/test/setup.rb +8 -0
- data/test/static/test.txt +1 -0
- data/test/template.html +58 -0
- data/test/template_inner.html +1 -0
- data/test/template_innerparam.html +1 -0
- data/test/test_auth.rb +43 -0
- data/test/test_blank.rb +44 -0
- data/test/test_csrf.rb +87 -0
- data/test/test_errorconsole.rb +91 -0
- data/test/test_handler.rb +155 -0
- data/test/test_helper.rb +296 -0
- data/test/test_keitai.rb +107 -0
- data/test/test_m17n.rb +129 -0
- data/test/test_route.rb +192 -0
- data/test/test_sendmail.rb +146 -0
- data/test/test_session.rb +83 -0
- data/test/test_stringify_hash.rb +67 -0
- data/test/test_template.rb +114 -0
- data/test.bat +2 -0
- metadata +240 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'sequel'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'egalite'
|
6
|
+
require 'helper'
|
7
|
+
require 'auth/basic'
|
8
|
+
require 'errorconsole'
|
9
|
+
|
10
|
+
require 'rack/test'
|
11
|
+
require 'setup'
|
12
|
+
|
13
|
+
class T_ErrorConsole < Test::Unit::TestCase
|
14
|
+
include Rack::Test::Methods
|
15
|
+
|
16
|
+
def app
|
17
|
+
Egalite::Handler.new
|
18
|
+
end
|
19
|
+
def setup
|
20
|
+
db = Sequel.sqlite
|
21
|
+
db.execute("
|
22
|
+
CREATE TABLE logs (
|
23
|
+
id SERIAL PRIMARY KEY,
|
24
|
+
severity TEXT NOT NULL,
|
25
|
+
|
26
|
+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
27
|
+
checked_at TIMESTAMP,
|
28
|
+
ipaddress INET,
|
29
|
+
text TEXT,
|
30
|
+
url TEXT,
|
31
|
+
md5 TEXT
|
32
|
+
);
|
33
|
+
")
|
34
|
+
db[:logs] << {:id => 1, :severity=>'exception', :ipaddress=>'127.0.0.1', :text=>'hogehoge', :md5=>'1234', :url=>'/hoge'}
|
35
|
+
db[:logs] << {:id => 2, :severity=>'exception', :ipaddress=>'127.0.0.1', :text=>'hogehoge', :md5=>'1234', :url=>'/hoge'}
|
36
|
+
db[:logs] << {:id => 3, :created_at => Time.now + 60, :severity=>'exception', :ipaddress=>'127.0.0.1', :text=>'piyopiyo', :md5=>'5678', :url=>'/hoge'}
|
37
|
+
db[:logs] << {:id => 4, :severity=>'security', :ipaddress=>'127.0.0.1', :text=>'foobar', :md5=>'1111', :url=>'/hoge'}
|
38
|
+
|
39
|
+
EgaliteErrorController.database=db
|
40
|
+
EgaliteErrorController.password='9999'
|
41
|
+
end
|
42
|
+
def test_latest
|
43
|
+
basic_authorize('admin','9999')
|
44
|
+
get "/egalite/error/latest/1"
|
45
|
+
assert_match %r|5678</a></td><td>1</td>|, last_response.body
|
46
|
+
assert_no_match %r|1234</a></td><td>2</td>|, last_response.body
|
47
|
+
assert_no_match %r|1111</a></td><td>1</td>|, last_response.body
|
48
|
+
end
|
49
|
+
def test_frequent
|
50
|
+
basic_authorize('admin','9999')
|
51
|
+
get "/egalite/error/frequent/1"
|
52
|
+
assert_match %r|1234</a></td><td>2</td>|, last_response.body
|
53
|
+
assert_no_match %r|1111</a></td><td>1</td>|, last_response.body
|
54
|
+
assert_no_match %r|5678</a></td><td>1</td>|, last_response.body
|
55
|
+
end
|
56
|
+
def test_security
|
57
|
+
basic_authorize('admin','9999')
|
58
|
+
get "/egalite/error/security"
|
59
|
+
assert_no_match %r|5678</a></td><td>1</td>|, last_response.body
|
60
|
+
assert_no_match %r|1234</a></td><td>2</td>|, last_response.body
|
61
|
+
assert_match %r|1111</a></td><td>1</td>|, last_response.body
|
62
|
+
end
|
63
|
+
def test_group
|
64
|
+
basic_authorize('admin','9999')
|
65
|
+
get "/egalite/error/group/1234"
|
66
|
+
assert_match %r|<li>/hoge</li>\n*<li>hogehoge</li>|, last_response.body
|
67
|
+
end
|
68
|
+
def test_detail
|
69
|
+
basic_authorize('admin','9999')
|
70
|
+
get "/egalite/error/detail/1"
|
71
|
+
assert_not_equal "no record found.", last_response.body
|
72
|
+
assert_match %r|<li>127.0.0.1</li>\n*<li>/hoge</li>\n*<li>hogehoge</li>|, last_response.body
|
73
|
+
|
74
|
+
get "/egalite/error/detail/100"
|
75
|
+
assert_equal "no record found.", last_response.body
|
76
|
+
end
|
77
|
+
def test_delete
|
78
|
+
basic_authorize('admin','9999')
|
79
|
+
get "/egalite/error/latest"
|
80
|
+
assert_match %r|5678</a></td><td>1</td>|, last_response.body
|
81
|
+
assert_match %r|1234</a></td><td>2</td>|, last_response.body
|
82
|
+
|
83
|
+
get "/egalite/error/delete/1234"
|
84
|
+
assert last_response.redirect?
|
85
|
+
assert_equal "/egalite/error", last_response.headers['location']
|
86
|
+
|
87
|
+
get "/egalite/error/latest"
|
88
|
+
assert_match %r|5678</a></td><td>1</td>|, last_response.body
|
89
|
+
assert_no_match %r|1234</a></td><td>2</td>|, last_response.body
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
$LOAD_PATH << File.dirname(__FILE__)
|
2
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..')
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'egalite'
|
7
|
+
|
8
|
+
require 'rack'
|
9
|
+
require 'rack/multipart'
|
10
|
+
require 'rack/test'
|
11
|
+
|
12
|
+
require 'setup'
|
13
|
+
|
14
|
+
class TestController < Egalite::Controller
|
15
|
+
def parameters
|
16
|
+
raise "param foo is not bar: #{params[:foo].inspect}" unless params[:foo] == 'bar'
|
17
|
+
raise "param hash isnt okay: #{params[:hash].inspect}" unless params[:hash][:a] == '1' and params[:hash][:b] == '2'
|
18
|
+
"okay"
|
19
|
+
end
|
20
|
+
def exception
|
21
|
+
raise
|
22
|
+
end
|
23
|
+
def notfoundtest
|
24
|
+
notfound
|
25
|
+
end
|
26
|
+
def delegatetest
|
27
|
+
delegate(:action => :test)
|
28
|
+
end
|
29
|
+
def test
|
30
|
+
"delegated"
|
31
|
+
end
|
32
|
+
def niltest
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
def ipaddr
|
36
|
+
req.ipaddr.to_s
|
37
|
+
end
|
38
|
+
def accesslog
|
39
|
+
@log_values = [1,2,3]
|
40
|
+
"accesslog"
|
41
|
+
end
|
42
|
+
private
|
43
|
+
def priv
|
44
|
+
"private!"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
class BeforefilterController < TestController
|
48
|
+
def before_filter
|
49
|
+
case params[:test]
|
50
|
+
when /notfound/: notfound
|
51
|
+
when /delegate/: delegate(:controller => :test,:action => :test)
|
52
|
+
when /forbidden/: false
|
53
|
+
else redirect(:action => :test)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
class T_Handler < Test::Unit::TestCase
|
60
|
+
include Rack::Test::Methods
|
61
|
+
|
62
|
+
def app
|
63
|
+
Egalite::Handler.new
|
64
|
+
end
|
65
|
+
def test_exception
|
66
|
+
$raise_exception = false
|
67
|
+
get "/test/exception"
|
68
|
+
assert last_response.server_error?
|
69
|
+
assert last_response.body =~ /Exception/
|
70
|
+
ensure
|
71
|
+
$raise_exception = true
|
72
|
+
end
|
73
|
+
def test_private
|
74
|
+
get "/priv"
|
75
|
+
assert_no_match /private!/, last_response.body
|
76
|
+
end
|
77
|
+
def test_parameters
|
78
|
+
post("/test/parameters", {'foo' => 'bar', 'hash[a]' => '1', 'hash[b]' => '2'})
|
79
|
+
assert last_response.ok?
|
80
|
+
assert last_response.body =~ /okay/
|
81
|
+
assert last_response.content_type =~ /text\/html/i
|
82
|
+
end
|
83
|
+
def test_notfound
|
84
|
+
get "/test/notfoundtest"
|
85
|
+
assert last_response.not_found?
|
86
|
+
end
|
87
|
+
def test_delegate
|
88
|
+
get "/test/delegatetest"
|
89
|
+
assert last_response.ok?
|
90
|
+
assert last_response.body =~ /delegated/
|
91
|
+
end
|
92
|
+
def test_nil
|
93
|
+
$raise_exception = false
|
94
|
+
get "/test/niltest"
|
95
|
+
assert last_response.server_error?
|
96
|
+
assert last_response.body =~ /Exception/
|
97
|
+
assert last_response.body =~ /nil/
|
98
|
+
ensure
|
99
|
+
$raise_exception = true
|
100
|
+
end
|
101
|
+
def test_beforefilter
|
102
|
+
get "/beforefilter/niltest"
|
103
|
+
assert last_response.redirect?
|
104
|
+
assert last_response.headers['location'] == "/beforefilter/test"
|
105
|
+
get "/beforefilter/niltest?test=delegate"
|
106
|
+
assert last_response.ok?
|
107
|
+
assert last_response.body =~ /delegated/
|
108
|
+
get "/beforefilter/niltest?test=notfound"
|
109
|
+
assert last_response.not_found?
|
110
|
+
get "/beforefilter/niltest?test=forbidden"
|
111
|
+
assert last_response.forbidden?
|
112
|
+
end
|
113
|
+
def test_ipaddr
|
114
|
+
get "/test/ipaddr"
|
115
|
+
assert last_response.ok?
|
116
|
+
assert last_response.body =~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/
|
117
|
+
end
|
118
|
+
def test_accesslog
|
119
|
+
io = StringIO.new
|
120
|
+
Egalite::AccessLogger.io = io
|
121
|
+
get("http://localhost/test/accesslog",nil, {'HTTP_REFERER' => 'http://yahoo.com'})
|
122
|
+
assert last_response.ok?
|
123
|
+
|
124
|
+
io.rewind
|
125
|
+
s = io.read
|
126
|
+
a = s.chomp.split(/\t/)
|
127
|
+
assert_match /\A[0-9-]+T[0-9:]+\+[0-9:]+\z/, a[0]
|
128
|
+
assert_equal "127.0.0.1", a[1]
|
129
|
+
assert_match /\A[0-9]+\.[0-9]+\z/, a[2]
|
130
|
+
assert_equal "http://localhost/test/accesslog", a[3]
|
131
|
+
assert_equal "http://yahoo.com", a[4]
|
132
|
+
assert_equal "1", a[5]
|
133
|
+
assert_equal "2", a[6]
|
134
|
+
assert_equal "3", a[7]
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class T_StaticController < Test::Unit::TestCase
|
139
|
+
include Rack::Test::Methods
|
140
|
+
|
141
|
+
def app
|
142
|
+
Egalite::Handler.new(:static_root=>'test/static/')
|
143
|
+
end
|
144
|
+
def test_default_controller
|
145
|
+
get "/static/foo"
|
146
|
+
assert last_response.ok? == false
|
147
|
+
assert last_response.not_found?
|
148
|
+
get "/static/../../hoge/hoge/"
|
149
|
+
assert last_response.ok? == false
|
150
|
+
assert last_response.forbidden?
|
151
|
+
get "/static/test.txt"
|
152
|
+
assert last_response.ok?
|
153
|
+
assert last_response.body =~ /piyo/
|
154
|
+
end
|
155
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,296 @@
|
|
1
|
+
$LOAD_PATH << File.dirname(__FILE__)
|
2
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..')
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'egalite'
|
7
|
+
require 'rexml/xpath'
|
8
|
+
require 'rexml/document'
|
9
|
+
|
10
|
+
module XPathTestingHelper
|
11
|
+
|
12
|
+
def to_doc(text)
|
13
|
+
REXML::Document.new(StringIO.new(text))
|
14
|
+
end
|
15
|
+
|
16
|
+
def assert_xpath_match_count(exp, n, doc)
|
17
|
+
assert_equal(n, REXML::XPath::match(doc, exp).size)
|
18
|
+
end
|
19
|
+
|
20
|
+
def X(doc, exp)
|
21
|
+
doc = to_doc(doc) unless doc.kind_of?(REXML::Document)
|
22
|
+
REXML::XPath::match(doc, exp)
|
23
|
+
end
|
24
|
+
|
25
|
+
def X1(doc, exp)
|
26
|
+
doc = to_doc(doc) unless doc.kind_of?(REXML::Document)
|
27
|
+
REXML::XPath::first(doc, exp)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class T_TableHelper < Test::Unit::TestCase
|
33
|
+
include Egalite
|
34
|
+
include XPathTestingHelper
|
35
|
+
|
36
|
+
def make_hash_content(n=1)
|
37
|
+
[{
|
38
|
+
:foo => "Foo",
|
39
|
+
:bar => "Bar",
|
40
|
+
:baz => "Baz",
|
41
|
+
:ika => "Ika",
|
42
|
+
}]*n
|
43
|
+
end
|
44
|
+
|
45
|
+
def make_array_content(n=1)
|
46
|
+
[["Foo", "Bar", "Baz"]]*n
|
47
|
+
end
|
48
|
+
|
49
|
+
def make_header
|
50
|
+
["header1", "header2", "header3"]
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_table_by_hash_hello
|
54
|
+
table = to_doc(TableHelper.table_by_hash([:foo, :bar, :baz],
|
55
|
+
make_header,
|
56
|
+
make_hash_content))
|
57
|
+
assert_xpath_match_count("/table", 1, table)
|
58
|
+
assert_xpath_match_count("/table/tr", 2, table)
|
59
|
+
assert_xpath_match_count("/table/tr/th", 3, table)
|
60
|
+
assert_xpath_match_count("/table/tr/td", 3, table)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_table_by_hash_table_opts_should_appear_as_attribute
|
64
|
+
table = to_doc(TableHelper.table_by_hash([], [], [],
|
65
|
+
{"foo_attr"=>"foo_value"}))
|
66
|
+
t = X1(table, "/table")
|
67
|
+
assert_equal("foo_value", t.attributes["foo_attr"])
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_table_by_hash_should_raise_if_header_content_mismatch
|
71
|
+
assert_raise(ArgumentError) do
|
72
|
+
TableHelper.table_by_hash([:foo, :bar],
|
73
|
+
make_header,
|
74
|
+
make_hash_content)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_table_by_hash_should_handle_multiple_rows
|
79
|
+
count = 3
|
80
|
+
table = to_doc(TableHelper.table_by_hash([:foo, :bar, :baz],
|
81
|
+
make_header,
|
82
|
+
make_hash_content(count)))
|
83
|
+
assert_xpath_match_count("/table/tr", 1 + count, table) # + 1 for header <tr>
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_table_by_hash_should_show_empty_cell_for_nonexist_key
|
87
|
+
table = to_doc(TableHelper.table_by_hash([:foo, :bar, :notexist],
|
88
|
+
make_header,
|
89
|
+
make_hash_content))
|
90
|
+
assert_nil(REXML::XPath::match(table, "/table/tr/td").map{ |i| i.text }[2])
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_table_by_array_hello
|
94
|
+
table = to_doc(TableHelper.table_by_array(make_header,
|
95
|
+
make_array_content))
|
96
|
+
assert_xpath_match_count("/table/tr", 2, table)
|
97
|
+
assert_xpath_match_count("/table/tr/th", 3, table)
|
98
|
+
assert_xpath_match_count("/table/tr/td", 3, table)
|
99
|
+
|
100
|
+
assert_equal(2, X(table, "/table/tr").size)
|
101
|
+
assert_equal(3, X(table, "/table/tr/th").size)
|
102
|
+
assert_equal(3, X(table, "/table/tr/td").size)
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
class T_FormHelper < Test::Unit::TestCase
|
108
|
+
include Egalite
|
109
|
+
include XPathTestingHelper
|
110
|
+
|
111
|
+
def test_should_instantiatable
|
112
|
+
FormHelper.new({})
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_form_hello
|
116
|
+
action = "/action"
|
117
|
+
f = FormHelper.new({:foo => "Foo"})
|
118
|
+
i = X(f.form(:GET, action) + f.close, "/form")
|
119
|
+
assert_equal(i.size, 1)
|
120
|
+
assert_equal("GET", i[0].attributes["method"])
|
121
|
+
assert_equal(action, i[0].attributes["action"])
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_form_should_allow_nil_action
|
125
|
+
f = FormHelper.new({:foo => "Foo"})
|
126
|
+
i = X1(f.form(:GET) + f.close, "/form")
|
127
|
+
assert_equal(nil, i.attributes["action"])
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_text_should_result_text_input
|
131
|
+
f = FormHelper.new({:foo => "Foo"})
|
132
|
+
i = X1(f.text(:foo), "/input")
|
133
|
+
assert_equal("Foo", i.attributes["value"])
|
134
|
+
assert_equal("text", i.attributes["type"])
|
135
|
+
assert_equal("foo", i.attributes["name"])
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_text_should_use_default_if_no_data
|
139
|
+
f = FormHelper.new({:foo => "Foo"})
|
140
|
+
i = X1(f.text(:bar, {:default => "Default"}), "/input")
|
141
|
+
assert_equal("Default", i.attributes["value"])
|
142
|
+
assert_equal("text",i.attributes["type"])
|
143
|
+
assert_equal("bar", i.attributes["name"])
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_text_timestamp_should_be_ok
|
147
|
+
input = "2009/01/02 12:30:45"
|
148
|
+
expected = "2009-01-02 12:30:45"
|
149
|
+
f = FormHelper.new({:foo => Time.parse(input)})
|
150
|
+
i = X1(f.timestamp_text(:foo), "/input")
|
151
|
+
assert_equal(expected, i.attributes["value"])
|
152
|
+
assert_equal("text", i.attributes["type"])
|
153
|
+
assert_equal("foo", i.attributes["name"])
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_hidden_should_be_ok
|
157
|
+
f = FormHelper.new({:foo => "Foo"})
|
158
|
+
i = X1(f.hidden(:foo), "/input")
|
159
|
+
assert_equal("Foo", i.attributes["value"])
|
160
|
+
assert_equal("hidden", i.attributes["type"])
|
161
|
+
assert_equal("foo", i.attributes["name"])
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_password_should_be_ok
|
165
|
+
f = FormHelper.new({:foo => "Foo"})
|
166
|
+
i = X1(f.password(:foo), "/input")
|
167
|
+
assert_equal("Foo", i.attributes["value"])
|
168
|
+
assert_equal("password", i.attributes["type"])
|
169
|
+
assert_equal("foo", i.attributes["name"])
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_checkbox_should_be_checked_if_data_given
|
173
|
+
f = FormHelper.new({:foo => "OK"})
|
174
|
+
i = X1(f.checkbox(:foo, "Bar", :nohidden => true), "/input")
|
175
|
+
assert_equal("Bar", i.attributes["value"])
|
176
|
+
assert_equal("checked", i.attributes["checked"])
|
177
|
+
assert_equal("foo", i.attributes["name"])
|
178
|
+
assert_equal("checkbox", i.attributes["type"])
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_checkbox_should_NOT_be_checked_unless_something_given
|
182
|
+
f = FormHelper.new({:foo => "OK"})
|
183
|
+
i = X1(f.checkbox(:bar, "Bar", :nohidden => true), "/input")
|
184
|
+
assert_equal(nil, i.attributes["checked"])
|
185
|
+
end
|
186
|
+
|
187
|
+
def _test_checkbox_should_be_checked_if_opt_default_given(option_name)
|
188
|
+
f = FormHelper.new({})
|
189
|
+
i = X1(f.checkbox(:foo, "Bar", {option_name => 'OK', :nohidden => true}), "/input")
|
190
|
+
assert_equal("checked", i.attributes["checked"])
|
191
|
+
assert_equal("foo", i.attributes["name"])
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_checkbox_should_be_checked_if_opt_default_given
|
195
|
+
_test_checkbox_should_be_checked_if_opt_default_given(:default)
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_checkbox_should_be_checked_if_opt_checked_given
|
199
|
+
_test_checkbox_should_be_checked_if_opt_default_given(:checked)
|
200
|
+
end
|
201
|
+
|
202
|
+
def test_radio_should_be_ok
|
203
|
+
f = FormHelper.new({:foo => "Foo"})
|
204
|
+
i = X1(f.radio(:foo, "Foo"), "/input")
|
205
|
+
assert_equal("Foo", i.attributes["value"])
|
206
|
+
assert_equal("radio", i.attributes["type"])
|
207
|
+
assert_equal("foo", i.attributes["name"])
|
208
|
+
assert_equal("selected", i.attributes["selected"])
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_radio_should_not_be_checked_if_choice_mismatch
|
212
|
+
f = FormHelper.new({:bar => "Foo"})
|
213
|
+
i = X1(f.radio(:foo, "Foo"), "/input")
|
214
|
+
assert_equal("Foo", i.attributes["value"])
|
215
|
+
assert_equal(nil, i.attributes["selected"])
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_textarea_should_be_ok
|
219
|
+
f = FormHelper.new({:foo => "Foo"})
|
220
|
+
t = X(f.textarea(:foo), "/textarea")
|
221
|
+
assert_equal(1, t.size)
|
222
|
+
assert_equal("Foo", t[0].text)
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_file_should_be_ok
|
226
|
+
f = FormHelper.new({:foo => "Foo"})
|
227
|
+
i = X1(f.file(:foo), "/input")
|
228
|
+
assert_equal("file", i.attributes["type"])
|
229
|
+
assert_equal("foo", i.attributes["name"])
|
230
|
+
end
|
231
|
+
|
232
|
+
def test_submit_should_be_ok
|
233
|
+
f = FormHelper.new()
|
234
|
+
i = X1(f.submit("foovalue", "fooname"), "/input")
|
235
|
+
assert_equal("foovalue", i.attributes["value"])
|
236
|
+
assert_equal("submit", i.attributes["type"])
|
237
|
+
assert_equal("fooname", i.attributes["name"])
|
238
|
+
end
|
239
|
+
|
240
|
+
def make_select_options
|
241
|
+
[{:optname => "optvalue0", :id=> "id0"},
|
242
|
+
{:optname => "optvalue1", :id=> "id1"}]
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_select_by_association_hello
|
246
|
+
f = FormHelper.new()
|
247
|
+
d = to_doc(f.select_by_association("Name", make_select_options, :optname))
|
248
|
+
assert_equal(1, X(d, "/select").size)
|
249
|
+
assert_equal(2, X(d, "/select/option").size)
|
250
|
+
o0 = X(d, "/select/option")[0]
|
251
|
+
assert_equal("optvalue0", o0.text)
|
252
|
+
assert_equal("id0", o0.attributes["value"])
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_select_by_association_should_add_option_if_nil_opt
|
256
|
+
options = make_select_options
|
257
|
+
niltext = "NILTEXT"
|
258
|
+
f = FormHelper.new()
|
259
|
+
d = to_doc(f.select_by_association("Name", options, :optname, {:nil => niltext}))
|
260
|
+
assert_equal(1, X(d, "/select").size)
|
261
|
+
assert_equal(options.size + 1, X(d, "/select/option").size)
|
262
|
+
last = X1(d, "/select/option")
|
263
|
+
assert_equal("", last.attributes["value"])
|
264
|
+
assert_equal(niltext, last.text)
|
265
|
+
end
|
266
|
+
|
267
|
+
def make_opt_div(opts)
|
268
|
+
f = FormHelper.new()
|
269
|
+
"<div #{f.opt(opts)} />"
|
270
|
+
end
|
271
|
+
|
272
|
+
def test_opt_hello
|
273
|
+
d = X1(to_doc(make_opt_div({:foo => "Foo", :bar => "Bar"})), "/div")
|
274
|
+
assert_equal("Foo", d.attributes["foo"])
|
275
|
+
assert_equal("Bar", d.attributes["bar"])
|
276
|
+
end
|
277
|
+
|
278
|
+
def _test_opt_should_skip_special_keys(key)
|
279
|
+
opts = {:foo => "Foo"}
|
280
|
+
opts[key] = "Val"
|
281
|
+
d = X1(to_doc(make_opt_div(opts)), "/div")
|
282
|
+
assert_equal("Foo", d.attributes["foo"])
|
283
|
+
assert_equal(nil, d.attributes[key.to_s])
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_opt_should_skip_special_keys
|
287
|
+
_test_opt_should_skip_special_keys(:default)
|
288
|
+
_test_opt_should_skip_special_keys(:checked)
|
289
|
+
_test_opt_should_skip_special_keys(:selected)
|
290
|
+
_test_opt_should_skip_special_keys(:nil)
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
294
|
+
|
295
|
+
# XXX: test form
|
296
|
+
# XXX: test expand_name
|
data/test/test_keitai.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
#!ruby -Ku
|
2
|
+
|
3
|
+
$KCODE = 'UTF8'
|
4
|
+
|
5
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), '..')
|
6
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__))
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'test/unit'
|
10
|
+
require 'egalite'
|
11
|
+
require 'keitai/keitai'
|
12
|
+
|
13
|
+
require 'rack'
|
14
|
+
require 'rack/multipart'
|
15
|
+
require 'rack/test'
|
16
|
+
|
17
|
+
require 'kconv'
|
18
|
+
|
19
|
+
require 'setup'
|
20
|
+
|
21
|
+
class KeitaiController < Egalite::Keitai::Controller
|
22
|
+
def post(id)
|
23
|
+
return "false" unless params[:foo][:bar] == "あいうえお" # utf8
|
24
|
+
"foobar:#{params[:foo][:bar]}"
|
25
|
+
end
|
26
|
+
def login(id)
|
27
|
+
session.create(:user_id => id)
|
28
|
+
redirect_to :controller => :mobile
|
29
|
+
end
|
30
|
+
end
|
31
|
+
class MobileController < Egalite::Keitai::Controller
|
32
|
+
def before_filter
|
33
|
+
super
|
34
|
+
|
35
|
+
return redirect_to :controller => :keitai, :action => :login unless session[:user_id].to_i > 0
|
36
|
+
true
|
37
|
+
end
|
38
|
+
def get
|
39
|
+
{}
|
40
|
+
end
|
41
|
+
def get_userid
|
42
|
+
session[:user_id].to_s
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class RedirectorController < Egalite::Keitai::Redirector
|
47
|
+
end
|
48
|
+
|
49
|
+
class T_Keitai < Test::Unit::TestCase
|
50
|
+
include Rack::Test::Methods
|
51
|
+
|
52
|
+
def app
|
53
|
+
db = Sequel.sqlite
|
54
|
+
Egalite::SessionSequel.create_table(db)
|
55
|
+
db.alter_table :sessions do
|
56
|
+
add_column :user_id, :integer
|
57
|
+
end
|
58
|
+
|
59
|
+
Rack::Builder.new {
|
60
|
+
use Rack::Ketai
|
61
|
+
run Egalite::Handler.new(
|
62
|
+
:db => db,
|
63
|
+
:session_handler => Egalite::SessionSequel,
|
64
|
+
:template_path => File.dirname(__FILE__)
|
65
|
+
)
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_ctler_should_see_utf8_for_sjis_query_parameter
|
70
|
+
s = "あいうえお".tosjis
|
71
|
+
post("/keitai", {"foo[bar]" => s}, {'HTTP_USER_AGENT' => 'DoCoMo/2.0 P903i'})
|
72
|
+
assert last_response.ok?
|
73
|
+
assert_equal("foobar:#{s}", last_response.body)
|
74
|
+
end
|
75
|
+
def test_session
|
76
|
+
get("/keitai/login/1234")
|
77
|
+
assert last_response.redirect?
|
78
|
+
assert_match(%r|/mobile\?sessionid=[0-9]+_[0-9a-f]+|,last_response.location)
|
79
|
+
location = last_response.location
|
80
|
+
|
81
|
+
clear_cookies
|
82
|
+
|
83
|
+
get(location)
|
84
|
+
assert last_response.ok?
|
85
|
+
assert_match(%r|<a\s+href='mailto:arai\@example\.com'\s*>mail</a>|,last_response.body)
|
86
|
+
assert_match(%r|<a\s+href='tel:03-1234-5678'\s*>tel</a>|,last_response.body)
|
87
|
+
assert_match(%r|<a\s+href='(/mobile/get_userid\?sessionid=[0-9]+_[0-9a-f]+)'\s*>hoge</a>|,last_response.body)
|
88
|
+
%r|<a\s+href='(/mobile/get_userid\?sessionid=[0-9]+_[0-9a-f]+)'\s*>hoge</a>| =~ last_response.body
|
89
|
+
get_userid = $1
|
90
|
+
assert_match(%r|<a\s+href='(/redirector/[0-9a-zA-Z._-]+)'\s*>yahoo</a>|,last_response.body)
|
91
|
+
%r|<a\s+href='(/redirector/[0-9a-zA-Z._-]+)'\s*>yahoo</a>| =~ last_response.body
|
92
|
+
redirector = $1
|
93
|
+
assert_match(%r|<input.+?value='[0-9]+_[0-9a-f]+'.+?>|, last_response.body)
|
94
|
+
|
95
|
+
clear_cookies
|
96
|
+
|
97
|
+
get(get_userid)
|
98
|
+
assert last_response.ok?
|
99
|
+
assert_equal("1234", last_response.body)
|
100
|
+
|
101
|
+
clear_cookies
|
102
|
+
|
103
|
+
get(redirector)
|
104
|
+
assert last_response.ok?
|
105
|
+
assert_match(%r|http://www.yahoo.com|, last_response.body)
|
106
|
+
end
|
107
|
+
end
|