mechanize 0.4.7 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mechanize might be problematic. Click here for more details.

Files changed (48) hide show
  1. data/CHANGELOG +17 -0
  2. data/EXAMPLES +23 -44
  3. data/NOTES +49 -0
  4. data/lib/mechanize.rb +95 -80
  5. data/lib/mechanize/cookie.rb +147 -148
  6. data/lib/mechanize/cookie.rb.rej +16 -0
  7. data/lib/mechanize/errors.rb +29 -0
  8. data/lib/mechanize/form.rb +211 -186
  9. data/lib/mechanize/form_elements.rb +31 -71
  10. data/lib/mechanize/list.rb +34 -0
  11. data/lib/mechanize/mech_version.rb +3 -1
  12. data/lib/mechanize/module.rb +1 -1
  13. data/lib/mechanize/page.rb +162 -180
  14. data/lib/mechanize/page_elements.rb +53 -40
  15. data/lib/mechanize/parsing.rb +11 -3
  16. data/lib/mechanize/pluggable_parsers.rb +147 -0
  17. data/test/data/server.crt +14 -0
  18. data/test/data/server.csr +11 -0
  19. data/test/data/server.key +18 -0
  20. data/test/data/server.pem +15 -0
  21. data/test/htdocs/no_title_test.html +6 -0
  22. data/test/parse.rb +39 -0
  23. data/test/proxy.rb +30 -0
  24. data/test/server.rb +2 -0
  25. data/test/servlets.rb +8 -0
  26. data/test/ssl_server.rb +49 -0
  27. data/test/tc_authenticate.rb +8 -6
  28. data/test/tc_cookie_class.rb +28 -18
  29. data/test/tc_cookie_jar.rb +88 -27
  30. data/test/tc_cookies.rb +41 -44
  31. data/test/tc_errors.rb +9 -23
  32. data/test/tc_forms.rb +36 -32
  33. data/test/tc_frames.rb +6 -4
  34. data/test/tc_links.rb +7 -6
  35. data/test/tc_mech.rb +43 -46
  36. data/test/tc_page.rb +24 -0
  37. data/test/tc_pluggable_parser.rb +103 -0
  38. data/test/tc_post_form.rb +41 -0
  39. data/test/tc_proxy.rb +25 -0
  40. data/test/tc_response_code.rb +13 -10
  41. data/test/tc_save_file.rb +25 -0
  42. data/test/tc_ssl_server.rb +27 -0
  43. data/test/tc_upload.rb +8 -6
  44. data/test/tc_watches.rb +5 -2
  45. data/test/test_includes.rb +3 -3
  46. data/test/ts_mech.rb +11 -2
  47. metadata +100 -86
  48. data/test/tc_filter.rb +0 -34
data/test/tc_links.rb CHANGED
@@ -8,23 +8,24 @@ require 'test_includes'
8
8
  class LinksMechTest < Test::Unit::TestCase
9
9
  include TestMethods
10
10
 
11
+ def setup
12
+ @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
13
+ end
14
+
11
15
  def test_find_meta
12
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
13
- page = agent.get("http://localhost:#{@port}/find_link.html")
16
+ page = @agent.get("http://localhost:#{PORT}/find_link.html")
14
17
  assert_equal(2, page.meta.length)
15
18
  assert_equal("http://www.drphil.com/", page.meta[0].href.downcase)
16
19
  assert_equal("http://www.upcase.com/", page.meta[1].href.downcase)
17
20
  end
18
21
 
19
22
  def test_find_link
20
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
21
- page = agent.get("http://localhost:#{@port}/find_link.html")
23
+ page = @agent.get("http://localhost:#{PORT}/find_link.html")
22
24
  assert_equal(15, page.links.length)
23
25
  end
24
26
 
25
27
  def test_alt_text
26
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
27
- page = agent.get("http://localhost:#{@port}/alt_text.html")
28
+ page = @agent.get("http://localhost:#{PORT}/alt_text.html")
28
29
  assert_equal(4, page.links.length)
29
30
  assert_equal(1, page.meta.length)
30
31
 
