Skip to content

Commit e7b9574

Browse files
committed
enhance tests
1 parent 03879f0 commit e7b9574

File tree

1 file changed

+52
-64
lines changed

1 file changed

+52
-64
lines changed

spec/unit/middleware/service_broker_rate_limiter_spec.rb

Lines changed: 52 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -62,78 +62,66 @@ module Middleware
6262
end
6363

6464
describe 'service endpoints' do
65-
shared_examples 'rate-limited service endpoint' do
66-
before do
67-
allow(app).to receive(:call) do
68-
sleep(1)
69-
[200, {}, 'a body']
70-
end
71-
end
72-
73-
it 'does not allow more than the max number of concurrent requests' do
74-
threads = 2.times.map { Thread.new { middleware.call(env) } }
75-
statuses = threads.map(&:join).map(&:value).map(&:first)
76-
77-
expect(statuses).to include(200)
78-
expect(statuses).to include(429)
79-
expect(app).to have_received(:call).once
80-
expect(logger).to have_received(:info).with("Service broker concurrent rate limit exceeded for user '#{user_guid}'")
81-
end
82-
end
83-
84-
context 'v2 rate limited service endpoints' do
85-
v2_rate_limited_service_endpoints = %w[/v2/service_instances /v2/service_bindings /v2/service_keys]
86-
87-
methods = %w[PUT POST DELETE]
65+
context 'v2 and v3 rate limited service endpoints' do
66+
rate_limited_service_endpoints = [
67+
%w[/v2/service_instances POST],
68+
%w[/v2/service_bindings POST],
69+
%w[/v2/service_keys POST],
70+
%w[/v2/service_instances PUT],
71+
%w[/v2/service_bindings PUT],
72+
%w[/v2/service_keys PUT],
73+
%w[/v2/service_instances DELETE],
74+
%w[/v2/service_bindings DELETE],
75+
%w[/v2/service_keys DELETE],
76+
%w[/v3/service_instances/:guid/parameters GET],
77+
%w[/v3/service_credential_bindings/:guid/parameters GET],
78+
%w[/v3/service_route_bindings/:guid/parameters GET]
79+
]
80+
rate_limited_service_endpoints.each do |endpoint, method|
81+
context "#{endpoint} #{method} - rate-limited" do
82+
let(:path) { endpoint.gsub(':guid', instance.guid.to_s) }
83+
let(:request_method) { method }
84+
let(:fake_request) { instance_double(ActionDispatch::Request, fullpath: endpoint, method: method) }
8885

89-
v2_rate_limited_service_endpoints.each do |endpoint|
90-
methods.each do |method|
91-
context "#{endpoint} #{method} - rate-limited" do
92-
let(:path) { endpoint }
93-
let(:request_method) { method }
94-
let(:fake_request) { instance_double(ActionDispatch::Request, fullpath: endpoint, method: method) }
86+
it 'does not allow more than the max number of concurrent requests' do
87+
threads = 2.times.map { Thread.new { middleware.call(env) } }
88+
statuses = threads.map(&:join).map(&:value).map(&:first)
9589

96-
include_examples 'rate-limited service endpoint'
90+
expect(statuses).to include(200)
91+
expect(statuses).to include(429)
92+
expect(app).to have_received(:call).once
93+
expect(logger).to have_received(:info).with("Service broker concurrent rate limit exceeded for user '#{user_guid}'")
9794
end
9895
end
9996
end
10097
end
10198

102-
context 'v3 rate limited GET service endpoints' do
103-
v3_rate_limited_service_endpoints = %w[/v3/service_instances/:guid/parameters /v3/service_credential_bindings/:guid/parameters
104-
/v3/service_route_bindings/:guid/parameters]
105-
106-
v3_rate_limited_service_endpoints.each do |endpoint|
107-
context "#{endpoint} GET - rate-limited" do
108-
let(:path) { endpoint.gsub(':guid', instance.guid.to_s) }
109-
let(:request_method) { 'GET' }
110-
let(:fake_request) { instance_double(ActionDispatch::Request, fullpath: path, method: request_method) }
111-
112-
include_examples 'rate-limited service endpoint'
113-
end
114-
end
115-
end
116-
11799
context 'v3 non rate limited service endpoints' do
118-
v3_not_rate_limited_service_endpoints = %w[/v3/service_instances /v3/service_credential_bindings /v3/service_route_bindings]
119-
120-
methods = %w[POST PATCH DELETE]
121-
122-
v3_not_rate_limited_service_endpoints.each do |endpoint|
123-
methods.each do |method|
124-
context "#{endpoint} #{method} - non-rate-limited" do
125-
let(:path) { endpoint }
126-
let(:request_method) { method }
127-
let(:fake_request) { instance_double(ActionDispatch::Request, fullpath: endpoint, method: method) }
128-
129-
it 'allows concurrent requests without limits' do
130-
threads = 2.times.map { Thread.new { middleware.call(env) } }
131-
statuses = threads.map(&:join).map(&:value).map(&:first)
132-
133-
expect(statuses).to all(eq(200))
134-
expect(app).to have_received(:call).twice
135-
expect(logger).not_to have_received(:info)
136-
end
100+
v3_not_rate_limited_service_endpoints = [
101+
%w[/v3/service_instances POST],
102+
%w[/v3/service_credential_bindings POST],
103+
%w[/v3/service_route_bindings POST],
104+
%w[/v3/service_instances PATCH],
105+
%w[/v3/service_credential_bindings PATCH],
106+
%w[/v3/service_route_bindings PATCH],
107+
%w[/v3/service_instances DELETE],
108+
%w[/v3/service_credential_bindings DELETE],
109+
%w[/v3/service_route_bindings DELETE]
110+
]
111+
112+
v3_not_rate_limited_service_endpoints.each do |endpoint, method|
113+
context "#{endpoint} #{method} - non-rate-limited" do
114+
let(:path) { endpoint }
115+
let(:request_method) { method }
116+
let(:fake_request) { instance_double(ActionDispatch::Request, fullpath: endpoint, method: method) }
117+
118+
it 'allows concurrent requests without limits' do
119+
threads = 2.times.map { Thread.new { middleware.call(env) } }
120+
statuses = threads.map(&:join).map(&:value).map(&:first)
121+
122+
expect(statuses).to all(eq(200))
123+
expect(app).to have_received(:call).twice
124+
expect(logger).not_to have_received(:info)
137125
end
138126
end
139127
end

0 commit comments

Comments
 (0)