rack-berater 0.3.1 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b726fd0709ef54111d4e767fbe040ad3ab05fd0b0426bae20c67ef41f6fc869
4
- data.tar.gz: 666a443527911b61660208f5e6ebf09b2a7adf298c67cb157665988c32868623
3
+ metadata.gz: d9e1b9ca0c696948b6d7ec973e1855c71a73fe9ca59d0833107e57295d2d9f21
4
+ data.tar.gz: fcbbeef6465c6f7cefa0c110401e4c80d6462ea3adc69a7a1e3975e19e9f8355
5
5
  SHA512:
6
- metadata.gz: f5bfe591019a79137181785ae76f6e5603b3495cc5ef8e207eab1be88bec114988bbe190509752e093aa1458ecd2ce11dd91dcd4a31fe96b9c00e94f62a931ea
7
- data.tar.gz: a83f26c06eeeb1c570233b1b84bdec1dadf501e8774a388290dd6c733fd2c095435f86974b4f27216ffbd118e261206097ac3f16187dfeb2f2da8eb03adb9657
6
+ metadata.gz: 6d00a14d8136ac96cac236c4bb2ff55c98871dc32cb85b30d8800b4784526cb27c53e54979703f554cb1698ea99093ea77584222dab213f4cbbbe6aeba1b2e71
7
+ data.tar.gz: 03b030982e003903461f929853138f7c4dcbffbfbcccb2aba1335ffac6e2aa9aeb0b265c4c13d3ebc6f1e04ef4acc1c942606c9d57f44cf2003c3a46e42b8c64
@@ -48,34 +48,16 @@ module Rack
48
48
  Thread.current[ENV_KEY] = priority
49
49
  end
50
50
 
51
- @@cache_prefix = 'Berater:Rack:Prioritizer'
52
51
  def cache_key_for(env)
53
- req = Rack::Request.new(env)
54
-
55
- req_method = env[Rack::REQUEST_METHOD].downcase
56
- path = ''
57
-
58
- if defined?(Rails) && Rails.respond_to?(:application) && Rails.application
59
- res = Rails.application.routes.recognize_path(
60
- env[Rack::PATH_INFO],
61
- method: env[Rack::REQUEST_METHOD],
62
- )
63
- path = res.values_at(:controller, :action).compact.join('#')
64
- end
65
-
66
- if path.empty?
67
- path = env['PATH_INFO'].gsub(%r{/[0-9]+(/|$)}, '/x\1')
68
- end
69
-
70
52
  [
71
- @@cache_prefix,
72
- req_method,
73
- path,
53
+ env[Rack::REQUEST_METHOD].downcase,
54
+
55
+ # normalize RESTful paths
56
+ env['PATH_INFO'].gsub(%r{/[0-9]+(/|$)}, '/x\1'),
74
57
  ].join(':')
75
58
  end
76
59
 
77
60
  @@cache = {}
78
-
79
61
  def cache_get(key)
80
62
  synchronize { @@cache[key] }
81
63
  end
@@ -0,0 +1,17 @@
1
+ require 'action_controller/metal'
2
+ require 'action_dispatch'
3
+
4
+ module Rack
5
+ class Berater
6
+ class RailsPrioritizer < Prioritizer
7
+ def cache_key_for(env)
8
+ Rails.application.routes.recognize_path(
9
+ env[Rack::PATH_INFO],
10
+ method: env[Rack::REQUEST_METHOD],
11
+ ).values_at(:controller, :action).compact.join('#')
12
+ rescue ActionController::RoutingError
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
@@ -5,7 +5,7 @@ module Rack
5
5
  class Railtie < Rails::Railtie
6
6
  initializer 'rack.berater' do |app|
7
7
  if ::Berater.middleware.include?(::Berater::Middleware::LoadShedder)
8
- app.middleware.use Rack::Berater::Prioritizer
8
+ app.middleware.use Rack::Berater::RailsPrioritizer
9
9
  end
10
10
 
11
11
  app.middleware.use Rack::Berater
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Berater
3
- VERSION = "0.3.1"
3
+ VERSION = "0.3.2"
4
4
  end
5
5
  end
data/lib/rack/berater.rb CHANGED
@@ -6,6 +6,7 @@ require 'set'
6
6
  module Rack
7
7
  class Berater
8
8
  autoload :Prioritizer, 'rack/berater/prioritizer'
9
+ autoload :RailsPrioritizer, 'rack/berater/rails_prioritizer'
9
10
  autoload :Railtie, 'rack/berater/railtie'