data/test/tc_mech.rb CHANGED
@@ -9,63 +9,63 @@ require 'test_includes'
9
9
  class MechMethodsTest < Test::Unit::TestCase
10
10
  include TestMethods
11
11
 
12
+ def setup
13
+ @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
14
+ end
15
+
12
16
  def test_history
13
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
14
17
  0.upto(25) do |i|
15
- assert_equal(i, agent.history.size)
16
- page = agent.get("http://localhost:#{@port}/")
18
+ assert_equal(i, @agent.history.size)
19
+ page = @agent.get("http://localhost:#{PORT}/")
17
20
  end
18
- page = agent.get("http://localhost:#{@port}/form_test.html")
21
+ page = @agent.get("http://localhost:#{PORT}/form_test.html")
19
22
 
20
- assert_equal("http://localhost:#{@port}/form_test.html",
21
- agent.history.last.uri.to_s)
22
- assert_equal("http://localhost:#{@port}/",
23
- agent.history[-2].uri.to_s)
23
+ assert_equal("http://localhost:#{PORT}/form_test.html",
24
+ @agent.history.last.uri.to_s)
25
+ assert_equal("http://localhost:#{PORT}/",
26
+ @agent.history[-2].uri.to_s)
24
27
 
25
- assert_equal(true, agent.visited?("http://localhost:#{@port}/"))
26
- assert_equal(true, agent.visited?("/form_test.html"))
27
- assert_equal(false, agent.visited?("http://google.com/"))
28
- assert_equal(true, agent.visited?(page.links.first))
28
+ assert_equal(true, @agent.visited?("http://localhost:#{PORT}/"))
29
+ assert_equal(true, @agent.visited?("/form_test.html"))
30
+ assert_equal(false, @agent.visited?("http://google.com/"))
31
+ assert_equal(true, @agent.visited?(page.links.first))
29
32
 
30
33
  end
31
34
 
32
35
  def test_max_history
33
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
34
- agent.max_history = 10
36
+ @agent.max_history = 10
35
37
  0.upto(10) do |i|
36
- assert_equal(i, agent.history.size)
37
- page = agent.get("http://localhost:#{@port}/")
38
+ assert_equal(i, @agent.history.size)
39
+ page = @agent.get("http://localhost:#{PORT}/")
38
40
  end
39
41
 
40
42
  0.upto(10) do |i|
41
- assert_equal(10, agent.history.size)
42
- page = agent.get("http://localhost:#{@port}/")
43
+ assert_equal(10, @agent.history.size)
44
+ page = @agent.get("http://localhost:#{PORT}/")
43
45
  end
44
46
  end
45
47
 
46
48
  def test_back_button
47
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
48
49
  0.upto(5) do |i|
49
- assert_equal(i, agent.history.size)
50
- page = agent.get("http://localhost:#{@port}/")
50
+ assert_equal(i, @agent.history.size)
51
+ page = @agent.get("http://localhost:#{PORT}/")
51
52
  end
52
- page = agent.get("http://localhost:#{@port}/form_test.html")
53
-
54
- assert_equal("http://localhost:#{@port}/form_test.html",
55
- agent.history.last.uri.to_s)
56
- assert_equal("http://localhost:#{@port}/",
57
- agent.history[-2].uri.to_s)
58
-
59
- assert_equal(7, agent.history.size)
60
- agent.back
61
- assert_equal(6, agent.history.size)
62
- assert_equal("http://localhost:#{@port}/",
63
- agent.history.last.uri.to_s)
53
+ page = @agent.get("http://localhost:#{PORT}/form_test.html")
54
+
55
+ assert_equal("http://localhost:#{PORT}/form_test.html",
56
+ @agent.history.last.uri.to_s)
57
+ assert_equal("http://localhost:#{PORT}/",
58
+ @agent.history[-2].uri.to_s)
59
+
60
+ assert_equal(7, @agent.history.size)
61
+ @agent.back
62
+ assert_equal(6, @agent.history.size)
63
+ assert_equal("http://localhost:#{PORT}/",
64
+ @agent.history.last.uri.to_s)
64
65
  end
65
66
 
66
67
  def test_google
