egalite 1.5.2 → 1.5.6

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db514bb6d9faeb6cfabbca789a9a763836dc3632
4
+ data.tar.gz: 38c947f91c01a9af375e5483c82ef85692b08c13
5
+ SHA512:
6
+ metadata.gz: 2f17b9cbf57d51b09832e288b388a1344cc0a51ed48fa6c614922da74142df8baf03b9dd431049553021bbb0552b8de30d8375a3143830849cdffd753cb581da
7
+ data.tar.gz: 8b7099b961b735179af768fe91a6612fa3fdbb0960c3638f1cdd7e19810c66373c0b2e20cdaafd6722cd52e1229a9c18f2f6950e8b7d723bbbf8d81992e040db
data/README.md CHANGED
@@ -190,3 +190,9 @@ UserErrorとSystemError例外が追加されました。これによりユーザ
190
190
  ### 1.5.1
191
191
 
192
192
  Egalite::ErrorLogger.catch_exception(sendmail) {}が追加されました。これを使うと、任意のブロックで発生した例外をキャッチして、管理者へのメール送信やログの出力を行います。重要なエラーが発生する可能性がある場所への利用に適しています。
193
+
194
+ ## 1.5.6
195
+
196
+ Sendmailに添付ファイルを送信するためのメソッド二つが追加されました。send_with_uploaded_filesとsend_multipartです。send_with_uploaded_filesは、HTMLフォームで送信されたファイルを指定するだけでそのまま送信することができます。
197
+
198
+ testがRuby 2.1で動くようになり、Ruby 1.8.7では動かなくなりました。
data/lib/egalite/m17n.rb CHANGED
@@ -136,9 +136,12 @@ module Egalite
136
136
  h2 = {}
137
137
  h.each { |k,v|
138
138
  h2[k] = case v
139
- when String: t_string(list,v)
140
- when Array: v.map { |x| t_hash(list,x) }
141
- when Hash: t_hash(list, v)
139
+ when String
140
+ t_string(list,v)
141
+ when Array
142
+ v.map { |x| t_hash(list,x) }
143
+ when Hash
144
+ t_hash(list, v)
142
145
  else v
143
146
  end
144
147
  }
@@ -186,7 +189,7 @@ module Egalite
186
189
  end
187
190
  end
188
191
  string = string.dup
