Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uvlad7 committed Oct 4, 2024
1 parent 2aaaedf commit 03b39ee
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
13 changes: 12 additions & 1 deletion tests/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
58 changes: 57 additions & 1 deletion tests/tc_curl_easy_cookielist.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 03b39ee

Please sign in to comment.