Skip to content

Commit

Permalink
feat: Message#previous to get responses in negotiation
Browse files Browse the repository at this point in the history
HTTP::Message#previous keeps previous response in negotiation.  For
redirection, authorization negotiation and retry from custom filter.

Closes #234.
  • Loading branch information
Hiroshi Nakamura committed Dec 28, 2014
1 parent 9e8b6c4 commit 249c9ba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/httpclient.rb
Expand Up @@ -954,6 +954,7 @@ def do_request(method, uri, query, body, header, &block)
end
retry_count = @session_manager.protocol_retry_count
proxy = no_proxy?(uri) ? nil : @proxy
previous = nil
while retry_count > 0
body.pos = pos if pos
req = create_request(method, uri, query, body, header)
Expand All @@ -962,9 +963,10 @@ def do_request(method, uri, query, body, header, &block)
do_get_block(req, proxy, conn, &block)
end
res = conn.pop
res.previous = previous
break
rescue RetryableResponse
res = conn.pop
previous = conn.pop
retry_count -= 1
end
end
Expand Down Expand Up @@ -1029,15 +1031,18 @@ def follow_redirect(method, uri, query, body, header, &block)
pos = body.pos rescue nil
end
retry_number = 0
previous = nil
while retry_number < @follow_redirect_count
body.pos = pos if pos
res = do_request(method, uri, query, body, header, &filtered_block)
res.previous = previous
if res.redirect?
if res.header['location'].empty?
raise BadResponseError.new("Missing Location header for redirect", res)
end
method = :get if res.see_other? # See RFC2616 10.3.4
uri = urify(@redirect_uri_callback.call(uri, res))
previous = res
retry_number += 1
else
return res
Expand Down
6 changes: 5 additions & 1 deletion test/test_auth.rb
Expand Up @@ -146,7 +146,11 @@ def test_BASIC_auth
c.www_auth.basic_auth.instance_eval { @scheme = "BASIC" }
#
c.set_auth("http://localhost:#{serverport}/", 'admin', 'admin')
assert_equal('basic_auth OK', c.get_content("http://localhost:#{serverport}/basic_auth"))
res = c.get("http://localhost:#{serverport}/basic_auth")
assert_equal('basic_auth OK', res.content)
assert_equal(200, res.status)
assert_equal(401, res.previous.status)
assert_equal(nil, res.previous.previous)
ensure
@basic_auth.instance_eval { @auth_scheme = webrick_backup }
end
Expand Down
8 changes: 7 additions & 1 deletion test/test_httpclient.rb
Expand Up @@ -710,7 +710,13 @@ def test_head_follow_redirect
def test_get_follow_redirect
assert_equal('hello', @client.get(serverurl + 'hello', :follow_redirect => true).body)
assert_equal('hello', @client.get(serverurl + 'redirect1', :follow_redirect => true).body)
assert_equal('hello', @client.get(serverurl + 'redirect2', :follow_redirect => true).body)

res = @client.get(serverurl + 'redirect2', :follow_redirect => true)
assert_equal('hello', res.body)
assert_equal("http://localhost:#{@serverport}/hello", res.header.request_uri.to_s)
assert_equal("http://localhost:#{@serverport}/redirect3", res.previous.header.request_uri.to_s)
assert_equal("http://localhost:#{@serverport}/redirect2", res.previous.previous.header.request_uri.to_s)
assert_equal(nil, res.previous.previous.previous)
end

def test_get_async
Expand Down

0 comments on commit 249c9ba

Please sign in to comment.