Skip to content

Commit

Permalink
Refactor TooManyRequestsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed May 13, 2024
1 parent 22a9c45 commit f23777c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion test/x/client_request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setup
stub_request(http_method, "https://api.twitter.com/2/tweets")
@client.public_send(http_method, "tweets", headers:)

assert_requested http_method, "https://api.twitter.com/2/tweets", headers: {"User-Agent" => "Custom User Agent"}
assert_requested http_method, "https://api.twitter.com/2/tweets", headers:
end

define_method :"test_#{http_method}_request_with_custom_response_objects" do
Expand Down
31 changes: 27 additions & 4 deletions test/x/too_many_requests_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,37 @@ class TooManyRequestsTest < Minitest::Test
cover TooManyRequests

def setup
response = Net::HTTPTooManyRequests.new("1.1", 429, "Too Many Requests")

rate_limit(response)
app_limit(response)
user_limit(response)

@exception = TooManyRequests.new(response:)
end

def rate_limit(response)
Time.stub :now, Time.utc(1983, 11, 24) do
response = Net::HTTPTooManyRequests.new("1.1", 429, "Too Many Requests")
response["x-rate-limit-limit"] = "100"
response["x-rate-limit-remaining"] = "0"
response["x-rate-limit-reset"] = (Time.now + 60).to_i.to_s
end
response["x-rate-limit-limit"] = "100"
response["x-rate-limit-remaining"] = "0"
end

def app_limit(response)
Time.stub :now, Time.utc(1983, 11, 24) do
response["x-app-limit-24hour-reset"] = (Time.now + 61).to_i.to_s
end
response["x-app-limit-24hour-limit"] = "100"
response["x-app-limit-24hour-remaining"] = "0"
end

@exception = TooManyRequests.new(response:)
def user_limit(response)
Time.stub :now, Time.utc(1983, 11, 24) do
response["x-user-limit-24hour-remaining"] = (Time.now + 60).to_i.to_s
end
response["x-user-limit-24hour-reset"] = "100"
response["x-user-limit-24hour-reset"] = "0"
end

def test_initialize_with_empty_response
Expand Down

0 comments on commit f23777c

Please sign in to comment.