mechanize 0.4.6 → 0.4.7

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.

data/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ == 0.4.7
2
+
3
+ * Fixed bug with no action in forms. Thanks to Adam Wiggins
4
+ * Setting a default user-agent string
5
+ * Added house cleaning to the cookie jar so expired cookies don't stick around.
6
+ * Added new method WWW::Form#field to find the first field with a given name.
7
+ (thanks to Gregory Brown)
8
+ * Added WWW::Mechanize#get_file for fetching non text/html files
9
+
1
10
  == 0.4.6
2
11
 
3
12
  * Added support for proxies
data/NOTES CHANGED
@@ -1,5 +1,14 @@
1
1
  = Mechanize Release Notes
2
2
 
3
+ == 0.4.7
4
+
5
+ This release of mechanize comes with a few bug fixes including fixing a
6
+ bug when there is no action specified in a form.
7
+
8
+ In this version, a default user agent string is now set for mechanize. Also
9
+ a convenience method WWW::Mechanize#get_file has been added for fetching
10
+ non text/html files.
11
+
3
12
  == 0.4.6
4
13
 
5
14
  The 0.4.6 release comes with proxy support which can be enabled by calling
@@ -66,6 +66,7 @@ class Mechanize
66
66
  'Mac Mozilla' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4a) Gecko/20030401',
67
67
  'Linux Mozilla' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624',
68
68
  'Linux Konqueror' => 'Mozilla/5.0 (compatible; Konqueror/3; Linux)',
69
+ 'Mechanize' => "WWW-Mechanize/#{WWW::MechVersion} (http://rubyforge.org/projects/mechanize/)"
69
70
  }
70
71
 
71
72
  attr_accessor :log
@@ -80,7 +81,7 @@ class Mechanize
80
81
 
81
82
  def initialize
82
83
  @history = []
83
- @user_agent = nil
84
+ @user_agent = AGENT_ALIASES['Mechanize']
84
85
  @user = nil
85
86
  @open_timeout = nil
86
87
  @read_timeout = nil
@@ -139,6 +140,11 @@ class Mechanize
139
140
  page
140
141
  end
141
142
 
143
+ # Fetch a file and return the contents
144
+ def get_file(url)
145
+ get(url).body
146
+ end
147
+
142
148
  # Posts to the given URL wht the query parameters passed in.
143
149
  def post(url, query={})
144
150
  cur_page = current_page() || Page.new
@@ -132,10 +132,13 @@ module WWW
132
132
  end
133
133
 
134
134
  @jar[cookie.domain][cookie.name] = cookie
135
+ cleanup()
136
+ cookie
135
137
  end
136
138
 
137
139
  # Fetch the cookies that should be used for the URI object passed in.
138
140
  def cookies(url)
141
+ cleanup
139
142
  cookies = []
140
143
  @jar.each_key do |domain|
141
144
  if url.host =~ /#{domain}$/
@@ -157,5 +160,19 @@ module WWW
157
160
  def empty?(url)
158
161
  cookies(url).length > 0 ? false : true
159
162
  end
163
+
164
+ private
165
+
166
+ # Remove expired cookies
167
+ def cleanup
168
+ @jar.each_key do |domain|
169
+ @jar[domain].each_key do |name|
170
+ if ! @jar[domain][name].expires.nil? &&
171
+ DateTime.now > @jar[domain][name].expires
172
+ @jar[domain].delete(name)
173
+ end
174
+ end
175
+ end
176
+ end
160
177
  end
161
178
  end
@@ -194,5 +194,10 @@ module WWW
194
194
  @node = node
195
195
  super(@node, @node)
196
196
  end
197
+
198
+ # Fetch the first field whose name is equal to field_name
199
+ def field(field_name)
200
+ fields.find { |f| f.name.eql? field_name }
201
+ end
197
202
  end
198
203
  end
@@ -1,5 +1,5 @@
1
1
  # DO NOT EDIT
2
2
  # This file is auto-generated by build scripts
3
3
  module WWW
4
- MechVersion = '0.4.6'
4
+ MechVersion = '0.4.7'
5
5
  end
@@ -24,6 +24,8 @@ module WWW
24
24
  attr_finder :frames, :iframes, :links, :forms, :meta, :watches
25
25
  attr_reader :body_filter
26
26
 
27
+ alias :content :body
28
+
27
29
  # Alias our finders so that we can lazily parse the html
28
30
  alias :find_frames :frames
29
31
  alias :find_iframes :iframes