67
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
68
- page = agent.get("http://localhost:#{@port}/google.html")
68
+ page = @agent.get("http://localhost:#{PORT}/google.html")
69
69
  search = page.forms.find { |f| f.name == "f" }
70
70
  assert_not_nil(search)
71
71
  assert_not_nil(search.fields.name('q').first)
@@ -74,19 +74,17 @@ class MechMethodsTest < Test::Unit::TestCase
74
74
  end
75
75
 
76
76
  def test_click
77
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
78
- agent.user_agent_alias = 'Mac Safari'
79
- page = agent.get("http://localhost:#{@port}/frame_test.html")
77
+ @agent.user_agent_alias = 'Mac Safari'
78
+ page = @agent.get("http://localhost:#{PORT}/frame_test.html")
80
79
  link = page.links.text("Form Test").first
81
80
  assert_not_nil(link)
82
- page = agent.click(link)
83
- assert_equal("http://localhost:#{@port}/form_test.html",
84
- agent.history.last.uri.to_s)
81
+ page = @agent.click(link)
82
+ assert_equal("http://localhost:#{PORT}/form_test.html",
83
+ @agent.history.last.uri.to_s)
85
84
  end
86
85
 
87
86
  def test_new_find
88
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
89
- page = agent.get("http://localhost:#{@port}/frame_test.html")
87
+ page = @agent.get("http://localhost:#{PORT}/frame_test.html")
90
88
  assert_equal(3, page.frames.size)
91
89
 
92
90
  find_orig = page.frames.find_all { |f| f.name == 'frame1' }
@@ -102,10 +100,9 @@ class MechMethodsTest < Test::Unit::TestCase
102
100
  end
103
101
 
104
102
  def test_get_file
105
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
106
- page = agent.get("http://localhost:#{@port}/frame_test.html")
103
+ page = @agent.get("http://localhost:#{PORT}/frame_test.html")
107
104
  content_length = page.header['Content-Length']
108
- page_as_string = agent.get_file("http://localhost:#{@port}/frame_test.html")
105
+ page_as_string = @agent.get_file("http://localhost:#{PORT}/frame_test.html")
109
106
  assert_equal(content_length.to_i, page_as_string.length.to_i)
110
107
  end
111
108
  end
