Skip to content

Commit

Permalink
Headers are now copied across redirects. Issue sparklemotion#215
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed May 2, 2012
1 parent 522b415 commit 862fefb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.rdoc
@@ -1,6 +1,6 @@
= Mechanize CHANGELOG

=== 2.4.1
=== 2.4

* Bug fixes
* Fixed typos in EXAMPLES and GUIDES. Pull Request #213 by Erkan Yilmaz.
Expand All @@ -10,6 +10,7 @@
by afhbl
* Mechanize handles saving of files with the same name better now. Pull
Request #223 by Godfrey Chan, Issue #219 by Jon Hart
* Mechanize now sends headers across redirects. Issue #215 by Chris Gahan

=== 2.4

Expand Down
7 changes: 4 additions & 3 deletions lib/mechanize/http/agent.rb
Expand Up @@ -283,7 +283,7 @@ def fetch uri, method = :get, headers = {}, params = [],
log.debug("Got cached page") if log
visited_page(uri) || page
when Net::HTTPRedirection
response_redirect response, method, page, redirects, referer
response_redirect response, method, page, redirects, headers, referer
when Net::HTTPUnauthorized
response_authenticate(response, page, uri, request, headers, params,
referer)
Expand Down Expand Up @@ -898,7 +898,8 @@ def response_read response, request, uri
body_io
end

def response_redirect response, method, page, redirects, referer = current_page
def response_redirect(response, method, page, redirects, headers,
referer = current_page)
case @redirect_ok
when true, :all
# shortcut
Expand All @@ -918,7 +919,7 @@ def response_redirect response, method, page, redirects, referer = current_page
@history.push(page, page.uri)
new_uri = resolve response['Location'].to_s, page

fetch new_uri, redirect_method, {}, [], referer, redirects + 1
fetch new_uri, redirect_method, headers, [], referer, redirects + 1
end

# :section: Robots
Expand Down
10 changes: 5 additions & 5 deletions lib/mechanize/test_case.rb
Expand Up @@ -437,9 +437,9 @@ def do_GET(req, res)

class RedirectServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
res['Content-Type'] = req.query['ct'] || "text/html"
res['Content-Type'] = req.query['ct'] || 'text/html'
res.status = req.query['code'] ? req.query['code'].to_i : '302'
res['Location'] = "/verb"
res['Location'] = req['X-Location'] || '/verb'
end

alias :do_POST :do_GET
Expand Down Expand Up @@ -513,12 +513,12 @@ def do_GET(req, res)
end

class VerbServlet < WEBrick::HTTPServlet::AbstractServlet
%w(HEAD GET POST PUT DELETE).each do |verb|
eval(<<-eomethod)
%w[HEAD GET POST PUT DELETE].each do |verb|
eval <<-METHOD
def do_#{verb}(req, res)
res.header['X-Request-Method'] = #{verb.dump}
end
eomethod
METHOD
end
end

Expand Down
29 changes: 27 additions & 2 deletions test/test_mechanize_http_agent.rb
Expand Up @@ -227,6 +227,14 @@ def test_fetch_post_connect_hook
assert response
end

def test_fetch_redirect_header
page = @agent.fetch('http://example/redirect', :get,
'X-Location' => '/http_headers',
'Range' => 'bytes=0-99999')

assert_match 'range|bytes=0-999', page.body
end

def test_fetch_server_error
e = assert_raises Mechanize::ResponseCodeError do
@mech.get 'http://localhost/response_code?code=500'
Expand Down Expand Up @@ -1355,20 +1363,37 @@ def test_response_redirect

page = fake_page
page = @agent.response_redirect({ 'Location' => '/index.html' }, :get,
page, 0, referer)
page, 0, {}, referer)

assert_equal URI('http://fake.example/index.html'), page.uri

assert_equal 'http://example/referer', requests.first['Referer']
end

def test_response_redirect_header
@agent.redirect_ok = true
referer = page 'http://example/referer'

headers = {
'Range' => 'bytes=0-9999',
}

page = fake_page
page = @agent.response_redirect({ 'Location' => '/http_headers' }, :get,
page, 0, headers, referer)

assert_equal URI('http://fake.example/http_headers'), page.uri

assert_match 'range|bytes=0-999', page.body
end

def test_response_redirect_malformed
@agent.redirect_ok = true
referer = page 'http://example/referer'

page = fake_page
page = @agent.response_redirect({ 'Location' => '/index.html?q=あ' }, :get,
page, 0, referer)
page, 0, {}, referer)

assert_equal URI('http://fake.example/index.html?q=%E3%81%82'), page.uri

Expand Down

0 comments on commit 862fefb

Please sign in to comment.