@@ -159,7 +161,9 @@ module WWW
159
161
 
160
162
  case name
161
163
  when 'form'
162
- @forms << Form.new(node)
164
+ form = Form.new(node)
165
+ form.action ||= @uri
166
+ @forms << form
163
167
  when 'a'
164
168
  @links << Link.new(node)
165
169
  when 'meta'
@@ -0,0 +1,18 @@
1
+ <html>
2
+ <head><title>Page Title</title></head>
3
+ <body>
4
+ <form name="get_form" method="get">
5
+ <table>
6
+ <tr>
7
+ <td>First Name</td>
8
+ <td><input name="first" type="text" /></td>
9
+ </tr>
10
+ <tr>
11
+ <td><input type="submit" value="Submit" /></td>
12
+ <td></td>
13
+ </tr>
14
+ </table>
15
+ </form>
16
+ </body>
17
+ </html>
18
+
@@ -129,6 +129,46 @@ class CookieClassTest < Test::Unit::TestCase
129
129
  end
130
130
  end
131
131
 
132
+ # Test using secure cookies
133
+ def test_cookie_with_secure
134
+ url = URI.parse('http://rubyforge.org/')
135
+ cookie_params = {}
136
+ cookie_params['expires'] = 'expires=Sun, 27-Sep-2037 00:00:00 GMT'
137
+ cookie_params['path'] = 'path=/'
138
+ cookie_params['domain'] = 'domain=.rubyforge.org'
139
+ cookie_params['secure'] = 'secure'
140
+ cookie_value = '12345%7D=ASDFWEE345%3DASda'
141
+
142
+ expires = DateTime.strptime('Sun, 27-Sep-2037 00:00:00 GMT',
143
+ '%a, %d-%b-%Y %T %Z')
144
+
145
+ cookie_params.keys.combine.each do |c|
146
+ next unless c.find { |k| k == 'secure' }
147
+ cookie_text = "#{cookie_value}; "
148
+ c.each_with_index do |key, idx|
149
+ if idx == (c.length - 1)
150
+ cookie_text << "#{cookie_params[key]}"
151
+ else
152
+ cookie_text << "#{cookie_params[key]}; "
153
+ end
154
+ end
155
+ cookie = nil
156
+ WWW::Cookie.parse(url, cookie_text) { |p_cookie| cookie = p_cookie }
157
+ assert_not_nil(cookie)
158
+ assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
159
+ assert_equal('rubyforge.org', cookie.domain)
160
+ assert_equal('/', cookie.path)
161
+ assert_equal(true, cookie.secure)
162
+
163
+ # if expires was set, make sure we parsed it
164
+ if c.find { |k| k == 'expires' }
165
+ assert_equal(expires, cookie.expires)
166
+ else
167
+ assert_nil(cookie.expires)
168
+ end
169
+ end
170
+ end
171
+
132
172
  # If no domain was given, we must use the one from the URL
133
173
  def test_cookie_with_url_domain
134
174
  url = URI.parse('http://login.rubyforge.org/')
@@ -33,7 +33,7 @@ class CookiesMechTest < Test::Unit::TestCase
33
33
  def test_many_cookies_as_string
34
34
  agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
35
35
  page = agent.get("http://localhost:#{@port}/many_cookies_as_string")
36
- assert_equal(5, agent.cookies.length)
36
+ assert_equal(4, agent.cookies.length)
37
37
 
38
38
  name_cookie = agent.cookies.find { |k| k.name == "name" }
39
39
  assert_not_nil(name_cookie, "Name cookie was nil")
@@ -42,10 +42,7 @@ class CookiesMechTest < Test::Unit::TestCase
42
42
  assert_equal(true, DateTime.now < name_cookie.expires)
43
43
 
44
44
  expired_cookie = agent.cookies.find { |k| k.name == "expired" }
45
- assert_not_nil(expired_cookie, "Expired cookie was nil")
46
- assert_equal("doh", expired_cookie.value)
47
- assert_equal("/", expired_cookie.path)
48
- assert_equal(true, DateTime.now > expired_cookie.expires)
45
+ assert_nil(expired_cookie, "Expired cookie was not nil")
49
46
 
50
47
  no_exp_cookie = agent.cookies.find { |k| k.name == "no_expires" }
51
48
  assert_not_nil(no_exp_cookie, "No expires cookie is nil")
@@ -68,7 +65,7 @@ class CookiesMechTest < Test::Unit::TestCase
68
65
  def test_many_cookies
