corntrace-rack-contrib 1.0.2

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 (72) hide show
  1. data/AUTHORS +26 -0
  2. data/COPYING +18 -0
  3. data/README.rdoc +87 -0
  4. data/Rakefile +89 -0
  5. data/lib/rack/contrib/accept_format.rb +46 -0
  6. data/lib/rack/contrib/access.rb +85 -0
  7. data/lib/rack/contrib/backstage.rb +20 -0
  8. data/lib/rack/contrib/bounce_favicon.rb +16 -0
  9. data/lib/rack/contrib/callbacks.rb +37 -0
  10. data/lib/rack/contrib/config.rb +16 -0
  11. data/lib/rack/contrib/cookies.rb +50 -0
  12. data/lib/rack/contrib/csshttprequest.rb +39 -0
  13. data/lib/rack/contrib/deflect.rb +137 -0
  14. data/lib/rack/contrib/evil.rb +12 -0
  15. data/lib/rack/contrib/expectation_cascade.rb +32 -0
  16. data/lib/rack/contrib/garbagecollector.rb +14 -0
  17. data/lib/rack/contrib/host_meta.rb +47 -0
  18. data/lib/rack/contrib/jsonp.rb +78 -0
  19. data/lib/rack/contrib/lighttpd_script_name_fix.rb +16 -0
  20. data/lib/rack/contrib/locale.rb +31 -0
  21. data/lib/rack/contrib/mailexceptions.rb +120 -0
  22. data/lib/rack/contrib/nested_params.rb +143 -0
  23. data/lib/rack/contrib/not_found.rb +18 -0
  24. data/lib/rack/contrib/post_body_content_type_parser.rb +40 -0
  25. data/lib/rack/contrib/proctitle.rb +30 -0
  26. data/lib/rack/contrib/profiler.rb +108 -0
  27. data/lib/rack/contrib/relative_redirect.rb +44 -0
  28. data/lib/rack/contrib/response_cache.rb +59 -0
  29. data/lib/rack/contrib/response_headers.rb +24 -0
  30. data/lib/rack/contrib/route_exceptions.rb +49 -0
  31. data/lib/rack/contrib/runtime.rb +31 -0
  32. data/lib/rack/contrib/sendfile.rb +142 -0
  33. data/lib/rack/contrib/signals.rb +63 -0
  34. data/lib/rack/contrib/simple_endpoint.rb +81 -0
  35. data/lib/rack/contrib/static_cache.rb +93 -0
  36. data/lib/rack/contrib/time_zone.rb +25 -0
  37. data/lib/rack/contrib.rb +39 -0
  38. data/rack-contrib.gemspec +104 -0
  39. data/test/404.html +1 -0
  40. data/test/Maintenance.html +1 -0
  41. data/test/documents/test +1 -0
  42. data/test/mail_settings.rb +12 -0
  43. data/test/spec_rack_accept_format.rb +72 -0
  44. data/test/spec_rack_access.rb +154 -0
  45. data/test/spec_rack_backstage.rb +26 -0
  46. data/test/spec_rack_callbacks.rb +65 -0
  47. data/test/spec_rack_config.rb +22 -0
  48. data/test/spec_rack_contrib.rb +8 -0
  49. data/test/spec_rack_cookies.rb +56 -0
  50. data/test/spec_rack_csshttprequest.rb +66 -0
  51. data/test/spec_rack_deflect.rb +107 -0
  52. data/test/spec_rack_evil.rb +19 -0
  53. data/test/spec_rack_expectation_cascade.rb +72 -0
  54. data/test/spec_rack_garbagecollector.rb +13 -0
  55. data/test/spec_rack_host_meta.rb +50 -0
  56. data/test/spec_rack_jsonp.rb +83 -0
  57. data/test/spec_rack_lighttpd_script_name_fix.rb +16 -0
  58. data/test/spec_rack_mailexceptions.rb +97 -0
  59. data/test/spec_rack_nested_params.rb +46 -0
  60. data/test/spec_rack_not_found.rb +17 -0
  61. data/test/spec_rack_post_body_content_type_parser.rb +32 -0
  62. data/test/spec_rack_proctitle.rb +26 -0
  63. data/test/spec_rack_profiler.rb +37 -0
  64. data/test/spec_rack_relative_redirect.rb +78 -0
  65. data/test/spec_rack_response_cache.rb +137 -0
  66. data/test/spec_rack_response_headers.rb +35 -0
  67. data/test/spec_rack_runtime.rb +35 -0
  68. data/test/spec_rack_sendfile.rb +86 -0
  69. data/test/spec_rack_simple_endpoint.rb +95 -0
  70. data/test/spec_rack_static_cache.rb +91 -0
  71. data/test/statics/test +1 -0
  72. metadata +234 -0
