Skip to content

Commit

Permalink
Merge pull request #258 from djanowski/shutdown
Browse files Browse the repository at this point in the history
`Mechanize::start` and `Mechanize#shutdown`
  • Loading branch information
flavorjones committed Oct 1, 2012
2 parents a05f1c6 + 6326ccb commit a6be7e2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
33 changes: 33 additions & 0 deletions lib/mechanize.rb
Expand Up @@ -137,6 +137,23 @@ def self.inherited(child) # :nodoc:
super
end

##
# Creates a new Mechanize instance and yields it to the given block.
#
# After the block executes, the instance is cleaned up. This includes
# closing all open connections.
#
# Mechanize.start do |m|
# m.get("http://example.com")
# end

def self.start
instance = new
yield(instance)
ensure
instance.shutdown
end

##
# Creates a new mechanize instance. If a block is given, the created
# instance is yielded to the block for setting up pre-connection state such
Expand Down Expand Up @@ -1211,6 +1228,22 @@ def set_proxy address, port, user = nil, password = nil
@agent.set_proxy address, port, user, password
end

##
# Clears history and cookies.

def reset
@agent.reset
end

##
# Shuts down this session by clearing browsing state and closing all
# persistent connections.

def shutdown
reset
@agent.shutdown
end

private

##
Expand Down
14 changes: 12 additions & 2 deletions lib/mechanize/http/agent.rb
Expand Up @@ -401,6 +401,11 @@ def connection_for uri
end
end

# Closes all open connections for this agent.
def shutdown
http.shutdown
end

##
# Decodes a gzip-encoded +body_io+. If it cannot be decoded, inflate is
# tried followed by raising an error.
Expand Down Expand Up @@ -694,7 +699,7 @@ def response_authenticate(response, page, uri, request, headers, params,
message = 'WWW-Authenticate header missing in response'
raise Mechanize::UnauthorizedError.new(page, nil, message)
end

challenges = @authenticate_parser.parse www_authenticate

unless @auth_store.credentials? uri, challenges then
Expand Down Expand Up @@ -798,7 +803,7 @@ def response_content_encoding response, body_io
begin
if Tempfile === body_io and
(StringIO === out_io or out_io.path != body_io.path) then
body_io.close!
body_io.close!
end
rescue IOError
# HACK ruby 1.8 raises IOError when closing the stream
Expand Down Expand Up @@ -1198,6 +1203,11 @@ def use_tempfile? size
size >= @max_file_buffer
end

def reset
@cookie_jar.clear!
@history.clear
end

end

require 'mechanize/http/auth_store'
Expand Down
34 changes: 34 additions & 0 deletions test/test_mechanize.rb
Expand Up @@ -1017,6 +1017,40 @@ def test_set_proxy
assert_equal URI('http://user:pass@localhost:8080'), http.proxy_uri
end

def test_shutdown
uri = URI 'http://localhost'
jar = Mechanize::CookieJar.new
Mechanize::Cookie.parse uri, 'a=b' do |cookie|
jar.add uri, cookie
end

@mech.cookie_jar = jar

@mech.get("http://localhost/")

assert_match /Hello World/, @mech.current_page.body
refute_empty @mech.cookies
refute_empty Thread.current[@mech.agent.http.request_key]

@mech.shutdown

assert_nil Thread.current[@mech.agent.http.request_key]
assert_empty @mech.history
assert_empty @mech.cookies
end

def test_start
body, id = nil

Mechanize.start do |m|
body = m.get("http://localhost/").body
id = m.agent.http.request_key
end

assert_match /Hello World/, body
assert_nil Thread.current[id]
end

def test_submit_bad_form_method
page = @mech.get("http://localhost/bad_form_test.html")
assert_raises ArgumentError do
Expand Down

0 comments on commit a6be7e2

Please sign in to comment.