rack-berater 0.3.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5840eaff6319d5e24c78c2f50960138ca27fcce25bb3d45354e7eca195b50f0d
4
- data.tar.gz: f9cdde9f38c97d82e03d6a1e9cea2e1939eaca13f4202a2a674a6fda4e6f530e
3
+ metadata.gz: d7cdec5112bd0a056e38bba20ca0c6c25c84f6703f1c031469b815806ef7f25a
4
+ data.tar.gz: 390634760abee230969aa03f51ad7eb86dc3372f17d4d0c1597cb759db4804fa
5
5
  SHA512:
6
- metadata.gz: b0f561957104a0d049d161b720615a97474439c41f16507b3bd34a1f68269fccbea314f54736393a5e030abdf755de8ae30bc5a92724502371728975b6cf7460
7
- data.tar.gz: f91a4cb03f24b7c18554c8145375a78f1b61cc66a2d99a0841ddca4b547dc97822bd99dfe9267ae1feeb01d3e2c8eba72d70ea075d367671f3e2077e404a8e36
6
+ metadata.gz: cae54a952dec822b533f3f16d555cc5215e888f0783c3fc5d9d266cc8f7b7f14884068e603e19e4e2c1880adcff1bd2adbf73de025cc93095da14b105f89cc8e
7
+ data.tar.gz: 44b0008bc762dfd16b28a9a731ee8c5be5a766e48341828b7c49df9967fbb48cc213955c199b61cbd5abae2bae7e5b4b207ce46e8e6afdd22564d916be49f73a
@@ -9,6 +9,8 @@ module Rack
9
9
  def initialize(app, options = {})
10
10
  @app = app
11
11
  @header = options[:header] || HEADER
12
+
13
+ synchronize { @@cache ||= {} }
12
14
  end
13
15
 
14
16
  def call(env)
@@ -48,31 +50,15 @@ module Rack
48
50
  Thread.current[ENV_KEY] = priority
49
51
  end
50
52
 
51
- @@cache_prefix = 'Berater:Rack:Prioritizer'
52
53
  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.application
59
- res = Rails.application.routes.recognize_path(env[Rack::PATH_INFO])
60
- path = res.values_at(:controller, :action).compact.join('#')
61
- end
62
-
63
- if path.empty?
64
- path = env['PATH_INFO'].gsub(%r{/[0-9]+(/|$)}, '/x\1')
65
- end
66
-
67
54
  [
68
- @@cache_prefix,
69
- req_method,
70
- path,
55
+ env[Rack::REQUEST_METHOD].downcase,
56
+
57
+ # normalize RESTful paths
58
+ env['PATH_INFO'].gsub(%r{/[0-9]+(/|$)}, '/x\1'),
71
59
  ].join(':')
72
60
  end
73
61
 
74
- @@cache = {}
75
-
76
62
  def cache_get(key)
77
63
  synchronize { @@cache[key] }
78
64
  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
@@ -0,0 +1,9 @@
1
+ require 'rack/berater'
2
+ require 'rspec/core'
3
+
4
+ RSpec.configure do |config|
5
+ config.after do
6
+ Thread.current[Rack::Berater::Prioritizer::ENV_KEY] = nil
7
+ Rack::Berater::Prioritizer.class_variable_get(:@@cache).clear
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Berater
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.1"
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 ]
@@ -1,9 +1,4 @@
1
1
  describe Rack::Berater::Prioritizer do
2
- after do
3
- cache.clear
4
- Thread.current[described_class::ENV_KEY] = nil
5
- end
6
-
7
2
  let(:cache) { described_class.class_variable_get(:@@cache) }
8
3
 
9
4
  describe '#call' do
@@ -117,12 +112,8 @@ describe Rack::Berater::Prioritizer do
117
112
  context 'with a basic env' do
118
113
  let(:env) { Rack::MockRequest.env_for('/') }
119
114
 
120
- it 'has a Berater prefix' do
121
- is_expected.to match /^Berater:/
122
- end
123
-
124
115
  it 'combines the verb and path' do
125
- is_expected.to match %r{:get:/$}
116
+ is_expected.to match %r{get:/$}
126
117
  end
127
118
  end
128
119
 
@@ -130,7 +121,7 @@ describe Rack::Berater::Prioritizer do
130
121
  let(:env) { Rack::MockRequest.env_for('/', method: 'PUT') }
131
122
 
132
123
  it 'combines the verb and path' do
133
- is_expected.to match %r{:put:/$}
124
+ is_expected.to match %r{put:/$}
134
125
  end
135
126
  end
136
127
 
@@ -138,7 +129,7 @@ describe Rack::Berater::Prioritizer do
138
129
  let(:env) { Rack::MockRequest.env_for('/user/123') }
