rack-robustness 1.0.0 → 1.2.0

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.
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+ describe Rack::Robustness, 'last resort' do
3
+ include Rack::Test::Methods
4
+
5
+ before do
6
+ $seen_ex = nil
7
+ end
8
+
9
+ context 'when the response cannot be built and no catch all' do
10
+ let(:app){
11
+ mock_app do |g|
12
+ g.no_catch_all
13
+ g.response{|ex| NoSuchResponseClass.new }
14
+ g.ensure(true){|ex| $seen_ex = ex }
15
+ end
16
+ }
17
+
18
+ it 'reraises the internal error' do
19
+ lambda{
20
+ get '/argument-error'
21
+ }.should raise_error(NameError, /NoSuchResponseClass/)
22
+ end
23
+
24
+ it 'passes into the ensure block with the original error' do
25
+ lambda{
26
+ get '/argument-error'
27
+ }.should raise_error(NameError, /NoSuchResponseClass/)
28
+ expect($seen_ex).to be_a(ArgumentError)
29
+ end
30
+ end
31
+
32
+ context 'when the response cannot be built and catch all' do
33
+ let(:app){
34
+ mock_app do |g|
35
+ g.response{|ex| NoSuchResponseClass.new }
36
+ end
37
+ }
38
+
39
+ it 'falls back to last resort response' do
40
+ get '/argument-error'
41
+ expect(last_response.status).to eq(500)
42
+ expect(last_response.content_type).to eq("text/plain")
43
+ expect(last_response.body).to eq("An internal error occured, sorry for the disagreement.")
44
+ end
45
+ end
46
+
47
+ context 'when an ensure block raises an error and no catch all' do
48
+ let(:app){
49
+ mock_app do |g|
50
+ g.no_catch_all
51
+ g.ensure{|ex| NoSuchResponseClass.new }
52
+ end
53
+ }
54
+
55
+ it 'reraises the internal error' do
56
+ lambda{
57
+ get '/argument-error'
58
+ }.should raise_error(NameError, /NoSuchResponseClass/)
59
+ end
60
+ end
61
+
62
+ context 'when an ensure block raises an error and catch all' do
63
+ let(:app){
64
+ mock_app do |g|
65
+ g.ensure{|ex| NoSuchResponseClass.new }
66
+ end
67
+ }
68
+
69
+ it 'reraises the internal error' do
70
+ get '/argument-error'
71
+ expect(last_response.status).to eq(500)
72
+ expect(last_response.content_type).to eq("text/plain")
73
+ expect(last_response.body).to eq("An internal error occured, sorry for the disagreement.")
74
+ end
75
+ end
76
+
77
+ context 'when the response block fails and the ensure block uses the response object' do
78
+ let(:app){
79
+ mock_app do |g|
80
+ g.response{|ex| NoSuchResponseClass.new }
81
+ g.ensure{|ex| $seen_response = response }
82
+ end
83
+ }
84
+
85
+ before do
86
+ $seen_response = nil
87
+ end
88
+
89
+ it 'sets a default response object for the ensure clause' do
90
+ get '/argument-error'
91
+ expect(last_response.status).to eq(500)
92
+ expect(last_response.content_type).to eq("text/plain")
93
+ expect(last_response.body).to eq("An internal error occured, sorry for the disagreement.")
94
+ expect($seen_response).to_not be_nil
95
+ end
96
+ end
97
+
98
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ describe Rack::Robustness, 'rescue' do
3
+ include Rack::Test::Methods
4
+
5
+ let(:app){
6
+ mock_app do |g|
7
+ g.status 400
8
+ g.rescue(ArgumentError){|ex| 'argument-error' }
9
+ g.rescue(SecurityError, 'security-error')
10
+ g.on(TypeError) {|ex| 'type-error' }
11
+ end
12
+ }
13
+
14
+ it 'correctly rescues specified errors' do
15
+ get '/argument-error'
16
+ expect(last_response.status).to eq(400)
17
+ expect(last_response.body).to eq("argument-error")
18
+ end
19
+
20
+ it 'correctly support a non-block shortcut' do
21
+ get '/security-error'
22
+ expect(last_response.status).to eq(400)
23
+ expect(last_response.body).to eq("security-error")
24
+ end
25
+
26
+ it 'is has a `on` alias' do
27
+ get '/type-error'
28
+ expect(last_response.status).to eq(400)
29
+ expect(last_response.body).to eq("type-error")
30
+ end
31
+
32
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ describe Rack::Robustness, 'response' do
3
+ include Rack::Test::Methods
4
+
5
+ class MyFooResponse < Rack::Response
6
+
7
+ def initialize(*args)
8
+ super
9
+ self['Content-Type'] = "application/json"
10
+ end
11
+
12
+ def each
13
+ yield("response text")
14
+ end
15
+
16
+ end
17
+
18
+ let(:app){
19
+ mock_app do |g|
20
+ g.status 400
21
+ g.response{|ex| MyFooResponse.new }
22
+ end
23
+ }
24
+
25
+ it 'correctly sets the status' do
26
+ get '/argument-error'
27
+ expect(last_response.status).to eq(400)
28
+ end
29
+
30
+ xit 'correctly sets the body' do
31
+ get '/argument-error'
32
+ expect(last_response.body).to eq("response text")
33
+ end
34
+
35
+ it 'correctly sets the content type' do
36
+ get '/argument-error'
37
+ expect(last_response.content_type).to eq("application/json")
38
+ end
39
+
40
+ end
@@ -2,28 +2,13 @@ require 'spec_helper'
2
2
  describe Rack::Robustness do