@@ -0,0 +1,104 @@
1
+ Gem::Specification.new do |s|
2
+ s.specification_version = 2 if s.respond_to? :specification_version=
3
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
+
5
+ s.name = 'corntrace-rack-contrib'
6
+ s.version = '1.0.2'
7
+ s.date = '2010-07-18'
8
+
9
+ s.description = "Contributed Rack Middleware and Utilities"
10
+ s.summary = "Contributed Rack Middleware and Utilities"
11
+
12
+ s.authors = ["Kevin Fu", "rack-devel"]
13
+ s.email = ["corntrace@gmail.com", "rack-devel@googlegroups.com"]
14
+
15
+ # = MANIFEST =
16
+ s.files = %w[
17
+ AUTHORS
18
+ COPYING
19
+ README.rdoc
20
+ Rakefile
21
+ lib/rack/contrib.rb
22
+ lib/rack/contrib/accept_format.rb
23
+ lib/rack/contrib/access.rb
24
+ lib/rack/contrib/backstage.rb
25
+ lib/rack/contrib/bounce_favicon.rb
26
+ lib/rack/contrib/callbacks.rb
27
+ lib/rack/contrib/config.rb
28
+ lib/rack/contrib/cookies.rb
29
+ lib/rack/contrib/csshttprequest.rb
30
+ lib/rack/contrib/deflect.rb
31
+ lib/rack/contrib/evil.rb
32
+ lib/rack/contrib/expectation_cascade.rb
33
+ lib/rack/contrib/garbagecollector.rb
34
+ lib/rack/contrib/host_meta.rb
35
+ lib/rack/contrib/jsonp.rb
36
+ lib/rack/contrib/lighttpd_script_name_fix.rb
37
+ lib/rack/contrib/locale.rb
38
+ lib/rack/contrib/mailexceptions.rb
39
+ lib/rack/contrib/nested_params.rb
40
+ lib/rack/contrib/not_found.rb
41
+ lib/rack/contrib/post_body_content_type_parser.rb
42
+ lib/rack/contrib/proctitle.rb
43
+ lib/rack/contrib/profiler.rb
44
+ lib/rack/contrib/relative_redirect.rb
45
+ lib/rack/contrib/response_cache.rb
46
+ lib/rack/contrib/response_headers.rb
47
+ lib/rack/contrib/route_exceptions.rb
48
+ lib/rack/contrib/runtime.rb
49
+ lib/rack/contrib/sendfile.rb
50
+ lib/rack/contrib/signals.rb
51
+ lib/rack/contrib/simple_endpoint.rb
52
+ lib/rack/contrib/static_cache.rb
53
+ lib/rack/contrib/time_zone.rb
54
+ rack-contrib.gemspec
55
+ test/404.html
56
+ test/Maintenance.html
57
+ test/documents/test
58
+ test/mail_settings.rb
59
+ test/spec_rack_accept_format.rb
60
+ test/spec_rack_access.rb
61
+ test/spec_rack_backstage.rb
62
+ test/spec_rack_callbacks.rb
63
+ test/spec_rack_config.rb
64
+ test/spec_rack_contrib.rb
65
+ test/spec_rack_cookies.rb
66
+ test/spec_rack_csshttprequest.rb
67
+ test/spec_rack_deflect.rb
68
+ test/spec_rack_evil.rb
69
+ test/spec_rack_expectation_cascade.rb
70
+ test/spec_rack_garbagecollector.rb
71
+ test/spec_rack_host_meta.rb
72
+ test/spec_rack_jsonp.rb
73
+ test/spec_rack_lighttpd_script_name_fix.rb
74
+ test/spec_rack_mailexceptions.rb
75
+ test/spec_rack_nested_params.rb
76
+ test/spec_rack_not_found.rb
77
+ test/spec_rack_post_body_content_type_parser.rb
78
+ test/spec_rack_proctitle.rb
79
+ test/spec_rack_profiler.rb
80
+ test/spec_rack_relative_redirect.rb
81
+ test/spec_rack_response_cache.rb
82
+ test/spec_rack_response_headers.rb
83
+ test/spec_rack_runtime.rb
84
+ test/spec_rack_sendfile.rb
85
+ test/spec_rack_simple_endpoint.rb
86
+ test/spec_rack_static_cache.rb
87
+ test/statics/test
88
+ ]
89
+ # = MANIFEST =
90
+
91
+ s.test_files = s.files.select {|path| path =~ /^test\/spec_.*\.rb/}
92
+
93
+ s.extra_rdoc_files = %w[README.rdoc COPYING]
94
+ s.add_dependency 'rack', '>= 0.9.1'
95
+ s.add_development_dependency 'test-spec', '>= 0.9.0'
96
+ s.add_development_dependency 'tmail', '>= 1.2'
97
+ s.add_development_dependency 'json', '>= 1.1'
98
+
99
+ s.has_rdoc = true
100
+ s.homepage = "http://github.com/corntrace/rack-contrib/"
101
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "rack-contrib", "--main", "README"]
102
+ s.require_paths = %w[lib]
103
+ s.rubygems_version = '1.1.1'
104
+ end
data/test/404.html ADDED
@@ -0,0 +1 @@
1
+ Not Found
@@ -0,0 +1 @@
1
+ Under maintenance.
@@ -0,0 +1 @@
1
+ nocache
@@ -0,0 +1,12 @@
1
+ TEST_SMTP = nil
2
+
3
+ # Enable SMTP tests by providing the following for your SMTP server.
4
+ #
5
+ # TEST_SMTP = {
6
+ # :server => 'localhost',
7
+ # :domain => 'localhost',
8
+ # :port => 25,
9
+ # :authentication => :login,
10
+ # :user_name => nil,
11
+ # :password => nil
12
+ # }
@@ -0,0 +1,72 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/accept_format'
4
+ require 'rack/mime'
5
+
6
+ context "Rack::AcceptFormat" do
7
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, env['PATH_INFO']] }
8
+
9
+ specify "should do nothing when a format extension is already provided" do
10
+ request = Rack::MockRequest.env_for("/resource.json")
11
+ body = Rack::AcceptFormat.new(app).call(request).last
12
+ body.should == "/resource.json"
13
+ end
14
+
15
+ context "default extention" do
16
+ specify "should allow custom default" do
17
+ request = Rack::MockRequest.env_for("/resource")
18
+ body = Rack::AcceptFormat.new(app, '.xml').call(request).last
19
+ body.should == "/resource.xml"
20
+ end
21
+
22
+ specify "should default to html" do
23
+ request = Rack::MockRequest.env_for("/resource")
24
+ body = Rack::AcceptFormat.new(app).call(request).last
25
+ body.should == "/resource.html"
26
+ end
27
+
28
+ specify "should notmalize custom extention" do
29
+ request = Rack::MockRequest.env_for("/resource")
30
+
31
+ body = Rack::AcceptFormat.new(app,'xml').call(request).last #no dot prefix
32
+ body.should == "/resource.xml"
33
+
34
+ body = Rack::AcceptFormat.new(app, :xml).call(request).last
35
+ body.should == "/resource.xml"
36
+ end
37
+ end
38
+
39
+ context "there is no format extension" do
40
+ Rack::Mime::MIME_TYPES.clear
41
+
42
+ def mime(ext, type)
43
+ ext = ".#{ext}" unless ext.to_s[0] == ?.
44
+ Rack::Mime::MIME_TYPES[ext.to_s] = type
45
+ end
46
+
47
+ specify "should add the default extension if no Accept header" do
48
+ request = Rack::MockRequest.env_for("/resource")
49
+ body = Rack::AcceptFormat.new(app).call(request).last
50
+ body.should == "/resource.html"
51
+ end
52
+
53
+ specify "should add the default extension if the Accept header is not registered in the Mime::Types" do
54
+ request = Rack::MockRequest.env_for("/resource", 'HTTP_ACCEPT' => 'application/json;q=1.0, text/html;q=0.8, */*;q=0.1')
55
+ body = Rack::AcceptFormat.new(app).call(request).last
56
+ body.should == "/resource.html"
57
+ end
58
+
59
+ specify "should add the correct extension if the Accept header is registered in the Mime::Types" do
60
+ mime :json, 'application/json'
61
+ request = Rack::MockRequest.env_for("/resource", 'HTTP_ACCEPT' => 'application/json;q=1.0, text/html;q=0.8, */*;q=0.1')
62
+ body = Rack::AcceptFormat.new(app).call(request).last
63
+ body.should == "/resource.json"
64
+ end
65
+ end
66
+
67
+ specify "shouldn't confuse extention when there are dots in path" do
68
+ request = Rack::MockRequest.env_for("/parent.resource/resource")
69
+ body = Rack::AcceptFormat.new(app, '.html').call(request).last
70
+ body.should == "/parent.resource/resource.html"
71
+ end
72
+ end
@@ -0,0 +1,154 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/access'
4
+
5
+ context "Rack::Access" do
6
+
7
+ setup do
8
+ @app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['hello']] }
9
+ @mock_addr_1 = '111.111.111.111'
10
+ @mock_addr_2 = '192.168.1.222'
11
+ @mock_addr_localhost = '127.0.0.1'
12
+ @mock_addr_range = '192.168.1.0/24'
13
+ end
14
+
15
+ def mock_env(remote_addr, path = '/')
16
+ Rack::MockRequest.env_for(path, { 'REMOTE_ADDR' => remote_addr })
17
+ end
18
+
19
+ def middleware(options = {})
20
+ Rack::Access.new(@app, options)
21
+ end
22
+
23
+ specify "default configuration should deny non-local requests" do
24
+ app = middleware
25
+ status, headers, body = app.call(mock_env(@mock_addr_1))
26
+ status.should.equal 403
27
+ body.should.equal []
28
+ end
29
+
30
+ specify "default configuration should allow requests from 127.0.0.1" do
31
+ app = middleware
32
+ status, headers, body = app.call(mock_env(@mock_addr_localhost))
33
+ status.should.equal 200
34
+ body.should.equal ['hello']
35
+ end
36
+
37
+ specify "should allow remote addresses in allow_ipmasking" do
38
+ app = middleware('/' => [@mock_addr_1])
39
+ status, headers, body = app.call(mock_env(@mock_addr_1))
40
+ status.should.equal 200
41
+ body.should.equal ['hello']
42
+ end
43
+
44
+ specify "should deny remote addresses not in allow_ipmasks" do
45
+ app = middleware('/' => [@mock_addr_1])
46
+ status, headers, body = app.call(mock_env(@mock_addr_2))
47
+ status.should.equal 403
48
+ body.should.equal []
49
+ end
50
+
51
+ specify "should allow remote addresses in allow_ipmasks range" do
52
+ app = middleware('/' => [@mock_addr_range])
53
+ status, headers, body = app.call(mock_env(@mock_addr_2))
54
+ status.should.equal 200
55
+ body.should.equal ['hello']
56
+ end
57
+
58
+ specify "should deny remote addresses not in allow_ipmasks range" do
59
+ app = middleware('/' => [@mock_addr_range])
60
+ status, headers, body = app.call(mock_env(@mock_addr_1))
61
+ status.should.equal 403
62
+ body.should.equal []
63
+ end
64
+
65
+ specify "should allow remote addresses in one of allow_ipmasking" do
66
+ app = middleware('/' => [@mock_addr_range, @mock_addr_localhost])
67
+
68
+ status, headers, body = app.call(mock_env(@mock_addr_2))
69
+ status.should.equal 200
70
+ body.should.equal ['hello']
71
+
72
+ status, headers, body = app.call(mock_env(@mock_addr_localhost))
73
+ status.should.equal 200
74
+ body.should.equal ['hello']
75
+ end
76
+
77
+ specify "should deny remote addresses not in one of allow_ipmasks" do
78
+ app = middleware('/' => [@mock_addr_range, @mock_addr_localhost])
79
+ status, headers, body = app.call(mock_env(@mock_addr_1))
80
+ status.should.equal 403
81
+ body.should.equal []
82
+ end
83
+
84
+ specify "handles paths correctly" do
85
+ app = middleware({
86
+ 'http://foo.org/bar' => [@mock_addr_localhost],
87
+ '/foo' => [@mock_addr_localhost],
88
+ '/foo/bar' => [@mock_addr_range, @mock_addr_localhost]
89
+ })
90
+
91
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/"))
92
+ status.should.equal 200
93
+ body.should.equal ['hello']
94
+
95
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/qux"))
96
+ status.should.equal 200
97
+ body.should.equal ['hello']
98
+
99
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/foo"))
100
+ status.should.equal 403
101
+ body.should.equal []
102
+ status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo"))
103
+ status.should.equal 200
104
+ body.should.equal ['hello']
105
+
106
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/foo/"))
107
+ status.should.equal 403
108
+ body.should.equal []
109
+ status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo/"))
110
+ status.should.equal 200
111
+ body.should.equal ['hello']
112
+
113
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/foo/bar"))
114
+ status.should.equal 403
115
+ body.should.equal []
116
+ status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo/bar"))
117
+ status.should.equal 200
118
+ body.should.equal ['hello']
119
+ status, headers, body = app.call(mock_env(@mock_addr_2, "/foo/bar"))
120
+ status.should.equal 200
121
+ body.should.equal ['hello']
122
+
123
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/foo/bar/"))
124
+ status.should.equal 403
125
+ body.should.equal []
126
+ status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo/bar/"))
127
+ status.should.equal 200
128
+ body.should.equal ['hello']
129
+
130
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/foo///bar//quux"))
131
+ status.should.equal 403
132
+ body.should.equal []
133
+ status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo///bar//quux"))
134
+ status.should.equal 200
135
+ body.should.equal ['hello']
136
+
137
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/foo/quux"))
138
+ status.should.equal 403
139
+ body.should.equal []
140
+ status, headers, body = app.call(mock_env(@mock_addr_localhost, "/foo/quux"))
141
+ status.should.equal 200
142
+ body.should.equal ['hello']
143
+
144
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/bar"))
145
+ status.should.equal 200
146
+ body.should.equal ['hello']
147
+ status, headers, body = app.call(mock_env(@mock_addr_1, "/bar").merge('HTTP_HOST' => 'foo.org'))
148
+ status.should.equal 403
149
+ body.should.equal []
150
+ status, headers, body = app.call(mock_env(@mock_addr_localhost, "/bar").merge('HTTP_HOST' => 'foo.org'))
151
+ status.should.equal 200
152
+ body.should.equal ['hello']
153
+ end
154
+ end
@@ -0,0 +1,26 @@
1
+ require 'test/spec'
2
+ require 'rack/builder'
3
+ require 'rack/mock'
4
+ require 'rack/contrib/backstage'
5
+
6
+ context "Rack::Backstage" do
7
+ specify "shows maintenances page if present" do
8
+ app = Rack::Builder.new do
9
+ use Rack::Backstage, 'test/Maintenance.html'
10
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
11
+ end
12
+ response = Rack::MockRequest.new(app).get('/')
13
+ response.body.should.equal('Under maintenance.')
14
+ response.status.should.equal(503)
15
+ end
16
+
17
+ specify "passes on request if page is not present" do
18
+ app = Rack::Builder.new do
19
+ use Rack::Backstage, 'test/Nonsense.html'
20
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
21
+ end
22
+ response = Rack::MockRequest.new(app).get('/')
23
+ response.body.should.equal('Hello, World!')
24
+ response.status.should.equal(200)
25
+ end
26
+ end
@@ -0,0 +1,65 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+
4
+ class Flame
5
+ def call(env)
6
+ env['flame'] = 'F Lifo..'
7
+ end
8
+ end
9
+
10
+ class Pacify
11
+ def initialize(with)
12
+ @with = with
13
+ end
14
+
15
+ def call(env)
16
+ env['peace'] = @with
17
+ end
18
+ end
19
+
20
+ class Finale
21
+ def call(response)
22
+ status, headers, body = response
23
+
24
+ headers['last'] = 'Finale'
25
+ $old_status = status
26
+
27
+ [201, headers, body]
28
+ end
29
+ end
30
+
31
+ class TheEnd
32
+ def call(response)
33
+ status, headers, body = response
34
+
35
+ headers['last'] = 'TheEnd'
36
+ [201, headers, body]
37
+ end
38
+ end
39
+
40
+ context "Rack::Callbacks" do
41
+ specify "works for love and small stack trace" do
42
+ callback_app = Rack::Callbacks.new do
43
+ before Flame
44
+ before Pacify, "with love"
45
+
46
+ run lambda {|env| [200, {}, [env['flame'], env['peace']]] }
47
+
48
+ after Finale
49
+ after TheEnd
50
+ end
51
+
52
+ app = Rack::Builder.new do
53
+ run callback_app
54
+ end.to_app
55
+
56
+ response = Rack::MockRequest.new(app).get("/")
57
+
58
+ response.body.should.equal 'F Lifo..with love'
59
+
60
+ $old_status.should.equal 200
61
+ response.status.should.equal 201
62
+
63
+ response.headers['last'].should.equal 'TheEnd'
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/config'
4
+
5
+ context "Rack::Config" do
6
+
7
+ specify "should accept a block that modifies the environment" do
8
+ app = Rack::Builder.new do
9
+ use Rack::Lint
10
+ use Rack::ContentLength
11
+ use Rack::Config do |env|
12
+ env['greeting'] = 'hello'
13
+ end
14
+ run lambda { |env|
15
+ [200, {'Content-Type' => 'text/plain'}, [env['greeting'] || '']]
16
+ }
17
+ end
18
+ response = Rack::MockRequest.new(app).get('/')
19
+ response.body.should.equal('hello')
20
+ end
21
+
22
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/spec'
2
+ require 'rack/contrib'
3
+
4
+ context "Rack::Contrib" do
5
+ specify "should expose release" do
6
+ Rack::Contrib.should.respond_to :release
7
+ end
8
+ end
@@ -0,0 +1,56 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+ require 'rack/contrib/cookies'
4
+
5
+ context "Rack::Cookies" do
6
+ specify "should be able to read received cookies" do
7
+ app = lambda { |env|
8
+ cookies = env['rack.cookies']
9
+ foo, quux = cookies[:foo], cookies['quux']
10
+ [200, {'Content-Type' => 'text/plain'}, ["foo: #{foo}, quux: #{quux}"]]
11
+ }
12
+ app = Rack::Cookies.new(app)
13
+
14
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_COOKIE' => 'foo=bar;quux=h&m')
15
+ response.body.should.equal('foo: bar, quux: h&m')
16
+ end
17
+
18
+ specify "should be able to set new cookies" do
19
+ app = lambda { |env|
20
+ cookies = env['rack.cookies']
21
+ cookies[:foo] = 'bar'
22
+ cookies['quux'] = 'h&m'
23
+ [200, {'Content-Type' => 'text/plain'}, []]
24
+ }
25
+ app = Rack::Cookies.new(app)
26
+
27
+ response = Rack::MockRequest.new(app).get('/')
28
+ response.headers['Set-Cookie'].should.equal("quux=h%26m; path=/\nfoo=bar; path=/")
29
+ end
30
+
31
+ specify "should be able to set cookie with options" do
32
+ app = lambda { |env|
33
+ cookies = env['rack.cookies']
34
+ cookies['foo'] = { :value => 'bar', :path => '/login', :secure => true }
35
+ [200, {'Content-Type' => 'text/plain'}, []]
36
+ }
37
+ app = Rack::Cookies.new(app)
38
+
39
+ response = Rack::MockRequest.new(app).get('/')
40
+ response.headers['Set-Cookie'].should.equal('foo=bar; path=/login; secure')
41
+ end
42
+
43
+ specify "should be able to delete received cookies" do
44
+ app = lambda { |env|
45
+ cookies = env['rack.cookies']
46
+ cookies.delete(:foo)
47
+ foo, quux = cookies['foo'], cookies[:quux]
48
+ [200, {'Content-Type' => 'text/plain'}, ["foo: #{foo}, quux: #{quux}"]]
49
+ }
50
+ app = Rack::Cookies.new(app)
51
+
52
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_COOKIE' => 'foo=bar;quux=h&m')
53
+ response.body.should.equal('foo: , quux: h&m')
54
+ response.headers['Set-Cookie'].should.equal('foo=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT')
55
+ end
56
+ end
@@ -0,0 +1,66 @@
1
+ require 'test/spec'
2
+ require 'rack/mock'
3
+
4
+ begin
5
+ require 'csshttprequest'
6
+ require 'rack/contrib/csshttprequest'
7
+
8
+ context "Rack::CSSHTTPRequest" do
9
+
10
+ before(:each) do
11
+ @test_body = '{"bar":"foo"}'
12
+ @test_headers = {'Content-Type' => 'text/plain'}
13
+ @encoded_body = CSSHTTPRequest.encode(@test_body)
14
+ @app = lambda { |env| [200, @test_headers, [@test_body]] }
15
+ end
16
+
17
+ specify "env['csshttprequest.chr'] should be set to true when \
18
+ PATH_INFO ends with '.chr'" do
19
+ request = Rack::MockRequest.env_for("/blah.chr", :lint => true, :fatal => true)
20
+ Rack::CSSHTTPRequest.new(@app).call(request)
21
+ request['csshttprequest.chr'].should.equal true
22
+ end
23
+
24
+ specify "env['csshttprequest.chr'] should be set to true when \
25
+ request parameter _format == 'chr'" do
26
+ request = Rack::MockRequest.env_for("/?_format=chr", :lint => true, :fatal => true)
27
+ Rack::CSSHTTPRequest.new(@app).call(request)
28
+ request['csshttprequest.chr'].should.equal true
29
+ end
30
+
31
+ specify "should not change the headers or response when !env['csshttprequest.chr']" do
32
+ request = Rack::MockRequest.env_for("/", :lint => true, :fatal => true)
33
+ status, headers, response = Rack::CSSHTTPRequest.new(@app).call(request)
34
+ headers.should.equal @test_headers
35
+ response.join.should.equal @test_body
36
+ end
37
+
38
+ context "when env['csshttprequest.chr']" do
39
+ before(:each) do
40
+ @request = Rack::MockRequest.env_for("/",
41
+ 'csshttprequest.chr' => true, :lint => true, :fatal => true)
42
+ end
43
+
44
+ specify "should modify the content length to the correct value" do
45
+ headers = Rack::CSSHTTPRequest.new(@app).call(@request)[1]
46
+ headers['Content-Length'].should.equal @encoded_body.length.to_s
47
+ end
48
+
49
+ specify "should modify the content type to the correct value" do
50
+ headers = Rack::CSSHTTPRequest.new(@app).call(@request)[1]
51
+ headers['Content-Type'].should.equal 'text/css'
52
+ end
53
+
54
+ specify "should not modify any other headers" do
55
+ headers = Rack::CSSHTTPRequest.new(@app).call(@request)[1]
56
+ headers.should.equal @test_headers.merge({
57
+ 'Content-Type' => 'text/css',
58
+ 'Content-Length' => @encoded_body.length.to_s
59
+ })
60
+ end
61
+ end
62
+
63
+ end
64
+ rescue LoadError => boom
65
+ STDERR.puts "WARN: Skipping Rack::CSSHTTPRequest tests (nbio-csshttprequest not installed)"
66
+ end