69
66
  agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
70
67
  page = agent.get("http://localhost:#{@port}/many_cookies")
71
- assert_equal(5, agent.cookies.length)
68
+ assert_equal(4, agent.cookies.length)
72
69
 
73
70
  name_cookie = agent.cookies.find { |k| k.name == "name" }
74
71
  assert_not_nil(name_cookie, "Name cookie was nil")
@@ -77,10 +74,7 @@ class CookiesMechTest < Test::Unit::TestCase
77
74
  assert_equal(true, DateTime.now < name_cookie.expires)
78
75
 
79
76
  expired_cookie = agent.cookies.find { |k| k.name == "expired" }
80
- assert_not_nil(expired_cookie, "Expired cookie was nil")
81
- assert_equal("doh", expired_cookie.value)
82
- assert_equal("/", expired_cookie.path)
83
- assert_equal(true, DateTime.now > expired_cookie.expires)
77
+ assert_nil(expired_cookie, "Expired cookie was not nil")
84
78
 
85
79
  no_exp_cookie = agent.cookies.find { |k| k.name == "no_expires" }
86
80
  assert_not_nil(no_exp_cookie, "No expires cookie is nil")
@@ -9,6 +9,13 @@ require 'test_includes'
9
9
  class FormsMechTest < Test::Unit::TestCase
10
10
  include TestMethods
11
11
 
12
+ def test_no_form_action
13
+ mech = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
14
+ page = mech.get('http://localhost:2000/form_no_action.html')
15
+ page.forms.first.fields.first.value = 'Aaron'
16
+ page = mech.submit(page.forms.first)
17
+ assert_match('/form_no_action.html?first=Aaron', page.uri.to_s)
18
+ end
12
19
  # Test submitting form with two fields of the same name
13
20
  def test_post_multival
14
21
  agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
@@ -367,13 +374,13 @@ class FormsMechTest < Test::Unit::TestCase
367
374
  "Gender female button was nil"
368
375
  )
369
376
 
370
- assert_not_nil(post_form.checkboxes.find { |f| f.name == "cool person" },
377
+ assert_not_nil(post_form.checkboxes.name("cool person").first,
371
378
  "couldn't find cool person checkbox")
372
379
  assert_not_nil(post_form.checkboxes.find { |f| f.name == "likes ham" },
373
380
  "couldn't find likes ham checkbox")
374
381
 
375
382
  # Now set all the fields
376
- post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
383
+ post_form.fields.name('first_name').value = "Aaron"
377
384
  post_form.radiobuttons.find { |f|
378
385
  f.name == "gender" && f.value == "male"
379
386
  }.checked = true
@@ -456,4 +463,12 @@ class FormsMechTest < Test::Unit::TestCase
456
463
  "one field missing"
457
464
  )
458
465
  end
466
+
467
+ def test_field_addition
468
+ agent = WWW::Mechanize.new { |a| a.log = Logger.new(nil) }
469
+ page = agent.get("http://localhost:#{@port}/form_test.html")
470
+ get_form = page.forms.find { |f| f.name == "get_form1" }
471
+ get_form.field("first_name").value = "Gregory"
472
+ assert_equal( "Gregory", get_form.field("first_name").value )
473
+ end
459
474
  end
@@ -100,4 +100,12 @@ class MechMethodsTest < Test::Unit::TestCase
100
100
  assert_equal(a, d)
101
101
  }
102
102
  end
103
+
104
+ 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")
107
+ content_length = page.header['Content-Length']
108
+ page_as_string = agent.get_file("http://localhost:#{@port}/frame_test.html")
109
+ assert_equal(content_length.to_i, page_as_string.length.to_i)
110
+ end
103
111
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11.13
3
3
  specification_version: 1
4
4
  name: mechanize
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.4.6
7
- date: 2006-06-02 00:00:00 -07:00
6
+ version: 0.4.7
7
+ date: 2006-06-19 00:00:00 -07:00
8
8
  summary: Mechanize provides automated web-browsing
9
9
  require_paths:
10
10
  - lib
@@ -60,6 +60,7 @@ files:
60
60
  - test/htdocs/find_link.html
61
61
  - test/htdocs/index.html
62
62
  - test/htdocs/google.html
63
+ - test/htdocs/form_no_action.html
63
64
  - test/htdocs/file_upload.html
64
65
  - test/htdocs/iframe_test.html
65
66
  - test/data/htpasswd