Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed the ThrottleHelper module in favor of rspec dsl matchers

  • Loading branch information...
commit 160dd24c52086568474c254e66f02161098528ac 1 parent 6ee33ac
@bemurphy bemurphy authored
View
7 spec/daily_spec.rb
@@ -8,21 +8,20 @@ def app
describe Rack::Throttle::Daily do
include Rack::Test::Methods
include Webrat::Matchers
- include ThrottleHelpers
it "should be allowed if not seen this day" do
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should be allowed if seen fewer than the max allowed per day" do
2.times { get "/foo" }
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should not be allowed if seen more times than the max allowed per day" do
4.times { get "/foo" }
- request_is_throttled
+ last_response.body.should show_throttled_response
end
# TODO mess with time travelling and requests to make sure no overlap
View
7 spec/hourly_spec.rb
@@ -8,21 +8,20 @@ def app
describe Rack::Throttle::Hourly do
include Rack::Test::Methods
include Webrat::Matchers
- include ThrottleHelpers
it "should be allowed if not seen this hour" do
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should be allowed if seen fewer than the max allowed per hour" do
2.times { get "/foo" }
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should not be allowed if seen more times than the max allowed per hour" do
4.times { get "/foo" }
- request_is_throttled
+ last_response.body.should show_throttled_response
end
# TODO mess with time travelling and requests to make sure no overlap
View
11 spec/interval_spec.rb
@@ -8,34 +8,33 @@ def app
describe Rack::Throttle::Interval do
include Rack::Test::Methods
include Webrat::Matchers
- include ThrottleHelpers
it "should allow the request if the source has not been seen" do
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should allow the request if the source has not been seen in the current interval" do
get "/foo"
sleep 0.2 # Should time travel this instead?
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should not all the request if the source has been seen inside the current interval" do
2.times { get "/foo" }
- request_is_throttled
+ last_response.body.should show_throttled_response
end
it "should gracefully allow the request if the cache bombs on getting" do
app.should_receive(:cache_get).and_raise(StandardError)
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should gracefully allow the request if the cache bombs on setting" do
app.should_receive(:cache_set).and_raise(StandardError)
get "/foo"
- last_response.body.should contain("Example App Body")
+ last_response.body.should show_allowed_response
end
end
View
13 spec/limiter_spec.rb
@@ -8,24 +8,23 @@ def app
describe Rack::Throttle::Limiter do
include Rack::Test::Methods
include Webrat::Matchers
- include ThrottleHelpers
describe "basic calling" do
it "should return the example app" do
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should call the application if allowed" do
app.should_receive(:allowed?).and_return(true)
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should give a rate limit exceeded message if not allowed" do
app.should_receive(:allowed?).and_return(false)
get "/foo"
- request_is_throttled
+ last_response.body.should show_throttled_response
end
end
@@ -33,20 +32,20 @@ def app
it "should return true if whitelisted" do
app.should_receive(:whitelisted?).and_return(true)
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
it "should return false if blacklisted" do
app.should_receive(:blacklisted?).and_return(true)
get "/foo"
- request_is_throttled
+ last_response.body.should show_throttled_response
end
it "should return true if not whitelisted or blacklisted" do
app.should_receive(:whitelisted?).and_return(false)
app.should_receive(:blacklisted?).and_return(false)
get "/foo"
- request_is_allowed
+ last_response.body.should show_allowed_response
end
end
end
View
44 spec/spec_helper.rb
@@ -3,17 +3,43 @@
require "rack/throttle"
require "webrat"
-module ThrottleHelpers
- def request_is_allowed
- last_response.body.should contain("Example App Body")
+def example_target_app
+ @target_app ||= mock("Example Rack App")
+ @target_app.stub!(:call).and_return([200, {}, "Example App Body"])
+end
+
+Spec::Matchers.define :show_allowed_response do
+ match do |body|
+ body.include?("Example App Body")
+ end
+
+ failure_message_for_should do
+ "expected response to show the allowed response"
+ end
+
+ failure_message_for_should_not do
+ "expected response not to show the allowed response"
end
- def request_is_throttled
- last_response.body.should contain("Rate Limit Exceeded")
- end
+ description do
+ "expected the allowed repsonse"
+ end
end
-def example_target_app
- @target_app ||= mock("Example Rack App")
- @target_app.stub!(:call).and_return([200, {}, "Example App Body"])
+Spec::Matchers.define :show_throttled_response do
+ match do |body|
+ body.include?("Rate Limit Exceeded")
+ end
+
+ failure_message_for_should do
+ "expected response to show the throttled response"
+ end
+
+ failure_message_for_should_not do
+ "expected response not to show the throttled response"
+ end
+
+ description do
+ "expected the throttled repsonse"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.