189
- placeholders.each_with_index { |s2,i| string.gsub!(/\{#{i}\}/, s2) }
192
+ placeholders.split(/\n/).each_with_index { |s2,i| string.gsub!(/\{#{i}\}/, s2) }
190
193
  string
191
194
  end
192
195
  end
@@ -27,6 +27,8 @@ require 'resolv'
27
27
  # 4. hash: { "Hoge Taro" => "hoge@example.com" }
28
28
 
29
29
  module Sendmail
30
+ class AttachmentsTooLarge < RuntimeError
31
+ end
30
32
  class QualifiedMailbox < String
31
33
  end
32
34
  @force_dkim = false
@@ -166,15 +168,20 @@ module Sendmail
166
168
  headers["MIME-Version"] = "1.0"
167
169
  date = params[:date] || Time.now
168
170
  headers["Date"] = date.is_a?(Time) ? date.rfc822 : date
169
- headers["Content-Type"] = "text/plain; charset=UTF-8"
171
+ headers["Content-Type"] = params[:content_type]
172
+ headers["Content-Type"] ||= "text/plain; charset=UTF-8"
170
173
 
171
- if multibyte?(body)
174
+ if params[:content_type] =~ /multipart/
175
+ body = body
176
+ elsif multibyte?(body)
172
177
  headers["Content-Transfer-Encoding"] = "base64"
173
178
  body = [body].pack('m')
174
179
  else
175
180
  headers["Content-Transfer-Encoding"] = "7bit"
176
181
  end
177
-
182
+
183
+ params[:headers].each_pair { |k,v| headers[k] = v unless headers.key?(k)} if params.key?(:headers)
184
+
178
185
  text = [headers.map{|k,v| "#{k}: #{v}"}.join("\n"),body].join("\n\n")
179
186
  end
180
187
  private
@@ -224,9 +231,11 @@ module Sendmail
224
231
  if dkim
225
232
  text = Dkim.sign(text,dkim_params)
226
233
  end
234
+ envelope_from = _extract_addrspec(params[:envelope_from] || params[:sender] || params[:from])
235
+ envelope_from = envelope_from[0] if envelope_from.is_a?(Array)
227
236
  _send(
228
237
  text,
229
- _extract_addrspec(params[:envelope_from] || params[:sender] || params[:from]),
238
+ envelope_from,
230
239
  to_addresses(params),
231
240
  host
232
241
  )
@@ -237,6 +246,23 @@ module Sendmail
237
246
  def send_with_dkim(body, params, host = 'localhost', dkim_params = {})
238
247
  send_inner_2(body, params, host, true, dkim_params)
239
248
  end
249
+ def send_multipart(parts, params, host = 'localhost')
250
+ (body, content_type) = mime_combine(parts)
251
+ params[:content_type] = content_type
252
+ send_inner_2(body, params, host, @force_dkim, {})
253
+ end
254
+ def send_with_uploaded_files(body, files, params, host = 'localhost')
255
+ # files should be Rack uploaded files.
256
+ sum_size = 0
257
+ parts = [mime_part(body)]
258
+ parts += files.map { |f|
259
+ binary = f[:tempfile].read
260
+ sum_size += binary.size
261
+ raise AttachmentsTooLarge if sum_size >= 25 * 1000 * 1000
262
+ mime_part(binary, f[:type], f[:filename])
263
+ }
264
+ send_multipart(parts, params, host)
265
+ end
240
266
  def send_with_template(filename, params, host = 'localhost')
241
267
  File.open("mail/"+ filename ,"r") { |f|
242
268
  text = f.read
@@ -261,5 +287,50 @@ module Sendmail
261
287
  return true if aaaa
262
288
  false
263
289
  end
290
+
291
+ #
292
+ # create MIME multipart
293
+ #
294
+
295
+ def mime_combine(parts, type = "mixed")
296
+ boundary = OpenSSL::Random.random_bytes(10).unpack('h*')[0]
297
+ content_type = "multipart/#{type}; boundary=\"#{boundary}\""
298
+
299
+ body = ""
300
+ parts.each { |part|
301
+ body << "--" + boundary
302
+ body << "\n"
303
+ body << part
304
+ }
305
+ body << "--" + boundary
306
+ body << "--\n"
307
+ [body, content_type]
308
+ end
309
+ def mime_header(header,s)
310
+ if multibyte?(s)
311
+ header + '"' + multibyte_folding(header, s) + '"'
312
+ else
313
+ header + folding(header, quote_string(s))
314
+ end
315
+ end
316
+ def mime_part(body, content_type = nil, filename = nil)
317
+ content_type = "text/plain; charset=UTF-8" unless content_type
318
+ part = ""
319
+ if filename
320
+ part << "Content-Type: #{content_type};\n"
321
+ part << mime_header(" name=", filename)
322
+ part << "\n"
323
+ part << "Content-Disposition: attachment;\n"
324
+ part << mime_header(" filename=", filename)
325
+ part << "\n"
326
+ part << "Content-Transfer-Encoding: base64\n"
327
+ else
328
+ part << "Content-Type: #{content_type}\n"
329
+ part << "Content-Transfer-Encoding: base64\n"
330
+ end
331
+ part << "\n"
332
+ part << [body].pack('m')
333
+ end
334
+
264
335
  end
265
336
  end
@@ -94,7 +94,7 @@ class SessionSequel < Session
94
94
  @loaded = true
95
95
  @cookies[@cookie_name] = cookie
96
96
 
97
- @db[@table].filter(:id => sid).update(:updated_at => 'now')
97
+ @db[@table].filter(:id => sid).update(:updated_at => Time.now)
98
98
 
99
99
  true
100
100
  end
@@ -106,11 +106,13 @@ class HTMLTemplate
106
106
  (colons, noncolons) = attr_colon(attrs)
107
107
  next s if colons.empty?
108
108
  # when :hoge=$foo, expand hash parameter ['foo']
109
+ c2 = colons.dup
109
110
  colons.each { |k,v|
110
111
  next if v[0,1] != '$'
111
112
  val = params[v[1..-1]]
112
- colons[k] = val
113
+ c2[k] = val
113
114
  }
115
+ colons = c2
114
116
  colons = StringifyHash.create(colons)
115
117
  link = @controller.url_for(colons)
116
118
  "<a href='#{link}' #{noncolons}>"
@@ -160,8 +162,11 @@ class HTMLTemplate
160
162
  if block_given?
161
163
  html.gsub!(RE_INCLUDE) {
162
164
  attrs = parse_tag_attributes($1)
163
- attrs.each { |k,v| attrs[k[1..-1]] = v if k =~ /^\:/ }
164
- yield(attrs)
165
+ new_attrs = attrs.dup
166
+ attrs.each { |k,v|
167
+ new_attrs[k[1..-1]] = v if k =~ /^\:/
168
+ }
169
+ yield(new_attrs)
165
170
  }
166
171
  parent = nil
167
172
  md = RE_PARENT.match(html)
@@ -205,7 +210,11 @@ class HTMLTemplate
205
210
  public
206
211
 
207
212
  def escapeHTML(s)
208
- s.to_s.gsub(/&/n, '&amp;').gsub(/'/n,'&#039;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
213
+ if RUBY_VERSION < '1.9.0'
214
+ s.to_s.gsub(/&/n, '&amp;').gsub(/'/n,'&#039;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;')
215
+ else
216
+ s.to_s.gsub(/&/, '&amp;').gsub(/'/,'&#039;').gsub(/\"/, '&quot;').gsub(/>/, '&gt;').gsub(/</, '&lt;')
217
+ end
209
218
  end
210
219
 
211
220
  def handleTemplate(html, orig_values, parent_params={}, &block)
@@ -1,3 +1,3 @@
1
- module Egalite
2
- VERSION = "1.5.2"
3
- end
1
+ module Egalite
2
+ VERSION = "1.5.6"
3
+ end
data/lib/egalite.rb CHANGED
@@ -223,7 +223,7 @@ class Controller
223
223
  "Last-Modified" => File.mtime(path).rfc822,
224
224
  "Content-Type" => content_type || MIME_TYPES[ext] || "text/plain",
225
225
  "Content-Length" => File.size(path).to_s
226
- }, s]
226
+ }, [s]]
227
227
  else
228
228
  return [404, {"Content-Type" => "text/plain"}, ["File not found\n"]]
229
229
  end
data/run_test.rb ADDED
@@ -0,0 +1,13 @@
1
+ base_dir = File.expand_path(File.join(File.dirname(__FILE__)))
2
+ lib_dir = File.join(base_dir, "lib")
3
+ test_dir = File.join(base_dir, "test")
4
+
5
+ $LOAD_PATH.unshift(lib_dir)
6
+
7
+ require 'rubygems'
8
+ require 'test/unit'
9
+ require 'simplecov'
10
+
11
+ SimpleCov.start
12
+
13
+ exit Test::Unit::AutoRunner.run(true, test_dir)
data/test/test_handler.rb CHANGED
@@ -47,9 +47,12 @@ end
47
47
  class BeforefilterController < TestController
48
48
  def before_filter
49
49
  case params[:test]
50
- when /notfound/: notfound
51
- when /delegate/: delegate(:controller => :test,:action => :test)
52
- when /forbidden/: false
50
+ when /notfound/
51
+ notfound
52
+ when /delegate/
53
+ delegate(:controller => :test,:action => :test)
54
+ when /forbidden/
55
+ false
53
56
  else redirect(:action => :test)
54
57
  end
55
58
  end
@@ -4,8 +4,6 @@ require 'test/unit'
4
4
  require 'lib/egalite/sendmail'
5
5
  require 'dkim'
6
6
 
7
- $KCODE = 'utf8'
8
-
9
7
  class T_Sendmail < Test::Unit::TestCase
10
8
  def test_folding
11
9
  s = Sendmail.folding('To', "012345678 \n\n "*20)
@@ -56,7 +54,7 @@ class T_Sendmail < Test::Unit::TestCase
56
54
  assert e.size < 77
57
55
  assert_match /\A\s?\=\?UTF-8\?B\?(.+?)\?\=\Z/, e
58
56
  e =~ /\=\?UTF-8\?B\?(.+?)\?\=/
59
- $1.unpack('m')[0]
57
+ $1.unpack('m')[0].force_encoding('UTF-8')
60
58
  }.join
61
59
  assert_equal 'あいうえお'*20, s2
62
60
  end
@@ -86,7 +84,7 @@ class T_Sendmail < Test::Unit::TestCase
86
84
  def test_message_multibyte
87
85
  s = Sendmail.message('あいうえお',:from=>'hoge@example.com')
88
86
  (h,b) = parse_message(s)
89
- assert_equal 'あいうえお', b.unpack('m')[0]
87
+ assert_equal 'あいうえお', b.unpack('m')[0].force_encoding('UTF-8')
90
88
  assert_equal '1.0', h['MIME-Version']
91
89
  assert Time.rfc822(h['Date'])
92
90
  assert_equal 'base64', h['Content-Transfer-Encoding']
@@ -94,7 +92,7 @@ class T_Sendmail < Test::Unit::TestCase
94
92
  end
95
93
  def params
96
94
  {
97
- :date => Time.local(0),
95
+ :date => Time.at(0),
98
96
  :from => 'hoge@example.com',
99
97
  :to => [Sendmail.address('arai@example.com','新井俊一'),
100
98
  ['tanaka@example.com','田中太郎'],
@@ -114,15 +112,15 @@ class T_Sendmail < Test::Unit::TestCase
114
112
  assert_raise(RuntimeError) { Sendmail.message('',{:from => [1,2,3]}) }
115
113
  s = Sendmail.message('あいうえお',params)
116
114
  (h,b) = parse_message(s)
117
- assert_equal 'あいうえお', b.unpack('m')[0]
115
+ assert_equal 'あいうえお', b.unpack('m')[0].force_encoding('UTF-8')
118
116
  assert_equal '1.0', h['MIME-Version']
119
- assert_equal Time.local(0), Time.rfc822(h['Date'])
117
+ assert_equal Time.at(0), Time.rfc822(h['Date'])
120
118
  assert_equal 'base64', h['Content-Transfer-Encoding']
121
119
  assert_equal 'text/plain; charset=UTF-8', h['Content-Type']
122
120
  assert_equal 'hoge@example.com', h['From']
123
121
  assert_match /\A\=\?UTF-8\?B\?(.+?)\?\=\n?\Z/, h['Subject']
124
122
  h['Subject'] =~ /\A\=\?UTF-8\?B\?(.+?)\?\=/
125
- assert_equal 'こんにちは', $1.unpack('m')[0]
123
+ assert_equal 'こんにちは', $1.unpack('m')[0].force_encoding('UTF-8')
126
124
  to = h['To']
127
125
  tos = to.split(/\s*,\s+/)
128
126
  assert_match /\A\=\?UTF-8\?B\?(.+?)\?\= <arai@example.com>\Z/, tos[0]
@@ -130,7 +128,7 @@ class T_Sendmail < Test::Unit::TestCase
130
128
  assert_match /\A\=\?UTF-8\?B\?(.+?)\?\= <takeda@example.com>\Z/, tos[2]
131
129
  assert_match /\Aueno@example.com\Z/, tos[3]
132
130
  to =~ /\A\=\?UTF-8\?B\?(.+?)\?\=.+?\=\?UTF-8\?B\?(.+?)\?\=.+?\=\?UTF-8\?B\?(.+?)\?\=/
133
- (a,b,c) = [$1,$2,$3].map { |s| s.unpack('m')[0] }
131
+ (a,b,c) = [$1,$2,$3].map { |s| s.unpack('m')[0].force_encoding('UTF-8') }
134
132
  assert_match '新井俊一', a
135
133
  assert_match '田中太郎', b
136
134
  assert_match '武田一郎', c
@@ -183,5 +181,27 @@ EOS
183
181
  assert_equal false, Sendmail.verify_address("test@example.jp")
184
182
  assert_equal true, Sendmail.verify_address("test@example.com")
185
183
  end
184
+ def test_attachment
185
+ Sendmail.mock = true
186
+ Sendmail.force_dkim = false
187
+ tf = Tempfile.open("z")
188
+ tf.print "piyo"
189
+ tf.rewind
190
+ file = {
191
+ :filename => File.basename("test/static/test.txt"),
192
+ :type => "text/plain",
193
+ :name => "test.txt",
194
+ :tempfile => tf,
195
+ :head => "Content-Disposition: form-data; name=\"test.txt\"; filename=\"#{File.basename("test/static/test.txt")}\"\r\n" +
196
+ "Content-Type: text/plain\r\n" +
197
+ "Content-Length: 4\r\n"
198
+ }
199
+ Sendmail.send_with_uploaded_files("test",[file],:from => "arai@example.com", :to => "to@example.com")
200
+ Sendmail.lastmail[0] =~ /boundary="(.+?)"/
201
+ boundary = $1
202
+ assert_match "dGVzdA==", Sendmail.lastmail[0]
203
+ assert_match "cGl5bw==", Sendmail.lastmail[0]
204
+ assert_match "--#{boundary}--", Sendmail.lastmail[0]
205
+ end
186
206
  end
187
207
 
data/test.bat CHANGED
@@ -1,2 +1,3 @@
1
1
 
2
- rcov -x rack test/*.rb
2
+ ruby run_test.rb
3
+
metadata CHANGED
@@ -1,76 +1,65 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: egalite
3
- version: !ruby/object:Gem::Version
4
- hash: 7
5
- prerelease:
6
- segments:
7
- - 1
8
- - 5
9
- - 2
10
- version: 1.5.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.5.6
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Shunichi Arai
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2015-01-31 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: bundler
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 9
29
- segments:
30
- - 1
31
- - 3
32
- version: "1.3"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
33
20
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: rake
37
21
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
41
31
  - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
47
34
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rack
51
35
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
53
- none: false
54
- requirements:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
55
38
  - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
61
48
  type: :runtime
62
- version_requirements: *id003
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
63
55
  description: Egalite - yet another web application framework. see description at https://github.com/araipiyo/egalite
64
- email:
56
+ email:
65
57
  - arai@mellowtone.co.jp
66
58
  executables: []
67
-
68
59
  extensions: []
69
-
70
60
  extra_rdoc_files: []
71
-
72
- files:
73
- - .gitignore
61
+ files:
62
+ - ".gitignore"
74
63
  - Gemfile
75
64
  - LICENSE.txt
76
65
  - README.md
@@ -112,6 +101,7 @@ files:
112
101
  - lib/egalite/support.rb
113
102
  - lib/egalite/template.rb
114
103
  - lib/egalite/version.rb
104
+ - run_test.rb
115
105
  - sendmail.md
116
106
  - test.bat
117
107
  - test/cache.html
@@ -136,7 +126,6 @@ files:
136
126
  - test/test_errortemplate.rb
137
127
  - test/test_handler.rb
138
128
  - test/test_helper.rb
139
- - test/test_keitai.rb
140
129
  - test/test_m17n.rb
141
130
  - test/test_route.rb
142
131
  - test/test_sendmail.rb
@@ -144,39 +133,30 @@ files:
144
133
  - test/test_stringify_hash.rb
145
134
  - test/test_template.rb
146
135
  homepage: https://github.com/araipiyo/egalite
147
- licenses:
136
+ licenses:
148
137
  - MIT
138
+ metadata: {}
149
139
  post_install_message:
150
140
  rdoc_options: []
151
-
152
- require_paths:
141
+ require_paths:
153
142
  - lib
154
- required_ruby_version: !ruby/object:Gem::Requirement
155
- none: false
156
- requirements:
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
157
145
  - - ">="
158
- - !ruby/object:Gem::Version
159
- hash: 3
160
- segments:
161
- - 0
162
- version: "0"
163
- required_rubygems_version: !ruby/object:Gem::Requirement
164
- none: false
165
- requirements:
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
166
150
  - - ">="
167
- - !ruby/object:Gem::Version
168
- hash: 3
169
- segments:
170
- - 0
171
- version: "0"
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
172
153
  requirements: []
173
-
174
154
  rubyforge_project:
175
- rubygems_version: 1.8.24
155
+ rubygems_version: 2.2.2
176
156
  signing_key:
177
- specification_version: 3
157
+ specification_version: 4
178
158
  summary: Egalite - yet another web application framework.
179
- test_files:
159
+ test_files:
180
160
  - test/cache.html
181
161
  - test/cache_cache.html
182
162
  - test/cache_nocache.html
@@ -199,7 +179,6 @@ test_files:
199
179
  - test/test_errortemplate.rb
200
180
  - test/test_handler.rb
201
181
  - test/test_helper.rb
202
- - test/test_keitai.rb
203
182
  - test/test_m17n.rb
204
183
  - test/test_route.rb
205
184
  - test/test_sendmail.rb
data/test/test_keitai.rb DELETED
@@ -1,107 +0,0 @@
1
- #!ruby -Ku
2
-
3
- $KCODE = 'UTF8'
4
-
5
- $LOAD_PATH << File.join(File.dirname(__FILE__))
6
-
7
- require 'rubygems'
8
- require 'test/unit'
9
- require 'egalite'
10
- require 'egalite/keitai/keitai'
11
-
12
- require 'rack'
13
- require 'rack/multipart'
14
- require 'rack/test'
15
- require 'rack/ketai'
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