rack-berater 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack/berater/railtie.rb +2 -2
- data/lib/rack/berater/version.rb +1 -1
- data/lib/rack/berater.rb +8 -3
- data/spec/limiter_spec.rb +31 -9
- data/spec/railtie_spec.rb +17 -0
- data/spec/rescuer_spec.rb +13 -3
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7db43d04f14f84dd3e2f3f96352226a48d4e36bfe17467fd1e4eb77015ca6148
|
4
|
+
data.tar.gz: 40f79342ca6cd92145a32e2d0b76455850cc4bdd25d701e83157a7acf330bc15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f8b3b29d9d62796a49701340fb53446cfca19258f2083c5973bd17e9a9314abb28c7faf7070a260cd4128bb9028346d18f24c98d96d5d8a9e503e5d113f8456
|
7
|
+
data.tar.gz: 7ee2bef14d6c4334ef2d65b67a60fe432047a0ce8581f62461cf1fd0c65264083833bbd9f3fe45f2909be3b52cb2147db9704a44e2f9cbc9017b83f69da182d4
|
data/lib/rack/berater/railtie.rb
CHANGED
data/lib/rack/berater/version.rb
CHANGED
data/lib/rack/berater.rb
CHANGED
@@ -7,7 +7,7 @@ module Rack
|
|
7
7
|
class Berater
|
8
8
|
autoload :Railtie, "rack/berater/railtie"
|
9
9
|
|
10
|
-
|
10
|
+
ERRORS = Set[ ::Berater::Overloaded ]
|
11
11
|
|
12
12
|
def initialize(app, options = {})
|
13
13
|
@app = app
|
@@ -39,11 +39,11 @@ module Rack
|
|
39
39
|
|
40
40
|
def call(env)
|
41
41
|
if enabled?(env)
|
42
|
-
|
42
|
+
limit(env) { @app.call(env) }
|
43
43
|
else
|
44
44
|
@app.call(env)
|
45
45
|
end
|
46
|
-
rescue *
|
46
|
+
rescue *ERRORS => e
|
47
47
|
[
|
48
48
|
@options[:status_code],
|
49
49
|
@options[:headers],
|
@@ -57,5 +57,10 @@ module Rack
|
|
57
57
|
return false unless @limiter
|
58
58
|
@enabled.nil? ? true : @enabled.call(env)
|
59
59
|
end
|
60
|
+
|
61
|
+
def limit(env, &block)
|
62
|
+
limiter = @limiter.respond_to?(:call) ? @limiter.call(env) : @limiter
|
63
|
+
limiter.limit(&block)
|
64
|
+
end
|
60
65
|
end
|
61
66
|
end
|
data/spec/limiter_spec.rb
CHANGED
@@ -39,20 +39,42 @@ describe Rack::Berater do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
describe "limiter option" do
|
42
|
-
|
42
|
+
context "when limiter is a limiter" do
|
43
|
+
let(:limiter) { ::Berater::Unlimiter.new }
|
43
44
|
|
44
|
-
|
45
|
+
include_examples "works nominally"
|
46
|
+
|
47
|
+
it "calls the limiter" do
|
48
|
+
expect(limiter).to receive(:limit).and_call_original
|
49
|
+
response
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when operating beyond limits" do
|
53
|
+
before { Berater.test_mode = :fail }
|
45
54
|
|
46
|
-
|
47
|
-
|
48
|
-
|
55
|
+
it "returns an error" do
|
56
|
+
expect(response.status).to eq 429
|
57
|
+
end
|
58
|
+
end
|
49
59
|
end
|
50
60
|
|
51
|
-
context "when
|
52
|
-
|
61
|
+
context "when limiter is a proc" do
|
62
|
+
let(:limiter_instance) { ::Berater::Unlimiter.new }
|
63
|
+
let(:limiter) { Proc.new { limiter_instance } }
|
64
|
+
|
65
|
+
include_examples "works nominally"
|
66
|
+
|
67
|
+
it "calls the proc with env" do
|
68
|
+
expect(limiter).to receive(:call).with(Hash).and_call_original
|
69
|
+
response
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when operating beyond limits" do
|
73
|
+
before { Berater.test_mode = :fail }
|
53
74
|
|
54
|
-
|
55
|
-
|
75
|
+
it "returns an error" do
|
76
|
+
expect(response.status).to eq 429
|
77
|
+
end
|
56
78
|
end
|
57
79
|
end
|
58
80
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "rails"
|
2
|
+
require "rack/berater/railtie"
|
3
|
+
|
4
|
+
RSpec.describe Rack::Berater::Railtie do
|
5
|
+
subject { Rails.initialize! }
|
6
|
+
|
7
|
+
before do
|
8
|
+
Rails.application = Class.new(Rails::Application) do
|
9
|
+
config.eager_load = false
|
10
|
+
config.logger = ActiveSupport::Logger.new($stdout)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "adds middleware automatically" do
|
15
|
+
expect(subject.middleware).to include(Rack::Berater)
|
16
|
+
end
|
17
|
+
end
|
data/spec/rescuer_spec.rb
CHANGED
@@ -61,7 +61,7 @@ describe Rack::Berater do
|
|
61
61
|
context "with body nil" do
|
62
62
|
let(:options) { { body: nil } }
|
63
63
|
|
64
|
-
it "
|
64
|
+
it "falls back to the default" do
|
65
65
|
expect(response.body).to eq "Too Many Requests"
|
66
66
|
end
|
67
67
|
end
|
@@ -86,6 +86,16 @@ describe Rack::Berater do
|
|
86
86
|
expect(response.body).to eq body
|
87
87
|
end
|
88
88
|
end
|
89
|
+
|
90
|
+
context "with an erroneous value" do
|
91
|
+
let(:options) { { body: 123 } }
|
92
|
+
|
93
|
+
it 'should raise an error' do
|
94
|
+
expect {
|
95
|
+
response
|
96
|
+
}.to raise_error(ArgumentError)
|
97
|
+
end
|
98
|
+
end
|
89
99
|
end
|
90
100
|
|
91
101
|
context "with custom headers" do
|
@@ -132,9 +142,9 @@ describe Rack::Berater do
|
|
132
142
|
|
133
143
|
context "when an error type is registered with middleware" do
|
134
144
|
around do |example|
|
135
|
-
Rack::Berater::
|
145
|
+
Rack::Berater::ERRORS << IOError
|
136
146
|
example.run
|
137
|
-
Rack::Berater::
|
147
|
+
Rack::Berater::ERRORS.delete(IOError)
|
138
148
|
end
|
139
149
|
|
140
150
|
it "catches and transforms limit errors" do
|
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.
|
4
|
+
version: 0.2.0
|
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-10-
|
11
|
+
date: 2021-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: berater
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rails
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: rspec
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,6 +133,7 @@ files:
|
|
119
133
|
- lib/rack/berater/railtie.rb
|
120
134
|
- lib/rack/berater/version.rb
|
121
135
|
- spec/limiter_spec.rb
|
136
|
+
- spec/railtie_spec.rb
|
122
137
|
- spec/rescuer_spec.rb
|
123
138
|
homepage: https://github.com/dpep/rack-berater
|
124
139
|
licenses:
|
@@ -139,10 +154,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
154
|
- !ruby/object:Gem::Version
|
140
155
|
version: '0'
|
141
156
|
requirements: []
|
142
|
-
rubygems_version: 3.1.
|
157
|
+
rubygems_version: 3.1.6
|
143
158
|
signing_key:
|
144
159
|
specification_version: 4
|
145
160
|
summary: Rack::Berater
|
146
161
|
test_files:
|
162
|
+
- spec/railtie_spec.rb
|
147
163
|
- spec/rescuer_spec.rb
|
148
164
|
- spec/limiter_spec.rb
|