rufus-sixjo 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.txt CHANGED
@@ -8,7 +8,7 @@ A 'Rack application' for
8
8
 
9
9
  == features
10
10
 
11
- not many.
11
+ Does the conditional GET thing.
12
12
 
13
13
 
14
14
  == getting it
data/lib/rufus/sixjo.rb CHANGED
@@ -29,12 +29,25 @@
29
29
  require 'erb'
30
30
  require 'rack'
31
31
 
32
+ require 'time'
33
+ require 'date'
34
+
32
35
 
33
36
  class Rack::Request
34
37
 
38
+ #
39
+ # not sure about this one, might vanish soon
40
+ #
35
41
  def content
36
42
  @env['rack.request.form_vars']
37
43
  end
44
+
45
+ #
46
+ # returns an array of ETags
47
+ #
48
+ def etags
49
+ (@env['HTTP_IF_NONE_MATCH'] || '').split(/\s*,\s*/)
50
+ end
38
51
  end
39
52
 
40
53
  class Rack::Response
@@ -52,7 +65,7 @@ module Rufus
52
65
 
53
66
  module Sixjo
54
67
 
55
- VERSION = "0.1.0"
68
+ VERSION = '0.1.1'
56
69
 
57
70
  #
58
71
  # Sixjo's Rack app
@@ -186,7 +199,7 @@ module Rufus
186
199
 
187
200
  def erb (template, options = {})
188
201
 
189
- content = File.open("views/#{template}.erb").read
202
+ content = File.read("views/#{template}.erb")
190
203
 
191
204
  l = options[:locals]
192
205
  l = Local.new(self, l || {}) unless l.is_a?(Local)
@@ -237,7 +250,7 @@ module Rufus
237
250
  end
238
251
 
239
252
  if caught
240
- #puts caught.inspect
253
+ caught = Array(caught)
241
254
  r.response.status = caught[0]
242
255
  r.response.body = caught[1]
243
256
  end
@@ -256,12 +269,61 @@ module Rufus
256
269
  @params
257
270
  end
258
271
 
272
+ def h (text)
273
+ Rack::Utils.escape_html(text)
274
+ end
275
+
259
276
  def redirect (path, status=303, body=nil)
260
277
  @response.status = status
261
278
  @response.location = path
262
279
  @response.body = body || "#{status} redirecting to #{path}"
263
280
  throw :done
264
281
  end
282
+
283
+ #
284
+ # throws 304 as needed
285
+ #
286
+ def set_etag (t, weak=false)
287
+
288
+ t = '"%s"' % t
289
+ t = weak ? "W/#{t}" : t
290
+
291
+ @response.header['ETag'] = t
292
+
293
+ etags = @request.etags
294
+
295
+ if etags.include?(t) or etags.include?('*')
296
+ throw(:done, 304) if @request.get? or @request.head?
297
+ throw(:done, 412) # precondition failed
298
+ end
299
+ end
300
+
301
+ #
302
+ # throws 304 as needed
303
+ #
304
+ def set_last_modified (t)
305
+
306
+ t = Time.local(*t.to_a) # flatten milliseconds
307
+
308
+ @response.header['Last-Modified'] = t.httpdate
309
+
310
+ sin = @request.env['HTTP_IF_MODIFIED_SINCE']
311
+
312
+ return unless sin
313
+
314
+ # taken from the "Ruby Cookbook" by
315
+ # Lucas Carlson and Leonard Richardson
316
+ #
317
+ sin = DateTime.parse(sin)
318
+ sin = sin.new_offset(DateTime.now.offset - sin.offset)
319
+ sin = Time.local(
320
+ sin.year, sin.month, sin.day, sin.hour, sin.min, sin.sec, 0)
321
+
322
+ if sin >= t
323
+ throw(:done, 304) if @request.get? or @request.head?
324
+ throw(:done, 412) # precondition failed
325
+ end
326
+ end
265
327
  end
266
328
 
267
329
  #
data/test/ft_3_erb.rb CHANGED
@@ -41,17 +41,8 @@ class ErbTest < Test::Unit::TestCase
41
41
 
42
42
  @app = ErbApp.new_sixjo_rack_app(nil, :environment => 'test')
