/vendor/gems/mechanize-0.9.0/test/test_cookie_class.rb

https://github.com/BillTheBest/sms · Ruby · 329 lines · 278 code · 39 blank · 12 comment · 31 complexity · e0e022d12b71b462b24d199d7ec3f478 MD5 · raw file

  1. require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
  2. module Enumerable
  3. def combine
  4. masks = inject([[], 1]){|(ar, m), e| [ar << m, m << 1 ] }[0]
  5. all = masks.inject(0){ |al, m| al|m }
  6. result = []
  7. for i in 1..all do
  8. tmp = []
  9. each_with_index do |e, idx|
  10. tmp << e unless (masks[idx] & i) == 0
  11. end
  12. result << tmp
  13. end
  14. result
  15. end
  16. end
  17. class CookieClassTest < Test::Unit::TestCase
  18. def silently
  19. warn_level = $VERBOSE
  20. $VERBOSE = false
  21. res = yield
  22. $VERBOSE = warn_level
  23. res
  24. end
  25. def test_parse_dates
  26. url = URI.parse('http://localhost/')
  27. yesterday = Time.now - 86400
  28. dates = [ "14 Apr 89 03:20:12",
  29. "14 Apr 89 03:20 GMT",
  30. "Fri, 17 Mar 89 4:01:33",
  31. "Fri, 17 Mar 89 4:01 GMT",
  32. "Mon Jan 16 16:12 PDT 1989",
  33. "Mon Jan 16 16:12 +0130 1989",
  34. "6 May 1992 16:41-JST (Wednesday)",
  35. #"22-AUG-1993 10:59:12.82",
  36. "22-AUG-1993 10:59pm",
  37. "22-AUG-1993 12:59am",
  38. "22-AUG-1993 12:59 PM",
  39. #"Friday, August 04, 1995 3:54 PM",
  40. "06/21/95 04:24:34 PM",
  41. #"20/06/95 21:07",
  42. "95-06-08 19:32:48 EDT",
  43. ]
  44. dates.each do |date|
  45. cookie = "PREF=1; expires=#{date}"
  46. silently do
  47. WWW::Mechanize::Cookie.parse(url, cookie) { |c|
  48. assert c.expires, "Tried parsing: #{date}"
  49. assert_equal(true, c.expires < yesterday)
  50. }
  51. end
  52. end
  53. end
  54. def test_double_semicolon
  55. double_semi = 'WSIDC=WEST;; domain=.williams-sonoma.com; path=/'
  56. url = URI.parse('http://williams-sonoma.com/')
  57. WWW::Mechanize::Cookie.parse(url, double_semi) { |cookie|
  58. assert_equal('WSIDC', cookie.name)
  59. assert_equal('WEST', cookie.value)
  60. }
  61. end
  62. def test_parse_bad_version
  63. bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Version=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
  64. url = URI.parse('http://localhost/')
  65. WWW::Mechanize::Cookie.parse(url, bad_cookie) { |cookie|
  66. assert_nil(cookie.version)
  67. }
  68. end
  69. def test_parse_bad_max_age
  70. bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Max-Age=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
  71. url = URI.parse('http://localhost/')
  72. WWW::Mechanize::Cookie.parse(url, bad_cookie) { |cookie|
  73. assert_nil(cookie.max_age)
  74. }
  75. end
  76. def test_parse_date_fail
  77. url = URI.parse('http://localhost/')
  78. dates = [
  79. "20/06/95 21:07",
  80. ]
  81. silently do
  82. dates.each do |date|
  83. cookie = "PREF=1; expires=#{date}"
  84. WWW::Mechanize::Cookie.parse(url, cookie) { |c|
  85. assert_equal(true, c.expires.nil?)
  86. }
  87. end
  88. end
  89. end
  90. def test_parse_valid_cookie
  91. url = URI.parse('http://rubyforge.org/')
  92. cookie_params = {}
  93. cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
  94. cookie_params['path'] = 'path=/'
  95. cookie_params['domain'] = 'domain=.rubyforge.org'
  96. cookie_params['httponly'] = 'HttpOnly'
  97. cookie_value = '12345%7D=ASDFWEE345%3DASda'
  98. expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
  99. cookie_params.keys.combine.each do |c|
  100. cookie_text = "#{cookie_value}; "
  101. c.each_with_index do |key, idx|
  102. if idx == (c.length - 1)
  103. cookie_text << "#{cookie_params[key]}"
  104. else
  105. cookie_text << "#{cookie_params[key]}; "
  106. end
  107. end
  108. cookie = nil
  109. WWW::Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
  110. assert_not_nil(cookie)
  111. assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
  112. assert_equal('/', cookie.path)
  113. assert_equal('rubyforge.org', cookie.domain)
  114. # if expires was set, make sure we parsed it
  115. if c.find { |k| k == 'expires' }
  116. assert_equal(expires, cookie.expires)
  117. else
  118. assert_nil(cookie.expires)
  119. end
  120. end
  121. end
  122. def test_parse_valid_cookie_empty_value
  123. url = URI.parse('http://rubyforge.org/')
  124. cookie_params = {}
  125. cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
  126. cookie_params['path'] = 'path=/'
  127. cookie_params['domain'] = 'domain=.rubyforge.org'
  128. cookie_params['httponly'] = 'HttpOnly'
  129. cookie_value = '12345%7D='
  130. expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
  131. cookie_params.keys.combine.each do |c|
  132. cookie_text = "#{cookie_value}; "
  133. c.each_with_index do |key, idx|
  134. if idx == (c.length - 1)
  135. cookie_text << "#{cookie_params[key]}"
  136. else
  137. cookie_text << "#{cookie_params[key]}; "
  138. end
  139. end
  140. cookie = nil
  141. WWW::Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
  142. assert_not_nil(cookie)
  143. assert_equal('12345%7D=', cookie.to_s)
  144. assert_equal('', cookie.value)
  145. assert_equal('/', cookie.path)
  146. assert_equal('rubyforge.org', cookie.domain)
  147. # if expires was set, make sure we parsed it
  148. if c.find { |k| k == 'expires' }
  149. assert_equal(expires, cookie.expires)
  150. else
  151. assert_nil(cookie.expires)
  152. end
  153. end
  154. end
  155. # If no path was given, use the one from the URL
  156. def test_cookie_using_url_path
  157. url = URI.parse('http://rubyforge.org/login.php')
  158. cookie_params = {}
  159. cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
  160. cookie_params['path'] = 'path=/'
  161. cookie_params['domain'] = 'domain=.rubyforge.org'
  162. cookie_params['httponly'] = 'HttpOnly'
  163. cookie_value = '12345%7D=ASDFWEE345%3DASda'
  164. expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
  165. cookie_params.keys.combine.each do |c|
  166. next if c.find { |k| k == 'path' }
  167. cookie_text = "#{cookie_value}; "
  168. c.each_with_index do |key, idx|
  169. if idx == (c.length - 1)
  170. cookie_text << "#{cookie_params[key]}"
  171. else
  172. cookie_text << "#{cookie_params[key]}; "
  173. end
  174. end
  175. cookie = nil
  176. WWW::Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
  177. assert_not_nil(cookie)
  178. assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
  179. assert_equal('rubyforge.org', cookie.domain)
  180. assert_equal('/', cookie.path)
  181. # if expires was set, make sure we parsed it
  182. if c.find { |k| k == 'expires' }
  183. assert_equal(expires, cookie.expires)
  184. else
  185. assert_nil(cookie.expires)
  186. end
  187. end
  188. end
  189. # Test using secure cookies
  190. def test_cookie_with_secure
  191. url = URI.parse('http://rubyforge.org/')
  192. cookie_params = {}
  193. cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
  194. cookie_params['path'] = 'path=/'
  195. cookie_params['domain'] = 'domain=.rubyforge.org'
  196. cookie_params['secure'] = 'secure'
  197. cookie_value = '12345%7D=ASDFWEE345%3DASda'
  198. expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
  199. cookie_params.keys.combine.each do |c|
  200. next unless c.find { |k| k == 'secure' }
  201. cookie_text = "#{cookie_value}; "
  202. c.each_with_index do |key, idx|
  203. if idx == (c.length - 1)
  204. cookie_text << "#{cookie_params[key]}"
  205. else
  206. cookie_text << "#{cookie_params[key]}; "
  207. end
  208. end
  209. cookie = nil
  210. WWW::Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
  211. assert_not_nil(cookie)
  212. assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
  213. assert_equal('rubyforge.org', cookie.domain)
  214. assert_equal('/', cookie.path)
  215. assert_equal(true, cookie.secure)
  216. # if expires was set, make sure we parsed it
  217. if c.find { |k| k == 'expires' }
  218. assert_equal(expires, cookie.expires)
  219. else
  220. assert_nil(cookie.expires)
  221. end
  222. end
  223. end
  224. # If no domain was given, we must use the one from the URL
  225. def test_cookie_with_url_domain
  226. url = URI.parse('http://login.rubyforge.org/')
  227. cookie_params = {}
  228. cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
  229. cookie_params['path'] = 'path=/'
  230. cookie_params['domain'] = 'domain=.rubyforge.org'
  231. cookie_params['httponly'] = 'HttpOnly'
  232. cookie_value = '12345%7D=ASDFWEE345%3DASda'
  233. expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
  234. cookie_params.keys.combine.each do |c|
  235. next if c.find { |k| k == 'domain' }
  236. cookie_text = "#{cookie_value}; "
  237. c.each_with_index do |key, idx|
  238. if idx == (c.length - 1)
  239. cookie_text << "#{cookie_params[key]}"
  240. else
  241. cookie_text << "#{cookie_params[key]}; "
  242. end
  243. end
  244. cookie = nil
  245. WWW::Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
  246. assert_not_nil(cookie)
  247. assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
  248. assert_equal('/', cookie.path)
  249. assert_equal('login.rubyforge.org', cookie.domain)
  250. # if expires was set, make sure we parsed it
  251. if c.find { |k| k == 'expires' }
  252. assert_equal(expires, cookie.expires)
  253. else
  254. assert_nil(cookie.expires)
  255. end
  256. end
  257. end
  258. def test_parse_cookie_no_spaces
  259. url = URI.parse('http://rubyforge.org/')
  260. cookie_params = {}
  261. cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
  262. cookie_params['path'] = 'path=/'
  263. cookie_params['domain'] = 'domain=.rubyforge.org'
  264. cookie_params['httponly'] = 'HttpOnly'
  265. cookie_value = '12345%7D=ASDFWEE345%3DASda'
  266. expires = Time.parse('Sun, 27-Sep-2037 00:00:00 GMT')
  267. cookie_params.keys.combine.each do |c|
  268. cookie_text = "#{cookie_value};"
  269. c.each_with_index do |key, idx|
  270. if idx == (c.length - 1)
  271. cookie_text << "#{cookie_params[key]}"
  272. else
  273. cookie_text << "#{cookie_params[key]};"
  274. end
  275. end
  276. cookie = nil
  277. WWW::Mechanize::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
  278. assert_not_nil(cookie)
  279. assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
  280. assert_equal('/', cookie.path)
  281. assert_equal('rubyforge.org', cookie.domain)
  282. # if expires was set, make sure we parsed it
  283. if c.find { |k| k == 'expires' }
  284. assert_equal(expires, cookie.expires)
  285. else
  286. assert_nil(cookie.expires)
  287. end
  288. end
  289. end
  290. end