3
3
  include Rack::Test::Methods
4
4
 
5
- def mock_app(&bl)
6
- Rack::Builder.new do
7
- use Rack::Robustness, &bl
8
- map '/happy' do
9
- run lambda{|env| [200, {'Content-Type' => 'text/plain'}, ['happy']]}
10
- end
11
- map "/argument-error" do
12
- run lambda{|env| raise ArgumentError, "an argument error" }
13
- end
14
- map "/type-error" do
15
- run lambda{|env| raise TypeError, "a type error" }
16
- end
17
- end
18
- end
19
-
20
5
  shared_examples_for 'A transparent middleware for happy paths' do
21
6
 
22
7
  it 'let happy responses unchanged' do
23
8
  get '/happy'
24
- last_response.status.should eq(200)
25
- last_response.content_type.should eq('text/plain')
26
- last_response.body.should eq('happy')
9
+ expect(last_response.status).to eq(200)
10
+ expect(last_response.content_type).to eq('text/plain')
11
+ expect(last_response.body).to eq('happy')
27
12
  end
28
13
  end
29
14
 
@@ -36,9 +21,16 @@ describe Rack::Robustness do
36
21
 
37
22
  it 'set a status 500 with a standard error message by default' do
38
23
  get '/argument-error'
39
- last_response.status.should eq(500)
40
- last_response.content_type.should eq("text/plain")
41
- last_response.body.should eq("Sorry, a fatal error occured.")
24
+ expect(last_response.status).to eq(500)
25
+ expect(last_response.content_type).to eq("text/plain")
26
+ expect(last_response.body).to eq("Sorry, a fatal error occured.")
27
+ end
28
+
29
+ it 'catches all exceptions by default' do
30
+ get '/security-error'
31
+ expect(last_response.status).to eq(500)
32
+ expect(last_response.content_type).to eq("text/plain")
33
+ expect(last_response.body).to eq("Sorry, a fatal error occured.")
42
34
  end
43
35
  end
44
36
 
@@ -55,9 +47,9 @@ describe Rack::Robustness do
55
47
 
56
48
  it 'set the specified status and body on errors' do
57
49
  get '/argument-error'
58
- last_response.status.should eq(501)
59
- last_response.content_type.should eq("text/test")
60
- last_response.body.should eq("An error occured")
50
+ expect(last_response.status).to eq(501)
51
+ expect(last_response.content_type).to eq("text/test")
52
+ expect(last_response.body).to eq("An error occured")
61
53
  end
62
54
  end
63
55
 
@@ -74,7 +66,7 @@ describe Rack::Robustness do
74
66
  it 'set the specified headers on error' do
75
67
  get '/argument-error'
76
68
  last_response.headers['Foo'].should eq('Bar')
77
- last_response.content_type.should eq("text/test")
69
+ expect(last_response.content_type).to eq("text/test")
78
70
  end
79
71
  end
80
72
 
@@ -91,16 +83,16 @@ describe Rack::Robustness do
91
83
 
92
84
  it 'correctly sets the status, content_type and body on ArgumentError' do
