Permalink
Browse files

Modified the cookie_jar save_as method to optionally allow the saving…

… of session cookie hash.
  • Loading branch information...
1 parent c81c08e commit e1c368fddd55a4bc9271ef29f14eed8944afc90f Jim Jones committed May 16, 2012
Showing with 29 additions and 2 deletions.
  1. +2 −2 lib/mechanize/cookie_jar.rb
  2. +27 −0 test/test_mechanize_cookie_jar.rb
@@ -75,9 +75,9 @@ def each
# Available formats:
# :yaml <- YAML structure
# :cookiestxt <- Mozilla's cookies.txt format
- def save_as(file, format = :yaml)
+ def save_as(file, format = :yaml, cookie_cleanup = true)
jar = dup
- jar.cleanup true
+ jar.cleanup cookie_cleanup
open(file, 'w') { |f|
case format
@@ -289,6 +289,33 @@ def test_save_cookies_yaml
assert_equal(3, @jar.cookies(url).length)
end
+ def test_save_session_cookies_yaml
+ url = URI 'http://rubyforge.org/'
+
+ # Add one cookie with an expiration date in the future
+ cookie = Mechanize::Cookie.new(cookie_values)
+ s_cookie = Mechanize::Cookie.new(cookie_values(:name => 'Bar',
+ :expires => nil,
+ :session => true))
+
+ @jar.add(url, cookie)
+ @jar.add(url, s_cookie)
+ @jar.add(url, Mechanize::Cookie.new(cookie_values(:name => 'Baz')))
+
+ assert_equal(3, @jar.cookies(url).length)
+
+ in_tmpdir do
+ @jar.save_as("cookies.yml", :yaml, false)
+
+ jar = Mechanize::CookieJar.new
+ jar.load("cookies.yml")
+ assert_equal(3, jar.cookies(url).length)
+ end
+
+ assert_equal(3, @jar.cookies(url).length)
+ end
+
+
def test_save_cookies_cookiestxt
url = URI 'http://rubyforge.org/'

0 comments on commit e1c368f

Please sign in to comment.