rack-rack-contrib 0.9.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.
Files changed (55) hide show
  1. data/COPYING +18 -0
  2. data/README.rdoc +74 -0
  3. data/Rakefile +97 -0
  4. data/lib/rack/contrib.rb +32 -0
  5. data/lib/rack/contrib/accept_format.rb +44 -0
  6. data/lib/rack/contrib/backstage.rb +20 -0
  7. data/lib/rack/contrib/bounce_favicon.rb +16 -0
  8. data/lib/rack/contrib/callbacks.rb +37 -0
  9. data/lib/rack/contrib/config.rb +16 -0
  10. data/lib/rack/contrib/csshttprequest.rb +39 -0
  11. data/lib/rack/contrib/deflect.rb +137 -0
  12. data/lib/rack/contrib/etag.rb +20 -0
  13. data/lib/rack/contrib/evil.rb +12 -0
  14. data/lib/rack/contrib/garbagecollector.rb +14 -0
  15. data/lib/rack/contrib/jsonp.rb +41 -0
  16. data/lib/rack/contrib/lighttpd_script_name_fix.rb +16 -0
  17. data/lib/rack/contrib/locale.rb +31 -0
  18. data/lib/rack/contrib/mailexceptions.rb +120 -0
  19. data/lib/rack/contrib/nested_params.rb +143 -0
  20. data/lib/rack/contrib/not_found.rb +18 -0
  21. data/lib/rack/contrib/post_body_content_type_parser.rb +40 -0
  22. data/lib/rack/contrib/proctitle.rb +30 -0
  23. data/lib/rack/contrib/profiler.rb +106 -0
  24. data/lib/rack/contrib/relative_redirect.rb +44 -0
  25. data/lib/rack/contrib/response_cache.rb +59 -0
  26. data/lib/rack/contrib/route_exceptions.rb +48 -0
  27. data/lib/rack/contrib/sendfile.rb +142 -0
  28. data/lib/rack/contrib/signals.rb +63 -0
  29. data/lib/rack/contrib/time_zone.rb +25 -0
  30. data/rack-contrib.gemspec +87 -0
  31. data/test/404.html +1 -0
  32. data/test/Maintenance.html +1 -0
  33. data/test/mail_settings.rb +12 -0
  34. data/test/spec_rack_accept_format.rb +41 -0
  35. data/test/spec_rack_backstage.rb +26 -0
  36. data/test/spec_rack_callbacks.rb +65 -0
  37. data/test/spec_rack_config.rb +22 -0
  38. data/test/spec_rack_contrib.rb +8 -0
  39. data/test/spec_rack_csshttprequest.rb +66 -0
  40. data/test/spec_rack_deflect.rb +107 -0
  41. data/test/spec_rack_etag.rb +23 -0
  42. data/test/spec_rack_evil.rb +19 -0
  43. data/test/spec_rack_garbagecollector.rb +13 -0
  44. data/test/spec_rack_jsonp.rb +34 -0
  45. data/test/spec_rack_lighttpd_script_name_fix.rb +16 -0
  46. data/test/spec_rack_mailexceptions.rb +97 -0
  47. data/test/spec_rack_nested_params.rb +46 -0
  48. data/test/spec_rack_not_found.rb +17 -0
  49. data/test/spec_rack_post_body_content_type_parser.rb +32 -0
  50. data/test/spec_rack_proctitle.rb +26 -0
  51. data/test/spec_rack_profiler.rb +37 -0
  52. data/test/spec_rack_relative_redirect.rb +78 -0
  53. data/test/spec_rack_response_cache.rb +137 -0
  54. data/test/spec_rack_sendfile.rb +86 -0
  55. metadata +171 -0
