Skip to content

Commit

Permalink
Merge pull request #83 from fotos/fix-idempotance-check
Browse files Browse the repository at this point in the history
Use method to check idempotence in Net::HTTP::Persistent#idempotent?
  • Loading branch information
drbrain authored Jul 25, 2019
2 parents 06c2950 + 54756af commit a904297
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/net/http/persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,8 @@ def http_version uri
# Is +req+ idempotent according to RFC 2616?

def idempotent? req
case req
when Net::HTTP::Delete, Net::HTTP::Get, Net::HTTP::Head,
Net::HTTP::Options, Net::HTTP::Put, Net::HTTP::Trace then
case req.method
when 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'TRACE' then
true
end
end
Expand Down Expand Up @@ -943,7 +942,7 @@ def reset connection
# If a block is passed #request behaves like Net::HTTP#request (the body of
# the response will not have been read).
#
# +req+ must be a Net::HTTPRequest subclass (see Net::HTTP for a list).
# +req+ must be a Net::HTTPGenericRequest subclass (see Net::HTTP for a list).
#
# If there is an error and the request is idempotent according to RFC 2616
# it will be retried automatically.
Expand Down
9 changes: 9 additions & 0 deletions test/test_net_http_persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,16 @@ def test_idempotent_eh
assert @http.idempotent? Net::HTTP::Put.new '/'
assert @http.idempotent? Net::HTTP::Trace.new '/'

assert @http.idempotent? Net::HTTPGenericRequest.new('DELETE', false, true, '/')
assert @http.idempotent? Net::HTTPGenericRequest.new('GET', false, true, '/')
assert @http.idempotent? Net::HTTPGenericRequest.new('HEAD', false, false, '/')
assert @http.idempotent? Net::HTTPGenericRequest.new('OPTIONS', false, false, '/')
assert @http.idempotent? Net::HTTPGenericRequest.new('PUT', true, true, '/')
assert @http.idempotent? Net::HTTPGenericRequest.new('TRACE', false, true, '/')

refute @http.idempotent? Net::HTTP::Post.new '/'

refute @http.idempotent? Net::HTTPGenericRequest.new('POST', true, true, '/')
end

def test_normalize_uri
Expand Down

0 comments on commit a904297

Please sign in to comment.