93
85
  get '/argument-error'
94
- last_response.status.should eq(400)
95
- last_response.content_type.should eq('text/arg')
96
- last_response.body.should eq('an argument error')
86
+ expect(last_response.status).to eq(400)
87
+ expect(last_response.content_type).to eq('text/arg')
88
+ expect(last_response.body).to eq('an argument error')
97
89
  end
98
90
 
99
91
  it 'correctly sets the status, content_type and body on TypeError' do
100
92
  get '/type-error'
101
- last_response.status.should eq(500)
102
- last_response.content_type.should eq('text/other')
103
- last_response.body.should eq('a type error')
93
+ expect(last_response.status).to eq(500)
94
+ expect(last_response.content_type).to eq('text/other')
95
+ expect(last_response.body).to eq('a type error')
104
96
  end
105
97
  end
106
98
 
@@ -117,12 +109,12 @@ describe Rack::Robustness do
117
109
 
118
110
  it 'correctly sets the specified headers on an ArgumentError' do
119
111
  get '/argument-error'
120
- last_response.content_type.should eq("text/arg")
112
+ expect(last_response.content_type).to eq("text/arg")
121
113
  end
122
114
 
123
115
  it 'correctly sets the specified headers on a TypeError' do
124
116
  get '/type-error'
125
- last_response.content_type.should eq("text/other")
117
+ expect(last_response.content_type).to eq("text/other")
126
118
  end
127
119
  end
128
120
 
@@ -139,13 +131,13 @@ describe Rack::Robustness do
139
131
  it 'correctly sets the specified headers on an ArgumentError' do
140
132
  get '/argument-error'
141
133
  last_response.headers['Foo'].should eq('Bar')
142
- last_response.content_type.should eq("text/arg")
134
+ expect(last_response.content_type).to eq("text/arg")
143
135
  end
144
136
 
145
137
  it 'correctly sets the specified headers on a TypeError' do
146
138
  get '/type-error'
147
139
  last_response.headers['Foo'].should eq('Bar')
148
- last_response.content_type.should eq("text/other")
140
+ expect(last_response.content_type).to eq("text/other")
149
141
  end
150
142
  end
151
143
 
@@ -165,16 +157,16 @@ describe Rack::Robustness do
165
157
 
166
158
  it 'uses the response on ArgumentError' do
167
159
  get '/argument-error'
168
- last_response.status.should eq(401)
169
- last_response.content_type.should eq('text/arg')
170
- last_response.body.should eq("an argument error")
160
+ expect(last_response.status).to eq(401)
161
+ expect(last_response.content_type).to eq('text/arg')
162
+ expect(last_response.body).to eq("an argument error")
171
163
  end
172
164
 
173
165
  it 'uses the response on TypeError' do
174
166
  get '/type-error'
175
- last_response.status.should eq(402)
176
- last_response.content_type.should eq('default/one')
177
- last_response.body.should eq("a type error")
167
+ expect(last_response.status).to eq(402)
168
+ expect(last_response.content_type).to eq('default/one')
169
+ expect(last_response.body).to eq("a type error")
178
170
  end
179
171
  end
180
172
 
@@ -187,9 +179,9 @@ describe Rack::Robustness do
187
179
 
188
180
  it 'uses the status and fallback to defaults for the rest' do
189
181
  get '/argument-error'
190
- last_response.status.should eq(401)
191
- last_response.content_type.should eq('text/plain')
192
- last_response.body.should eq("Sorry, a fatal error occured.")
182
+ expect(last_response.status).to eq(401)
183
+ expect(last_response.content_type).to eq('text/plain')
184
+ expect(last_response.body).to eq("Sorry, a fatal error occured.")
193
185
  end
194
186
  end
195
187
 
@@ -202,9 +194,9 @@ describe Rack::Robustness do
202
194
 
203
195
  it 'uses it as body and fallback to defaults for the rest' do
204
196
  get '/argument-error'
205
- last_response.status.should eq(500)
206
- last_response.content_type.should eq('text/plain')
207
- last_response.body.should eq("an argument error")
197
+ expect(last_response.status).to eq(500)
198
+ expect(last_response.content_type).to eq('text/plain')
199
+ expect(last_response.body).to eq("an argument error")
208
200
  end
209
201
  end
210
202
 
@@ -218,7 +210,7 @@ describe Rack::Robustness do
218
210
 