10
11
 
11
12
  ERRORS = Set[ ::Berater::Overloaded ]
@@ -117,12 +117,8 @@ describe Rack::Berater::Prioritizer do
117
117
  context 'with a basic env' do
118
118
  let(:env) { Rack::MockRequest.env_for('/') }
119
119
 
120
- it 'has a Berater prefix' do
121
- is_expected.to match /^Berater:/
122
- end
123
-
124
120
  it 'combines the verb and path' do
125
- is_expected.to match %r{:get:/$}
121
+ is_expected.to match %r{get:/$}
126
122
  end
127
123
  end
128
124
 
@@ -130,7 +126,7 @@ describe Rack::Berater::Prioritizer do
130
126
  let(:env) { Rack::MockRequest.env_for('/', method: 'PUT') }
131
127
 
132
128
  it 'combines the verb and path' do
133
- is_expected.to match %r{:put:/$}
129
+ is_expected.to match %r{put:/$}
134
130
  end
135
131
  end
136
132
 
@@ -138,7 +134,7 @@ describe Rack::Berater::Prioritizer do
138
134
  let(:env) { Rack::MockRequest.env_for('/user/123') }
139
135
 
140
136
  it 'normalizes the id' do
141
- is_expected.to match %r{:/user/x$}
137
+ is_expected.to match %r{get:/user/x$}
142
138
  end
143
139
  end
144
140
 
@@ -146,7 +142,7 @@ describe Rack::Berater::Prioritizer do
146
142
  let(:env) { Rack::MockRequest.env_for('/user/123/') }
147
143
 
148
144
  it 'normalizes the id and keeps the trailing slash' do
149
- is_expected.to match %r{:/user/x/$}
145
+ is_expected.to match %r{get:/user/x/$}
150
146
  end
151
147
  end
152
148
 
@@ -154,7 +150,7 @@ describe Rack::Berater::Prioritizer do
154
150
  let(:env) { Rack::MockRequest.env_for('/user/123/friend/456') }
155
151
 
156
152
  it 'normalizes both ids' do
157
- is_expected.to match %r{:/user/x/friend/x$}
153
+ is_expected.to match %r{get:/user/x/friend/x$}
158
154
  end
159
155
  end
160
156
  end
@@ -219,77 +215,4 @@ describe Rack::Berater::Prioritizer do
219
215
  # end
220
216
  # end
221
217
  end