43
43
 
44
- FileUtils.mkdir('views') unless File.exist?('views')
45
-
46
- fn = 'views/view0.erb'
47
-
48
- FileUtils.rm(fn) if File.exist?(fn)
49
- File.open(fn, 'w') { |f| f.write "this is view0, life is <%= life %>" }
50
-
51
- fn = 'views/view1.erb'
52
-
53
- FileUtils.rm(fn) if File.exist?(fn)
54
- File.open(fn, 'w') { |f| f.write "<%= request.path_info %>" }
44
+ save_view('views/view0.erb', 'this is view0, life is <%= life %>')
45
+ save_view('views/view1.erb', '<%= request.path_info %>')
55
46
  end
56
47
 
57
48
  def test_0
data/test/ft_5_put.rb CHANGED
@@ -19,11 +19,15 @@ module PutApp
19
19
  post '/' do
20
20
  #puts request.inspect
21
21
  #request.env['rack.input'].read
22
- request.content
22
+ request.env['rack.request.form_vars']
23
23
  end
24
24
 
25
25
  put '/car' do
26
- "put : #{request.content || params[:brand]}"
26
+ #"put : #{request.content || params[:brand]}"
27
+ answer = request.body.read
28
+ answer = params[:brand] if answer.empty?
29
+ answer = request.env['rack.request.form_vars'] if answer.empty?
30
+ "put : #{answer}"
27
31
  end
28
32
 
29
33
  delete '/car/:id' do
@@ -93,5 +97,21 @@ class PutTest < Test::Unit::TestCase
93
97
  }).status)
94
98
  assert_equal 'put : ford', @response.body
95
99
  end
100
+
101
+ def test_3
102
+
103
+ data = "<brand>mitsubishi</brand>"
104
+
105
+ assert_equal(
106
+ 200,
107
+ put(
108
+ '/car',
109
+ {
110
+ :input => data,
111
+ 'CONTENT_LENGTH' => data.length.to_s,
112
+ 'CONTENT_TYPE' => 'application/xml'
113
+ }).status)
114
+ assert_equal "put : #{data}", @response.body
115
+ end
96
116
  end
97
117
 