data/test/tc_page.rb ADDED
@@ -0,0 +1,24 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mechanize'
6
+ require 'test_includes'
7
+
8
+ class PageTest < Test::Unit::TestCase
9
+ include TestMethods
10
+
11
+ def setup
12
+ @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
13
+ end
14
+
15
+ def test_title
16
+ page = @agent.get("http://localhost:#{PORT}/file_upload.html")
17
+ assert_equal('File Upload Form', page.title)
18
+ end
19
+
20
+ def test_no_title
21
+ page = @agent.get("http://localhost:#{PORT}/no_title_test.html")
22
+ assert_equal(nil, page.title)
23
+ end
24
+ end
@@ -0,0 +1,103 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mechanize'
6
+ require 'test_includes'
7
+
8
+ class PluggableParserTest < Test::Unit::TestCase
9
+ include TestMethods
10
+
11
+ def setup
12
+ @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
13
+ end
14
+
15
+ def test_content_type_error
16
+ page = @agent.get("http://localhost:#{PORT}/bad_content_type")
17
+ page = WWW::Mechanize::Page.new(
18
+ page.uri,
19
+ page.response,
20
+ page.body,
21
+ page.code
22
+ )
23
+ assert_raise(WWW::Mechanize::ContentTypeError) {
24
+ page.root
25
+ }
26
+ begin
27
+ page.root
28
+ rescue WWW::Mechanize::ContentTypeError => ex
29
+ assert_equal('text/xml', ex.content_type)
30
+ end
31
+ end
32
+
33
+ def test_content_type
34
+ page = @agent.get("http://localhost:#{PORT}/content_type_test")
35
+ assert_kind_of(WWW::Mechanize::Page, page)
36
+ end
37
+
38
+ class Filter < WWW::Mechanize::Page
39
+ def initialize(uri=nil, response=nil, body=nil, code=nil)
40
+ super( uri,
41
+ response,
42
+ body.gsub(/<body>/, '<body><a href="http://daapclient.rubyforge.org">Net::DAAP::Client</a>'),
43
+ code
44
+ )
45
+ end
46
+ end
47
+
48
+ def test_filter
49
+ @agent.pluggable_parser.html = Filter
50
+ page = @agent.get("http://localhost:#{PORT}/find_link.html")
51
+ assert_kind_of(Filter, page)
52
+ assert_equal(16, page.links.length)
53
+ assert_not_nil(page.links.text('Net::DAAP::Client').first)
54
+ assert_equal(1, page.links.text('Net::DAAP::Client').length)
55
+ end
56
+
57
+ def test_filter_hash
58
+ @agent.pluggable_parser['text/html'] = Filter
59
+ page = @agent.get("http://localhost:#{PORT}/find_link.html")
60
+ assert_kind_of(Class, @agent.pluggable_parser['text/html'])
61
+ assert_equal(Filter, @agent.pluggable_parser['text/html'])
62
+ assert_kind_of(Filter, page)
63
+ assert_equal(16, page.links.length)
64
+ assert_not_nil(page.links.text('Net::DAAP::Client').first)
65
+ assert_equal(1, page.links.text('Net::DAAP::Client').length)
66
+ end
67
+
68
+ def test_file_saver
69
+ @agent.pluggable_parser.html = WWW::Mechanize::FileSaver
70
+ page = @agent.get('http://localhost:2000/form_no_action.html')
71
+ length = page.response['Content-Length']
72
+ file_length = nil
73
+ File.open("localhost/form_no_action.html", "r") { |f|
74
+ file_length = f.read.length
75
+ }
76
+ FileUtils.rm_rf("localhost")
77
+ assert_equal(length.to_i, file_length)
78
+ end
79
+
80
+ def test_content_type_pdf
81
+ @agent.pluggable_parser.pdf = Filter
82
+ page = @agent.get("http://localhost:#{PORT}/content_type_test?ct=application/pdf")
83
+ assert_kind_of(Class, @agent.pluggable_parser['application/pdf'])
84
+ assert_equal(Filter, @agent.pluggable_parser['application/pdf'])
85
+ assert_kind_of(Filter, page)
86
+ end
87
+
88
+ def test_content_type_csv
89
+ @agent.pluggable_parser.csv = Filter
90
+ page = @agent.get("http://localhost:#{PORT}/content_type_test?ct=text/csv")
91
+ assert_kind_of(Class, @agent.pluggable_parser['text/csv'])
92
+ assert_equal(Filter, @agent.pluggable_parser['text/csv'])
93
+ assert_kind_of(Filter, page)
94
+ end
95
+
96
+ def test_content_type_xml
97
+ @agent.pluggable_parser.xml = Filter
98
+ page = @agent.get("http://localhost:#{PORT}/content_type_test?ct=text/xml")
99
+ assert_kind_of(Class, @agent.pluggable_parser['text/xml'])
100
+ assert_equal(Filter, @agent.pluggable_parser['text/xml'])
101
+ assert_kind_of(Filter, page)
102
+ end
103
+ end
@@ -0,0 +1,41 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mechanize'
6
+ require 'test_includes'
7
+
8
+ class PostForm < Test::Unit::TestCase
9
+ include TestMethods
10
+
11
+ def setup
12
+ @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
13
+ end
14
+
15
+ def test_post_form
16
+ page = @agent.post("http://localhost:#{PORT}/form_post",
17
+ 'gender' => 'female'
18
+ )
19
+ assert_not_nil(
20
+ page.links.find { |l| l.text == "gender:female" },
21
+ "gender field missing"
22
+ )
23
+ end
24
+
25
+ def test_post_form_multival
26
+ page = @agent.post("http://localhost:#{PORT}/form_post",
27
+ [ ['gender', 'female'],
28
+ ['gender', 'male']
29
+ ]
30
+ )
31
+ assert_not_nil(
32
+ page.links.find { |l| l.text == "gender:female" },
33
+ "gender field missing"
34
+ )
35
+ assert_not_nil(
36
+ page.links.find { |l| l.text == "gender:male" },
37
+ "gender field missing"
38
+ )
39
+ assert_equal(2, page.links.length)
40
+ end
41
+ end
data/test/tc_proxy.rb ADDED
@@ -0,0 +1,25 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mechanize'
6
+ require 'test_includes'
7
+
8
+ Thread.new {
9
+ require 'proxy'
10
+ }
11
+
12
+ class ProxyTest < Test::Unit::TestCase
13
+ include TestMethods
14
+
15
+ def setup
16
+ @agent = WWW::Mechanize.new
17
+ end
18
+
19
+ def test_proxy
20
+ length = @agent.get("http://localhost:#{PORT}/find_link.html").body.length
21
+ @agent.set_proxy('127.0.0.1', PROXYPORT)
22
+ l2 = @agent.get("http://localhost:#{PORT}/find_link.html").body.length
23
+ assert_equal(length, l2)
24
+ end
25
+ end
@@ -8,22 +8,25 @@ require 'test_includes'
8
8
  class ResponseCodeMechTest < Test::Unit::TestCase
