implement token bucket rate limiting
This commit is contained in:
@@ -19,16 +19,18 @@ class PostsControllerTest < ActionController::TestCase
|
||||
context "for api calls" do
|
||||
context "passing the api limit" do
|
||||
setup do
|
||||
User.any_instance.stubs(:api_hourly_limit).returns(5)
|
||||
@post = FactoryGirl.create(:post)
|
||||
@bucket = TokenBucket.create(user_id: @user.id, token_count: 5, last_touched_at: Time.now)
|
||||
User.any_instance.stubs(:api_burst_limit).returns(5)
|
||||
end
|
||||
|
||||
should "work" do
|
||||
CurrentUser.user.api_hourly_limit.times do
|
||||
get :index, {:format => "json", :login => @user.name, :api_key => @user.api_key.key}
|
||||
@user.api_burst_limit.times do
|
||||
post :update, {:format => "json", :id => @post.id, :post => {:rating => "q"}, :login => @user.name, :api_key => @user.api_key.key}
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
get :index, {:format => "json", :login => @user.name, :api_key => @user.api_key.key}
|
||||
post :update, {:format => "json", :id => @post.id, :post => {:rating => "q"}, :login => @user.name, :api_key => @user.api_key.key}
|
||||
assert_response 429
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user