data/test/ft_6_h.rb ADDED
@@ -0,0 +1,63 @@
1
+
2
+ #
3
+ # Testing rufus-sixjo
4
+ #
5
+ # jmettraux at gmail.org
6
+ #
7
+ # Mon Aug 25 09:09:34 JST 2008
8
+ #
9
+
10
+ require 'test/unit'
11
+ require 'testmixins'
12
+
13
+ #
14
+ # the "test" app
15
+ #
16
+ module HachiHelper
17
+ extend Rufus::Sixjo
18
+
19
+ get '/hello' do
20
+ hello
21
+ end
22
+
23
+ get '/bundi' do
24
+ h('<bundi>')
25
+ end
26
+
27
+ get '/bondzoi' do
28
+ erb :ft6, :locals => { :life => '>good<' }
29
+ end
30
+
31
+ helpers do
32
+ def hello
33
+ h("hello #{params[:target]}")
34
+ end
35
+ end
36
+ end
37
+
38
+ class HachiTest < Test::Unit::TestCase
39
+ include SixjoTestMixin
40
+
41
+ def setup
42
+
43
+ @app = HachiHelper.new_sixjo_rack_app(nil, :environment => 'test')
44
+
45
+ save_view('views/ft6.erb', 'ft6, life is <%= h(life) %>')
46
+ end
47
+
48
+ def test_0
49
+
50
+ assert_equal 200, get('/hello?target=maam').status
51
+ assert_equal 'hello maam', @response.body
52
+
53
+ assert_equal 200, get('/bundi').status
54
+ assert_equal '&lt;bundi&gt;', @response.body
55
+ end
56
+
57
+ def test_1
58
+
59
+ assert_equal 200, get('/bondzoi').status
60
+ assert_equal 'ft6, life is &gt;good&lt;', @response.body
61
+ end
62
+ end
63
+
data/test/ft_7_etag.rb ADDED
@@ -0,0 +1,72 @@
1
+
2
+ #
3
+ # Testing rufus-sixjo
4
+ #
5
+ # jmettraux at gmail.org
6
+ #
7
+ # Wed Aug 27 11:12:17 JST 2008
8
+ #
9
+
10
+ require 'test/unit'
11
+ require 'testmixins'
12
+
13
+ NOW = Time.now
14
+
15
+ #
16
+ # the "test" app
17
+ #
18
+ module EtagApp
19
+ extend Rufus::Sixjo
20
+
21
+ get '/hello' do
22
+ set_etag('wienerli')
23
+ 'hello'
24
+ end
25
+
26
+ get '/sayonara' do
27
+ set_last_modified(NOW)
28
+ 'sayonara'
29
+ end
30
+ end
31
+
32
+ class EtagTest < Test::Unit::TestCase
33
+ include SixjoTestMixin
34
+
35
+ def setup
36
+
37
+ @app = EtagApp.new_sixjo_rack_app(nil, :environment => 'test')
38
+ end
39
+
40
+ def test_0_etag
41
+
42
+ assert_equal 200, get('/hello').status
43
+ assert_equal '"wienerli"', @response.headers['ETag']
44
+ assert_equal 'hello', @response.body
45
+
46
+ assert_equal 304, get('/hello', 'HTTP_IF_NONE_MATCH' => '"wienerli"').status
47
+ assert_equal '"wienerli"', @response.headers['ETag']
48
+ assert_equal '', @response.body
49
+ end
50
+
51
+ def test_1_last_modified
52
+
53
+ assert_equal 200, get('/sayonara').status
54
+ assert_not_nil @response.headers['Last-Modified']
55
+ assert_equal 'sayonara', @response.body
56
+
57
+ get(
58
+ '/sayonara',
59
+ 'HTTP_IF_MODIFIED_SINCE' => @response.headers['Last-Modified'])
60
+ assert_equal 304, @response.status
61
+ assert_equal '', @response.body
62
+
63
+ assert_equal(
64
+ 200,
65
+ get(
66
+ '/sayonara',
67
+ 'HTTP_IF_MODIFIED_SINCE' => (Time.now - 3601).httpdate
68
+ ).status)
69
+ end
70
+
71
+ end
72
+
data/test/test.rb CHANGED
@@ -5,4 +5,6 @@ require 'ft_2_redirect'
5
5
  require 'ft_3_erb'
6
6
  require 'ft_4_configure'
7
7
  require 'ft_5_put'
8
+ require 'ft_6_h'
9
+ require 'ft_7_etag'
8
10
 
data/test/testmixins.rb CHANGED
@@ -10,6 +10,22 @@
10
10
  require 'rubygems'
11
11
  require 'rufus/sixjo'
12
12
 
13
+ #class Rack::MockResponse
14
+ # alias :old_init :initialize
15
+ # def initialize (s, h, b, errors=nil)
16
+ # puts
17
+ # p [ :status, s ]
18
+ # p [ :headers, h ]
19
+ # p [ :body, b ]
20
+ # old_init(s, h, b, errors)
21
+ # end
22
+ #end
23
+
24
+ class NilClass
25
+ def empty?
26
+ true
27
+ end
28
+ end
13
29
 
14
30
  module SixjoTestMixin
15
31
 
@@ -36,5 +52,13 @@ module SixjoTestMixin
36
52
  end
37
53
  ret << "--#{MFD_BOUNDARY}--\r\n"
38
54
  end
55
+
56
+ def save_view (filename, content)
57
+
58
+ FileUtils.mkdir('views') unless File.exist?('views')
59
+
60
+ FileUtils.rm(filename) if File.exist?(filename)
61
+ File.open(filename, 'w') { |f| f.write(content) }
62
+ end
39
63
  end
40
64
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-sixjo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-15 00:00:00 +09:00
12
+ date: 2008-08-28 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -39,6 +39,8 @@ files:
39
39
  - test/ft_3_erb.rb
40
40
  - test/ft_4_configure.rb
41
41
  - test/ft_5_put.rb
42
+ - test/ft_6_h.rb
43
+ - test/ft_7_etag.rb
42
44
  - test/test.rb
43
45
  - test/testmixins.rb
44
46
  - README.txt