Skip to content

Commit

Permalink
Fix tests in the same way as the YAMLSaver implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
knu committed May 25, 2022
1 parent e8b7de0 commit d12449a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions test/test_http_cookie.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
require File.expand_path('helper', File.dirname(__FILE__))
require 'psych' if !defined?(YAML) && RUBY_VERSION == "1.9.2"
require 'yaml'

class TestHTTPCookie < Test::Unit::TestCase
def setup
Expand Down Expand Up @@ -1073,36 +1075,46 @@ def test_valid_for_uri?
}
end

if YAML.name == 'Psych' && Psych::VERSION >= '3.1'
private def load_yaml(yaml)
YAML.safe_load(yaml, :permitted_classes => %w[Time HTTP::Cookie Mechanize::Cookie DomainName], :aliases => true)
end
else
private def load_yaml(yaml)
YAML.load(yaml)
end
end

def test_yaml_expires
require 'yaml'
cookie = HTTP::Cookie.new(cookie_values)

assert_equal false, cookie.session?
assert_equal nil, cookie.max_age

ycookie = YAML.load(cookie.to_yaml)
ycookie = load_yaml(cookie.to_yaml)
assert_equal false, ycookie.session?
assert_equal nil, ycookie.max_age
assert_in_delta cookie.expires, ycookie.expires, 1

cookie.expires = nil
ycookie = YAML.load(cookie.to_yaml)
ycookie = load_yaml(cookie.to_yaml)
assert_equal true, ycookie.session?
assert_equal nil, ycookie.max_age

cookie.expires = Time.now + 3600
ycookie = YAML.load(cookie.to_yaml)
ycookie = load_yaml(cookie.to_yaml)
assert_equal false, ycookie.session?
assert_equal nil, ycookie.max_age
assert_in_delta cookie.expires, ycookie.expires, 1

cookie.max_age = 3600
ycookie = YAML.load(cookie.to_yaml)
ycookie = load_yaml(cookie.to_yaml)
assert_equal false, ycookie.session?
assert_in_delta cookie.created_at + 3600, ycookie.expires, 1

cookie.max_age = nil
ycookie = YAML.load(cookie.to_yaml)
ycookie = load_yaml(cookie.to_yaml)
assert_equal true, ycookie.session?
assert_equal nil, ycookie.expires
end
Expand Down

0 comments on commit d12449a

Please sign in to comment.