222
-
223
- context 'as Rails middleware' do
224
- before do
225
- class EchoController < ActionController::Base
226
- def index
227
- render plain: Rack::Berater::Prioritizer.current_priority
228
- end
229
-
230
- def six
231
- response.set_header(Rack::Berater::Prioritizer::HEADER, '6')
232
- index
233
- end
234
-
235
- def nine
236
- response.set_header(Rack::Berater::Prioritizer::HEADER, '9')
237
- index
238
- end
239
- end
240
-
241
- Rails.application = Class.new(Rails::Application) do
242
- config.eager_load = false
243
- config.hosts.clear # disable hostname filtering
244
- end
245
- Rails.application.middleware.use described_class
246
- Rails.initialize!
247
-
248
- Rails.application.routes.draw do
249
- get '/' => 'echo#index'
250
- get '/six' => 'echo#six'
251
- post '/nine' => 'echo#nine'
252
- end
253
- end
254
-
255
- let(:app) { Rails.application }
256
- let(:middleware) { described_class.new(app) }
257
-
258
- after { Rails.application = nil }
259
-
260
- describe '#cache_key_for' do
261
- subject { described_class.new(app).send(:cache_key_for, env) }
262
-
263
- let(:env) { Rack::MockRequest.env_for('/') }
264
-
265
- it 'uses the controller and action name' do
266
- is_expected.to match %r{:echo#index$}
267
- end
268
- end
269
-
270
- context 'when a priority header is sent' do
271
- before { header described_class::HEADER, priority }
272
-
273
- let(:priority) { '6' }
274
-
275
- it 'sets the priority' do
276
- expect(get('/six').body).to eq priority
277
- end
278
- end
279
-
280
- context 'when the app returns a priority' do
281
- it 'does not know the first time the controller is called' do
282
- expect(get('/six').body).to be_empty
283
- expect(post('/nine').body).to be_empty
284
- end
285
-
286
- it 'caches the repsonses for the second time' do
287
- expect(get('/six').body).to be_empty
288
- expect(post('/nine').body).to be_empty
289
-
290
- expect(get('/six').body).to eq '6'
291
- expect(post('/nine').body).to eq '9'
292
- end
293
- end
294
- end
295
218
  end
@@ -0,0 +1,95 @@
1
+ require 'rspec/rails'
2
+
3
+ describe Rack::Berater::RailsPrioritizer do
4
+ before do
5
+ class EchoController < ActionController::Base
6
+ def index
7
+ render plain: Rack::Berater::Prioritizer.current_priority
8
+ end
9
+
10
+ def six
11
+ response.set_header(Rack::Berater::Prioritizer::HEADER, '6')
12
+ index
13
+ end
14
+
15
+ def nine
16
+ response.set_header(Rack::Berater::Prioritizer::HEADER, '9')
17
+ index
18
+ end
19
+ end
20
+
21
+ Rails.application = Class.new(Rails::Application) do
22
+ config.eager_load = false
23
+ config.hosts.clear # disable hostname filtering
24
+ # config.logger = ActiveSupport::Logger.new($stdout)
25
+ end
26
+ Rails.application.middleware.use described_class
27
+ Rails.initialize!
28
+
29
+ Rails.application.routes.draw do
30
+ get '/' => 'echo#index'
31
+ get '/six' => 'echo#six'
32
+ post '/nine' => 'echo#nine'
33
+ end
34
+ end
35
+
36
+ let(:app) { Rails.application }
37
+ let(:middleware) { described_class.new(app) }
38
+
39
+ after do
40
+ cache.clear
41
+ Thread.current[described_class::ENV_KEY] = nil
42
+ Rails.application = nil
43
+ end
44
+
45
+ let(:cache) { described_class.class_variable_get(:@@cache) }
46
+
47
+ describe '#cache_key_for' do
48
+ subject { described_class.new(app).method(:cache_key_for) }
49
+
50
+ it 'uses the controller and action name' do
51
+ expect(
52
+ subject.call(Rack::MockRequest.env_for('/'))
53
+ ).to match /echo#index/
54
+
55
+ expect(
56
+ subject.call(Rack::MockRequest.env_for('/six'))
57
+ ).to match /echo#six/
58
+
59
+ expect(
60
+ subject.call(Rack::MockRequest.env_for('/nine', method: 'POST'))
61
+ ).to match /echo#nine/
62
+ end
63
+
64
+ it 'falls back to Rack style names' do
65
+ expect(
66
+ subject.call(Rack::MockRequest.env_for('/nine'))
67
+ ).to match %r{get:/nine}
68
+ end
69
+ end
70
+
71
+ context 'when a priority header is sent' do
72
+ before { header described_class::HEADER, priority }
73
+
74
+ let(:priority) { '6' }
75
+
76
+ it 'sets the priority' do
77
+ expect(get('/six').body).to eq priority
78
+ end
79
+ end
80
+
81
+ context 'when the app returns a priority' do
82
+ it 'does not know the first time the controller is called' do
83
+ expect(get('/six').body).to be_empty
84
+ expect(post('/nine').body).to be_empty
85
+ end
86
+
87
+ it 'caches the repsonses for the second time' do
88
+ expect(get('/six').body).to be_empty
89
+ expect(post('/nine').body).to be_empty
90
+
91
+ expect(get('/six').body).to eq '6'
92
+ expect(post('/nine').body).to eq '9'
93
+ end
94
+ end
95
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-berater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: simplecov
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -131,10 +145,12 @@ files:
131
145
  - lib/rack-berater.rb
132
146
  - lib/rack/berater.rb
133
147
  - lib/rack/berater/prioritizer.rb
148
+ - lib/rack/berater/rails_prioritizer.rb
134
149
  - lib/rack/berater/railtie.rb
135
150
  - lib/rack/berater/version.rb
136
151
  - spec/limiter_spec.rb
137
152
  - spec/prioritizer_spec.rb
153
+ - spec/rails_prioritizer_spec.rb
138
154
  - spec/railtie_spec.rb
139
155
  - spec/rescuer_spec.rb
140
156
  homepage: https://github.com/dpep/rack-berater
@@ -161,6 +177,7 @@ signing_key:
161
177
  specification_version: 4
162
178
  summary: Rack::Berater
163
179
  test_files:
180
+ - spec/rails_prioritizer_spec.rb
164
181
  - spec/railtie_spec.rb
165
182
  - spec/rescuer_spec.rb
166
183
  - spec/prioritizer_spec.rb