diff --git a/tests/helper.rb b/tests/helper.rb index cd9316e..20970c7 100644 --- a/tests/helper.rb +++ b/tests/helper.rb @@ -94,7 +94,18 @@ def do_HEAD(req,res) end def do_POST(req,res) - if req.query['filename'].nil? + if req.path.match(/set_cookies$/) + JSON.parse(req.body || '[]', symbolize_names: true).each do |hash| + cookie = WEBrick::Cookie.new(hash.fetch(:name), hash.fetch(:value)) + cookie.domain = hash[:domain] if hash.key?(:domain) + cookie.expires = hash[:expires] if hash.key?(:expires) + cookie.path = hash[:path] if hash.key?(:path) + cookie.secure = hash[:secure] if hash.key?(:secure) + cookie.max_age = hash[:max_age] if hash.key?(:max_age) + res.cookies.push(cookie) + end + respond_with('OK', req, res) + elsif req.query['filename'].nil? if req.body params = {} req.body.split('&').map{|s| k,v=s.split('='); params[k] = v } diff --git a/tests/tc_curl_easy_cookielist.rb b/tests/tc_curl_easy_cookielist.rb index c8a8278..3209674 100644 --- a/tests/tc_curl_easy_cookielist.rb +++ b/tests/tc_curl_easy_cookielist.rb @@ -1,4 +1,5 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) +require 'json' class TestCurbCurlEasyCookielist < Test::Unit::TestCase def test_setopt_cookielist @@ -28,9 +29,23 @@ def test_setopt_cookielist easy.url = "http://localhost:#{TestServlet.port}#{TestServlet.path}/get_cookies" easy.perform assert_equal 'c2=v2; c1=v1', easy.body_str + end + + # libcurl documentation says: "This option also enables the cookie engine", but it's not tracked on the curb level + def test_setopt_cookielist_enables_cookie_engine + easy = Curl::Easy.new + expires = (Date.today + 2).to_datetime + easy.url = "http://localhost:#{TestServlet.port}#{TestServlet.path}/set_cookies" + easy.setopt(Curl::CURLOPT_COOKIELIST, "Set-Cookie: c1=v1; domain=localhost; expires=#{expires.httpdate};") + easy.post_body = JSON.generate([{ name: 'c2', value: 'v2', domain: 'localhost', expires: expires.httpdate, path: '/' }]) + easy.perform + easy.url = "http://localhost:#{TestServlet.port}#{TestServlet.path}/get_cookies" + easy.post_body = nil + easy.perform - # libcurl documentation says: "This option also enables the cookie engine", but it's not tracked on the curb level assert !easy.enable_cookies? + assert_equal [".localhost\tTRUE\t/\tFALSE\t#{expires.to_time.to_i}\tc1\tv1", ".localhost\tTRUE\t/\tFALSE\t#{expires.to_time.to_i}\tc2\tv2"], easy.cookielist + assert_equal 'c2=v2; c1=v1', easy.body_str end def test_setopt_cookielist_invalid_format @@ -192,6 +207,47 @@ def test_setopt_cookielist_command_reload end end + def test_commands_do_not_enable_cookie_engine + %w[ALL SESS FLUSH RELOAD].each do |command| + easy = Curl::Easy.new + expires = (Date.today + 2).to_datetime + easy.url = "http://localhost:#{TestServlet.port}#{TestServlet.path}/set_cookies" + easy.setopt(Curl::CURLOPT_COOKIELIST, command) + easy.post_body = JSON.generate([{ name: 'c2', value: 'v2', domain: 'localhost', expires: expires.httpdate, path: '/' }]) + easy.perform + easy.url = "http://localhost:#{TestServlet.port}#{TestServlet.path}/get_cookies" + easy.post_body = nil + easy.perform + + assert !easy.enable_cookies? + assert_nil easy.cookielist + assert_equal '', easy.body_str + end + end + + + def test_strings_without_cookie_enable_cookie_engine + [ + '', + '# Netscape HTTP Cookie File', + 'no_a_cookie', + ].each do |command| + easy = Curl::Easy.new + expires = (Date.today + 2).to_datetime + easy.url = "http://localhost:#{TestServlet.port}#{TestServlet.path}/set_cookies" + easy.setopt(Curl::CURLOPT_COOKIELIST, command) + easy.post_body = JSON.generate([{ name: 'c2', value: 'v2', domain: 'localhost', expires: expires.httpdate, path: '/' }]) + easy.perform + easy.url = "http://localhost:#{TestServlet.port}#{TestServlet.path}/get_cookies" + easy.post_body = nil + easy.perform + + assert !easy.enable_cookies? + assert_equal [".localhost\tTRUE\t/\tFALSE\t#{expires.to_time.to_i}\tc2\tv2"], easy.cookielist + assert_equal 'c2=v2', easy.body_str + end + end + def with_permanent_and_session_cookies(expires) easy = Curl::Easy.new easy.url = "http://localhost:#{TestServlet.port}#{TestServlet.path}/get_cookies"