@@ -0,0 +1,137 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/response_cache'
4
+ require 'fileutils'
5
+
6
+ context Rack::ResponseCache do
7
+ F = ::File
8
+
9
+ def request(opts={}, &block)
10
+ Rack::MockRequest.new(Rack::ResponseCache.new(block||@def_app, opts[:cache]||@cache, &opts[:rc_block])).send(opts[:meth]||:get, opts[:path]||@def_path, opts[:headers]||{})
11
+ end
12
+
13
+ setup do
14
+ @cache = {}
15
+ @def_disk_cache = F.join(F.dirname(__FILE__), 'response_cache_test_disk_cache')
16
+ @def_value = ["rack-response-cache"]
17
+ @def_path = '/path/to/blah'
18
+ @def_app = lambda { |env| [200, {'Content-Type' => env['CT'] || 'text/html'}, @def_value]}
19
+ end
20
+ teardown do
21
+ FileUtils.rm_rf(@def_disk_cache)
22
+ end
23
+
24
+ specify "should cache results to disk if cache is a string" do
25
+ request(:cache=>@def_disk_cache)
26
+ F.read(F.join(@def_disk_cache, 'path', 'to', 'blah.html')).should.equal @def_value.first
27
+ request(:path=>'/path/3', :cache=>@def_disk_cache)
28
+ F.read(F.join(@def_disk_cache, 'path', '3.html')).should.equal @def_value.first
29
+ end
30
+
31
+ specify "should cache results to given cache if cache is not a string" do
32
+ request
33
+ @cache.should.equal('/path/to/blah.html'=>@def_value)
34
+ request(:path=>'/path/3')
35
+ @cache.should.equal('/path/to/blah.html'=>@def_value, '/path/3.html'=>@def_value)
36
+ end
37
+
38
+ specify "should not CACHE RESults if request method is not GET" do
39
+ request(:meth=>:post)
40
+ @cache.should.equal({})
41
+ request(:meth=>:put)
42
+ @cache.should.equal({})
43
+ request(:meth=>:delete)
44
+ @cache.should.equal({})
45
+ end
46
+
47
+ specify "should not cache results if there is a query string" do
48
+ request(:path=>'/path/to/blah?id=1')
49
+ @cache.should.equal({})
50
+ request(:path=>'/path/to/?id=1')
51
+ @cache.should.equal({})
52
+ request(:path=>'/?id=1')
53
+ @cache.should.equal({})
54
+ end
55
+
56
+ specify "should cache results if there is an empty query string" do
57
+ request(:path=>'/?')
58
+ @cache.should.equal('/index.html'=>@def_value)
59
+ end
60
+
61
+ specify "should not cache results if the request is not sucessful (status 200)" do
62
+ request{|env| [404, {'Content-Type' => 'text/html'}, ['']]}
63
+ @cache.should.equal({})
64
+ request{|env| [500, {'Content-Type' => 'text/html'}, ['']]}
65
+ @cache.should.equal({})
66
+ request{|env| [302, {'Content-Type' => 'text/html'}, ['']]}
67
+ @cache.should.equal({})
68
+ end
69
+
70
+ specify "should not cache results if the block returns nil or false" do
71
+ request(:rc_block=>proc{false})
72
+ @cache.should.equal({})
73
+ request(:rc_block=>proc{nil})
74
+ @cache.should.equal({})
75
+ end
76
+
77
+ specify "should cache results to path returned by block" do
78
+ request(:rc_block=>proc{"1"})
79
+ @cache.should.equal("1"=>@def_value)
80
+ request(:rc_block=>proc{"2"})
81
+ @cache.should.equal("1"=>@def_value, "2"=>@def_value)
82
+ end
83
+
84
+ specify "should pass the environment and response to the block" do
85
+ e, r = nil, nil
86
+ request(:rc_block=>proc{|env,res| e, r = env, res; nil})
87
+ e['PATH_INFO'].should.equal @def_path
88
+ e['REQUEST_METHOD'].should.equal 'GET'
89
+ e['QUERY_STRING'].should.equal ''
90
+ r.should.equal([200, {"Content-Type"=>"text/html"}, ["rack-response-cache"]])
91
+ end
92
+
93
+ specify "should unescape the path by default" do
94
+ request(:path=>'/path%20with%20spaces')
95
+ @cache.should.equal('/path with spaces.html'=>@def_value)
96
+ request(:path=>'/path%3chref%3e')
97
+ @cache.should.equal('/path with spaces.html'=>@def_value, '/path<href>.html'=>@def_value)
98
+ end
99
+
100
+ specify "should cache html, css, and xml responses by default" do
101
+ request(:path=>'/a')
102
+ @cache.should.equal('/a.html'=>@def_value)
103
+ request(:path=>'/b', :headers=>{'CT'=>'text/xml'})
104
+ @cache.should.equal('/a.html'=>@def_value, '/b.xml'=>@def_value)
105
+ request(:path=>'/c', :headers=>{'CT'=>'text/css'})
106
+ @cache.should.equal('/a.html'=>@def_value, '/b.xml'=>@def_value, '/c.css'=>@def_value)
107
+ end
108
+
109
+ specify "should cache responses by default with the extension added if not already present" do
110
+ request(:path=>'/a.html')
111
+ @cache.should.equal('/a.html'=>@def_value)
112
+ request(:path=>'/b.xml', :headers=>{'CT'=>'text/xml'})
113
+ @cache.should.equal('/a.html'=>@def_value, '/b.xml'=>@def_value)
114
+ request(:path=>'/c.css', :headers=>{'CT'=>'text/css'})
115
+ @cache.should.equal('/a.html'=>@def_value, '/b.xml'=>@def_value, '/c.css'=>@def_value)
116
+ end
117
+
118
+ specify "should not delete existing extensions" do
119
+ request(:path=>'/d.css', :headers=>{'CT'=>'text/html'})
120
+ @cache.should.equal('/d.css.html'=>@def_value)
121
+ end
122
+
123
+ specify "should cache html responses with empty basename to index.html by default" do
124
+ request(:path=>'/')
125
+ @cache.should.equal('/index.html'=>@def_value)
126
+ request(:path=>'/blah/')
127
+ @cache.should.equal('/index.html'=>@def_value, '/blah/index.html'=>@def_value)
128
+ request(:path=>'/blah/2/')
129
+ @cache.should.equal('/index.html'=>@def_value, '/blah/index.html'=>@def_value, '/blah/2/index.html'=>@def_value)
130
+ end
131
+
132
+ specify "should raise an error if a cache argument is not provided" do
133
+ app = Rack::Builder.new{use Rack::ResponseCache; run lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env).POST]}}
134
+ proc{Rack::MockRequest.new(app).get('/')}.should.raise(ArgumentError)
135
+ end
136
+
137
+ end
@@ -0,0 +1,86 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/sendfile'
4
+
5
+ context "Rack::File" do
6
+ specify "should respond to #to_path" do
7
+ Rack::File.new(Dir.pwd).should.respond_to :to_path
8
+ end
9
+ end
10
+
11
+ context "Rack::Sendfile" do
12
+ def sendfile_body
13
+ res = ['Hello World']
14
+ def res.to_path ; "/tmp/hello.txt" ; end
15
+ res
16
+ end
17
+
18
+ def simple_app(body=sendfile_body)
19
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
20
+ end
21
+
22
+ def sendfile_app(body=sendfile_body)
23
+ Rack::Sendfile.new(simple_app(body))
24
+ end
25
+
26
+ setup do
27
+ @request = Rack::MockRequest.new(sendfile_app)
28
+ end
29
+
30
+ def request(headers={})
31
+ yield @request.get('/', headers)
32
+ end
33
+
34
+ specify "does nothing when no X-Sendfile-Type header present" do
35
+ request do |response|
36
+ response.should.be.ok
37
+ response.body.should.equal 'Hello World'
38
+ response.headers.should.not.include 'X-Sendfile'
39
+ end
40
+ end
41
+
42
+ specify "sets X-Sendfile response header and discards body" do
43
+ request 'HTTP_X_SENDFILE_TYPE' => 'X-Sendfile' do |response|
44
+ response.should.be.ok
45
+ response.body.should.be.empty
46
+ response.headers['X-Sendfile'].should.equal '/tmp/hello.txt'
47
+ end
48
+ end
49
+
50
+ specify "sets X-Lighttpd-Send-File response header and discards body" do
51
+ request 'HTTP_X_SENDFILE_TYPE' => 'X-Lighttpd-Send-File' do |response|
52
+ response.should.be.ok
53
+ response.body.should.be.empty
54
+ response.headers['X-Lighttpd-Send-File'].should.equal '/tmp/hello.txt'
55
+ end
56
+ end
57
+
58
+ specify "sets X-Accel-Redirect response header and discards body" do
59
+ headers = {
60
+ 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect',
61
+ 'HTTP_X_ACCEL_MAPPING' => '/tmp/=/foo/bar/'
62
+ }
63
+ request headers do |response|
64
+ response.should.be.ok
65
+ response.body.should.be.empty
66
+ response.headers['X-Accel-Redirect'].should.equal '/foo/bar/hello.txt'
67
+ end
68
+ end
69
+
70
+ specify 'writes to rack.error when no X-Accel-Mapping is specified' do
71
+ request 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect' do |response|
72
+ response.should.be.ok
73
+ response.body.should.equal 'Hello World'
74
+ response.headers.should.not.include 'X-Accel-Redirect'
75
+ response.errors.should.include 'X-Accel-Mapping'
76
+ end
77
+ end
78
+
79
+ specify 'does nothing when body does not respond to #to_path' do
80
+ @request = Rack::MockRequest.new(sendfile_app(['Not a file...']))
81
+ request 'HTTP_X_SENDFILE_TYPE' => 'X-Sendfile' do |response|
82
+ response.body.should.equal 'Not a file...'
83
+ response.headers.should.not.include 'X-Sendfile'
84
+ end
85
+ end
86
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-rack-contrib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - rack-devel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-07 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 0.9.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: test-spec
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: tmail
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "1.2"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: json
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "1.1"
54
+ version:
55
+ description: Contributed Rack Middleware and Utilities
56
+ email: rack-devel@googlegroups.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - README.rdoc
63
+ - COPYING
64
+ files:
65
+ - COPYING
66
+ - README.rdoc
67
+ - Rakefile
68
+ - lib/rack/contrib.rb
69
+ - lib/rack/contrib/accept_format.rb
70
+ - lib/rack/contrib/backstage.rb
71
+ - lib/rack/contrib/bounce_favicon.rb
72
+ - lib/rack/contrib/callbacks.rb
73
+ - lib/rack/contrib/config.rb
74
+ - lib/rack/contrib/csshttprequest.rb
75
+ - lib/rack/contrib/deflect.rb
76
+ - lib/rack/contrib/etag.rb
77
+ - lib/rack/contrib/evil.rb
78
+ - lib/rack/contrib/garbagecollector.rb
79
+ - lib/rack/contrib/jsonp.rb
80
+ - lib/rack/contrib/lighttpd_script_name_fix.rb
81
+ - lib/rack/contrib/locale.rb
82
+ - lib/rack/contrib/mailexceptions.rb
83
+ - lib/rack/contrib/nested_params.rb
84
+ - lib/rack/contrib/not_found.rb
85
+ - lib/rack/contrib/post_body_content_type_parser.rb
86
+ - lib/rack/contrib/proctitle.rb
87
+ - lib/rack/contrib/profiler.rb
88
+ - lib/rack/contrib/relative_redirect.rb
89
+ - lib/rack/contrib/response_cache.rb
90
+ - lib/rack/contrib/route_exceptions.rb
91
+ - lib/rack/contrib/sendfile.rb
92
+ - lib/rack/contrib/signals.rb
93
+ - lib/rack/contrib/time_zone.rb
94
+ - rack-contrib.gemspec
95
+ - test/404.html
96
+ - test/Maintenance.html
97
+ - test/mail_settings.rb
98
+ - test/spec_rack_accept_format.rb
99
+ - test/spec_rack_backstage.rb
100
+ - test/spec_rack_callbacks.rb
101
+ - test/spec_rack_config.rb
102
+ - test/spec_rack_contrib.rb
103
+ - test/spec_rack_csshttprequest.rb
104
+ - test/spec_rack_deflect.rb
105
+ - test/spec_rack_etag.rb
106
+ - test/spec_rack_evil.rb
107
+ - test/spec_rack_garbagecollector.rb
108
+ - test/spec_rack_jsonp.rb
109
+ - test/spec_rack_lighttpd_script_name_fix.rb
110
+ - test/spec_rack_mailexceptions.rb
111
+ - test/spec_rack_nested_params.rb
112
+ - test/spec_rack_not_found.rb
113
+ - test/spec_rack_post_body_content_type_parser.rb
114
+ - test/spec_rack_proctitle.rb
115
+ - test/spec_rack_profiler.rb
116
+ - test/spec_rack_relative_redirect.rb
117
+ - test/spec_rack_response_cache.rb
118
+ - test/spec_rack_sendfile.rb
119
+ has_rdoc: true
120
+ homepage: http://github.com/rack/rack-contrib/
121
+ post_install_message:
122
+ rdoc_options:
123
+ - --line-numbers
124
+ - --inline-source
125
+ - --title
126
+ - rack-contrib
127
+ - --main
128
+ - README
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: "0"
136
+ version:
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: "0"
142
+ version:
143
+ requirements: []
144
+
145
+ rubyforge_project:
146
+ rubygems_version: 1.2.0
147
+ signing_key:
148
+ specification_version: 2
149
+ summary: Contributed Rack Middleware and Utilities
150
+ test_files:
151
+ - test/spec_rack_accept_format.rb
152
+ - test/spec_rack_backstage.rb
153
+ - test/spec_rack_callbacks.rb
154
+ - test/spec_rack_config.rb
155
+ - test/spec_rack_contrib.rb
156
+ - test/spec_rack_csshttprequest.rb
157
+ - test/spec_rack_deflect.rb
158
+ - test/spec_rack_etag.rb
159
+ - test/spec_rack_evil.rb
160
+ - test/spec_rack_garbagecollector.rb
161
+ - test/spec_rack_jsonp.rb
162
+ - test/spec_rack_lighttpd_script_name_fix.rb
163
+ - test/spec_rack_mailexceptions.rb
164
+ - test/spec_rack_nested_params.rb
165
+ - test/spec_rack_not_found.rb
166
+ - test/spec_rack_post_body_content_type_parser.rb
167
+ - test/spec_rack_proctitle.rb
168
+ - test/spec_rack_profiler.rb
169
+ - test/spec_rack_relative_redirect.rb
170
+ - test/spec_rack_response_cache.rb
171
+ - test/spec_rack_sendfile.rb