Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Dec 13, 2024
1 parent 660d607 commit 4b111ce
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/peddler/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def meter(rate_limit)
# HTTP v6.0 will implement retriable. Until then, point to their GitHub repo, or it's a no-op.
# https://github.com/httprb/http/pull/790
delay = sandbox? ? 0.2 : 1.0 / rate_limit
retriable(delay: delay, retry_statuses: [429]) if @http.respond_to?(:retriable)
retriable(delay: delay, retry_statuses: [429])

self
end
Expand All @@ -97,7 +97,7 @@ def meter(rate_limit)
# @return [self]
[:via, :use, :retriable].each do |method|
define_method(method) do |*args, &block|
@http = http.send(method, *args, &block)
@http = http.send(method, *args, &block) if http.respond_to?(method)
self
end
end
Expand Down
48 changes: 48 additions & 0 deletions test/peddler/api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def test_chainable_http_methods
refute_empty(@api.http.default_options.features)
end

def test_rate_limit_noop
initial_http_object_id = @api.http.object_id
@api.meter(1.0).http

assert_equal(initial_http_object_id, @api.http.object_id)
end

def test_rate_limit
skip("HTTP v6.0 not released yet")
end
Expand All @@ -63,5 +70,46 @@ def test_client_error
@api.post("/")
end
end

def test_body_to_json_conversion
error = assert_raises(Peddler::Error) do
@api.post("/", body: { key: "value" })
end
request = error.response.request
request_body = request.body.source
request_content_type = request.headers["Content-Type"]

assert_equal({ key: "value" }.to_json, request_body)
assert_match(%r{\Aapplication/json}, request_content_type)
end

def test_cannot_sandbox!
test_api_class = Class.new(API) do
def perform_cannot_sandbox_operation
cannot_sandbox!
end
end

test_api = test_api_class.new("eu-west-1", "access_token")
test_api.perform_cannot_sandbox_operation

assert_raises(API::CannotSandbox) do
test_api.sandbox.perform_cannot_sandbox_operation
end
end

def test_must_sandbox!
test_api_class = Class.new(API) do
def perform_must_sandbox_operation
must_sandbox!
end
end

test_api = test_api_class.new("eu-west-1", "access_token")

assert_raises(API::MustSandbox) do
test_api.perform_must_sandbox_operation
end
end
end
end

0 comments on commit 4b111ce

Please sign in to comment.