9
9
  include TestMethods
10
10
 
11
+ def setup
12
+ @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
13
+ end
14
+
11
15
  def test_redirect
12
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
13
- agent.get("http://localhost:#{@port}/response_code?code=301")
14
- assert_equal("http://localhost:#{@port}/index.html",
15
- agent.current_page.uri.to_s)
16
+ @agent.get("http://localhost:#{PORT}/response_code?code=301")
17
+ assert_equal("http://localhost:#{PORT}/index.html",
18
+ @agent.current_page.uri.to_s)
16
19
 
17
- agent.get("http://localhost:#{@port}/response_code?code=302")
18
- assert_equal("http://localhost:#{@port}/index.html",
19
- agent.current_page.uri.to_s)
20
+ @agent.get("http://localhost:#{PORT}/response_code?code=302")
21
+ assert_equal("http://localhost:#{PORT}/index.html",
22
+ @agent.current_page.uri.to_s)
20
23
  end
21
24
 
22
25
  def test_error
23
- agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
26
+ @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
24
27
  begin
25
- agent.get("http://localhost:#{@port}/response_code?code=500")
26
- rescue WWW::ResponseCodeError => err
28
+ @agent.get("http://localhost:#{PORT}/response_code?code=500")
29
+ rescue WWW::Mechanize::ResponseCodeError => err
27
30
  assert_equal("500", err.response_code)
28
31
  end
29
32
  end
@@ -0,0 +1,25 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mechanize'
6
+ require 'test_includes'
7
+ require 'fileutils'
8
+
9
+ class TestSaveFile < Test::Unit::TestCase
10
+ include TestMethods
11
+
12
+ def setup
13
+ @agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
14
+ end
15
+
16
+ def test_save_file
17
+ page = @agent.get('http://localhost:2000/form_no_action.html')
18
+ length = page.response['Content-Length']
19
+ page.save_as("test.html")
20
+ file_length = nil
21
+ File.open("test.html", "r") { |f| file_length = f.read.length }
22
+ FileUtils.rm("test.html")
23
+ assert_equal(length.to_i, file_length)
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require 'test/unit'
4
+ require 'rubygems'
5
+ require 'mechanize'
6
+ require 'test_includes'
7
+
8
+ class SSLServerTest < Test::Unit::TestCase
9
+ include TestMethods
10
+
11
+ def setup
12
+ @agent = WWW::Mechanize.new
13
+ end
14
+
15
+ def test_ssl_request
16
+ non_ssl_page = @agent.get("http://localhost:#{PORT}/form_test.html")
17
+ ssl_page = @agent.get("https://localhost:#{SSLPORT}/form_test.html")
18
+ assert_equal(non_ssl_page.body.length, ssl_page.body.length)
19
+ end
20
+
21
+ def test_ssl_request_verify
22
+ non_ssl_page = @agent.get("http://localhost:#{PORT}/form_test.html")
23
+ @agent.ca_file = 'data/server.crt'
24
+ ssl_page = @agent.get("https://localhost:#{SSLPORT}/form_test.html")
25
+ assert_equal(non_ssl_page.body.length, ssl_page.body.length)
26
+ end
27
+ end