139
130
 
140
131
  it 'normalizes the id' do
141
- is_expected.to match %r{:/user/x$}
132
+ is_expected.to match %r{get:/user/x$}
142
133
  end
143
134
  end
144
135
 
@@ -146,7 +137,7 @@ describe Rack::Berater::Prioritizer do
146
137
  let(:env) { Rack::MockRequest.env_for('/user/123/') }
147
138
 
148
139
  it 'normalizes the id and keeps the trailing slash' do
149
- is_expected.to match %r{:/user/x/$}
140
+ is_expected.to match %r{get:/user/x/$}
150
141
  end
151
142
  end
152
143
 
@@ -154,7 +145,7 @@ describe Rack::Berater::Prioritizer do
154
145
  let(:env) { Rack::MockRequest.env_for('/user/123/friend/456') }
155
146
 
156
147
  it 'normalizes both ids' do
157
- is_expected.to match %r{:/user/x/friend/x$}
148
+ is_expected.to match %r{get:/user/x/friend/x$}
158
149
  end
159
150
  end
160
151
  end
@@ -219,77 +210,4 @@ describe Rack::Berater::Prioritizer do
219
210
  # end
220
211
  # end
221
212
  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
- get '/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(get('/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(get('/nine').body).to be_empty
289
-
290
- expect(get('/six').body).to eq '6'
291
- expect(get('/nine').body).to eq '9'
292
- end
293
- end
294
- end
295
213
  end
@@ -0,0 +1,100 @@
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
+ get '/redirect' => redirect('/')
34
+ end
35
+ end
36
+
37
+ let(:app) { Rails.application }
38
+ let(:middleware) { described_class.new(app) }
39
+
40
+ after do
41
+ Rails.application = nil
42
+ end
43
+
44
+ let(:cache) { described_class.class_variable_get(:@@cache) }
45
+
46
+ describe '#cache_key_for' do
47
+ subject { described_class.new(app).method(:cache_key_for) }
48
+
49
+ it 'uses the controller and action name' do
50
+ expect(
51
+ subject.call(Rack::MockRequest.env_for('/'))
52
+ ).to match /echo#index/
53
+
54
+ expect(
55
+ subject.call(Rack::MockRequest.env_for('/six'))
56
+ ).to match /echo#six/
57
+
58
+ expect(
59
+ subject.call(Rack::MockRequest.env_for('/nine', method: 'POST'))
60
+ ).to match /echo#nine/
61
+ end
62
+
63
+ it 'falls back to Rack style names' do
64
+ expect(
65
+ subject.call(Rack::MockRequest.env_for('/nine'))
66
+ ).to match %r{get:/nine}
67
+ end
68
+
69
+ it 'works with redirects' do
70
+ expect(
71
+ subject.call(Rack::MockRequest.env_for('/redirect'))
72
+ ).to match %r{get:/redirect}
73
+ end
74
+ end
75
+
76
+ context 'when a priority header is sent' do
77
+ before { header described_class::HEADER, priority }
78
+
79
+ let(:priority) { '6' }
80
+
81
+ it 'sets the priority' do
82
+ expect(get('/six').body).to eq priority
83
+ end
84
+ end
85
+
86
+ context 'when the app returns a priority' do
87
+ it 'does not know the first time the controller is called' do
88
+ expect(get('/six').body).to be_empty
89
+ expect(post('/nine').body).to be_empty
90
+ end
91
+
92
+ it 'caches the repsonses for the second time' do
93
+ expect(get('/six').body).to be_empty
94
+ expect(post('/nine').body).to be_empty
95
+
96
+ expect(get('/six').body).to eq '6'
97
+ expect(post('/nine').body).to eq '9'
98
+ end
99
+ end
100
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-berater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-17 00:00:00.000000000 Z
11
+ date: 2021-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: berater
@@ -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,13 @@ 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
150
+ - lib/rack/berater/rspec.rb
135
151
  - lib/rack/berater/version.rb
136
152
  - spec/limiter_spec.rb
137
153
  - spec/prioritizer_spec.rb
154
+ - spec/rails_prioritizer_spec.rb
138
155
  - spec/railtie_spec.rb
139
156
  - spec/rescuer_spec.rb
140
157
  homepage: https://github.com/dpep/rack-berater
@@ -161,6 +178,7 @@ signing_key:
161
178
  specification_version: 4
162
179
  summary: Rack::Berater
163
180
  test_files:
181
+ - spec/rails_prioritizer_spec.rb
164
182
  - spec/railtie_spec.rb
165
183
  - spec/rescuer_spec.rb
166
184
  - spec/prioritizer_spec.rb