219
211
  it 'matches known errors' do
220
212
  get '/argument-error'
221
- last_response.status.should eq(401)
213
+ expect(last_response.status).to eq(401)
222
214
  end
223
215
 
224
216
  it 'raises on unknown error' do
@@ -239,8 +231,8 @@ describe Rack::Robustness do
239
231
 
240
232
  it 'matches known errors' do
241
233
  get '/argument-error'
242
- last_response.status.should eq(401)
243
- last_response.body.should eq("Sorry, a fatal error occured.")
234
+ expect(last_response.status).to eq(401)
235
+ expect(last_response.body).to eq("Sorry, a fatal error occured.")
244
236
  end
245
237
 
246
238
  it 'raises on unknown error' do
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ describe "Rack::Robustness subclasses" do
3
+ include Rack::Test::Methods
4
+
5
+ class Shield < Rack::Robustness
6
+ self.body{|ex| ex.message }
7
+ self.rescue(ArgumentError){|ex| 400 }
8
+ end
9
+
10
+ let(:app){
11
+ mock_app(Shield)
12
+ }
13
+
14
+ it 'works as expected' do
15
+ get '/argument-error'
16
+ expect(last_response.status).to eq(400)
17
+ expect(last_response.body).to eq("an argument error")
18
+ end
19
+
20
+ end
data/tasks/gem.rake CHANGED
@@ -1,73 +1,39 @@
1
- # Installs rake tasks for gemming and packaging
2
- #
3
- # This file installs the 'rake package', 'rake gem' tasks and associates
4
- # (clobber_package, repackage, ...). It is automatically generated by Noe
5
- # from your .noespec file, and should therefore be configured there, under
6
- # the variables/rake_tasks/gem entry, as illustrated below:
7
- #
8
- # variables:
9
- # rake_tasks:
10
- # gem:
11
- # package_dir: pkg
12
- # need_tar: false
13
- # need_tar_gz: false
14
- # need_tar_bz2: false
15
- # need_zip: false
16
- # ...
17
- #
18
- # If you have specific needs requiring manual intervention on this file,
19
- # don't forget to set safe-override to false in your noe specification:
20
- #
21
- # template-info:
22
- # manifest:
23
- # tasks/gem.rake:
24
- # safe-override: false
25
- #
26
- begin
27
- require 'rubygems/package_task'
1
+ require 'rubygems/package_task'
28
2
 
29
- # Dynamically load the gem spec
30
- gemspec_file = File.expand_path('../../rack-robustness.gemspec', __FILE__)
31
- gemspec = Kernel.eval(File.read(gemspec_file))
3
+ # Dynamically load the gem spec
4
+ gemspec_file = File.expand_path('../../rack-robustness.gemspec', __FILE__)
5
+ gemspec = Kernel.eval(File.read(gemspec_file))
32
6
 
33
- Gem::PackageTask.new(gemspec) do |t|
7
+ Gem::PackageTask.new(gemspec) do |t|
34
8
 
35
- # Name of the package
36
- t.name = gemspec.name
9
+ # Name of the package
10
+ t.name = gemspec.name
37
11
 
38
- # Version of the package
39
- t.version = gemspec.version
12
+ # Version of the package
13
+ t.version = gemspec.version
40
14
 
41
- # Directory used to store the package files
42
- t.package_dir = "pkg"
15
+ # Directory used to store the package files
16
+ t.package_dir = "pkg"
43
17
 
44
- # True if a gzipped tar file (tgz) should be produced
45
- t.need_tar = false
18
+ # True if a gzipped tar file (tgz) should be produced
19
+ t.need_tar = false
46
20
 
47
- # True if a gzipped tar file (tar.gz) should be produced
48
- t.need_tar_gz = false
21
+ # True if a gzipped tar file (tar.gz) should be produced
22
+ t.need_tar_gz = false
49
23
 
50
- # True if a bzip2'd tar file (tar.bz2) should be produced
51
- t.need_tar_bz2 = false
24
+ # True if a bzip2'd tar file (tar.bz2) should be produced
25
+ t.need_tar_bz2 = false
52
26
 
53
- # True if a zip file should be produced (default is false)
54
- t.need_zip = false
27
+ # True if a zip file should be produced (default is false)
28
+ t.need_zip = false
55
29
 
56
- # List of files to be included in the package.
57
- t.package_files = gemspec.files
30
+ # List of files to be included in the package.
31
+ t.package_files = gemspec.files
58
32
 
59
- # Tar command for gzipped or bzip2ed archives.
60
- t.tar_command = "tar"
33
+ # Tar command for gzipped or bzip2ed archives.
34
+ t.tar_command = "tar"
61
35
 
62
- # Zip command for zipped archives.
63
- t.zip_command = "zip"
36
+ # Zip command for zipped archives.
37
+ t.zip_command = "zip"
64
38
 
65
- end
66
- rescue LoadError
67
- task :gem do
68
- abort 'rubygems/package_task is not available. You should verify your rubygems installation'
69
- end
70
- task :package do
71
- abort 'rubygems/package_task is not available. You should verify your rubygems installation'
72
- end
73
39
  end
data/tasks/test.rake ADDED
@@ -0,0 +1,11 @@
1
+ namespace :test do
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc %q{Run all RSpec tests}
5
+ RSpec::Core::RakeTask.new(:unit) do |t|
6
+ t.rspec_opts = %w[-I. -Ilib -Ispec --pattern=spec/**/test_*.rb --color .]
7
+ end
8
+
9
+ task :all => :"unit"
10
+ end
11
+ task :test => :"test:all"
metadata CHANGED
@@ -1,78 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-robustness
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 1.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Bernard Lambeau
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-26 00:00:00.000000000 Z
11
+ date: 2023-06-09 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '10.0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '10.0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: '2.12'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: '2.12'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rack
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: '1.5'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: '1.5'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rack-test
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ~>
59
+ - - "~>"
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0.6'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ~>
66
+ - - "~>"
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0.6'
78
69
  description: Rack::Robustness provides you with an easy way to handle errors in your
@@ -86,46 +77,53 @@ extra_rdoc_files:
86
77
  - CHANGELOG.md
87
78
  - LICENCE.md
88
79
  files:
89
- - rack-robustness.gemspec
90
80
  - CHANGELOG.md
91
81
  - Gemfile
92
82
  - Gemfile.lock
93
- - lib/rack/robustness.rb
94
83
  - LICENCE.md
95
84
  - Manifest.txt
96
- - Rakefile
97
85
  - README.md
86
+ - Rakefile
87
+ - lib/rack/robustness.rb
88
+ - rack-robustness.gemspec
98
89
  - spec/spec_helper.rb
90
+ - spec/test_context.rb
91
+ - spec/test_ensure.rb
92
+ - spec/test_last_resort.rb
93
+ - spec/test_rescue.rb
94
+ - spec/test_response.rb
99
95
  - spec/test_robustness.rb
96
+ - spec/test_subclass.rb
100
97
  - tasks/gem.rake
101
- - tasks/spec_test.rake
98
+ - tasks/test.rake
102
99
  homepage: https://github.com/blambeau/rack-robustness
103
100
  licenses: []
101
+ metadata: {}
104
102
  post_install_message:
105
103
  rdoc_options: []
106
104
  require_paths:
107
105
  - lib
108
106
  required_ruby_version: !ruby/object:Gem::Requirement
109
- none: false
110
107
  requirements:
111
- - - ! '>='
108
+ - - ">="
112
109
  - !ruby/object:Gem::Version
113
110
  version: '0'
114
- segments:
115
- - 0
116
- hash: 2934394792950840478
117
111
  required_rubygems_version: !ruby/object:Gem::Requirement
118
- none: false
119
112
  requirements:
120
- - - ! '>='
113
+ - - ">="
121
114
  - !ruby/object:Gem::Version
122
115
  version: '0'
123
116
  requirements: []
124
- rubyforge_project:
125
- rubygems_version: 1.8.24
117
+ rubygems_version: 3.1.6
126
118
  signing_key:
127
- specification_version: 3
119
+ specification_version: 4
128
120
  summary: Rack::Robustness, the rescue clause of your Rack stack.
129
121
  test_files:
130
- - spec/spec_helper.rb
122
+ - spec/test_response.rb
123
+ - spec/test_rescue.rb
131
124
  - spec/test_robustness.rb
125
+ - spec/test_subclass.rb
126
+ - spec/test_ensure.rb
127
+ - spec/test_context.rb
128
+ - spec/spec_helper.rb
129
+ - spec/test_last_resort.rb