mechanize 0.7.2 → 0.7.3

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.txt CHANGED
@@ -1,5 +1,12 @@
1
1
  = Mechanize CHANGELOG
2
2
 
3
+ == 0.7.3
4
+
5
+ * Pages are now yielded to a blocks given to WWW::Mechanize#get
6
+ * WWW::Mechanize#get now takes hash arguments for uri parameters.
7
+ * WWW::Mechanize#post takes an IO object as a parameter and posts correctly.
8
+ * Fixing a strange zlib inflate problem on windows
9
+
3
10
  == 0.7.2
4
11
 
5
12
  * Handling gzipped responses with no Content-Length header
@@ -26,7 +26,7 @@ module WWW
26
26
  attr_accessor :method, :action, :name
27
27
 
28
28
  attr_reader :fields, :buttons, :file_uploads, :radiobuttons, :checkboxes
29
- attr_reader :enctype
29
+ attr_accessor :enctype
30
30
 
31
31
  alias :elements :fields
32
32
 
data/lib/www/mechanize.rb CHANGED
@@ -38,7 +38,7 @@ module WWW
38
38
  class Mechanize
39
39
  ##
40
40
  # The version of Mechanize you are using.
41
- VERSION = '0.7.2'
41
+ VERSION = '0.7.3'
42
42
 
43
43
  ##
44
44
  # User Agent aliases
@@ -157,15 +157,35 @@ module WWW
157
157
  end
158
158
 
159
159
  # Fetches the URL passed in and returns a page.
160
- def get(url, referer=nil, &block)
161
- cur_page = referer || current_page ||
162
- Page.new( nil, {'content-type'=>'text/html'})
163
-
160
+ def get(url, parameters = [], referer = nil)
161
+ unless parameters.respond_to?(:each) # FIXME: Remove this in 0.8.0
162
+ referer = parameters
163
+ parameters = []
164
+ end
165
+
166
+ referer ||= current_page || Page.new(nil, {'content-type'=>'text/html'})
167
+
168
+ # FIXME: Huge hack so that using a URI as a referer works. I need to
169
+ # refactor everything to pass around URIs but still support
170
+ # WWW::Mechanize::Page#base
171
+ unless referer.is_a?(Page)
172
+ referer = referer.is_a?(String) ?
173
+ Page.new(URI.parse(referer), {'content-type' => 'text/html'}) :
174
+ Page.new(referer, {'content-type' => 'text/html'})
175
+ end
176
+ abs_uri = to_absolute_uri(url, referer)
177
+
178
+ if parameters.length > 0
179
+ abs_uri.query ||= ''
180
+ abs_uri.query << '&' if abs_uri.query.length > 0
181
+ abs_uri.query << self.class.build_query_string(parameters)
182
+ end
183
+
164
184
  # fetch the page
165
- abs_uri = to_absolute_uri(url, cur_page)
166
185
  request = fetch_request(abs_uri)
167
- page = fetch_page(abs_uri, request, cur_page, &block)
186
+ page = fetch_page(abs_uri, request, referer)
168
187
  add_to_history(page)
188
+ yield page if block_given?
169
189
  page
170
190
  end
171
191
 
@@ -210,7 +230,14 @@ module WWW
210
230
 
211
231
  form = Form.new(node)
212
232
  query.each { |k,v|
213
- form.fields << Form::Field.new(k,v)
233
+ if v.is_a?(IO)
234
+ form.enctype = 'multipart/form-data'
235
+ ul = Form::FileUpload.new(k.to_s,::File.basename(v.path))
236
+ ul.file_data = v.read
237
+ form.file_uploads << ul
238
+ else
239
+ form.fields << Form::Field.new(k.to_s,v)
240
+ end
214
241
  }
215
242
  post_form(url, form)
216
243
  end
@@ -537,7 +564,14 @@ module WWW
537
564
  when 'gzip'
538
565
  log.debug('gunzip body') if log
539
566
  if response['Content-Length'].to_i > 0 || body.length > 0
540
- Zlib::GzipReader.new(body).read
567
+ begin
568
+ Zlib::GzipReader.new(body).read
569
+ rescue Zlib::BufError => e
570
+ log.error('Caught a Zlib::BufError')
571
+ body.rewind
572
+ body.read(10)
573
+ Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate(body.read)
574
+ end
541
575
  else
542
576
  ''
543
577
  end
@@ -633,7 +667,7 @@ module WWW
633
667
  parameters.each { |k,v|
634
668
  next if k.nil?
635
669
  vals <<
636
- [WEBrick::HTTPUtils.escape_form(k),
670
+ [WEBrick::HTTPUtils.escape_form(k.to_s),
637
671
  WEBrick::HTTPUtils.escape_form(v.to_s)].join("=")
638
672
  }
639
673
 
data/test/tc_mech.rb CHANGED
@@ -5,6 +5,27 @@ class TestMechMethods < Test::Unit::TestCase
5
5
  @agent = WWW::Mechanize.new
6
6
  end
7
7
 
8
+ def test_get_with_params
9
+ page = @agent.get('http://localhost/', { :q => 'hello' })
10
+ assert_equal('http://localhost/?q=hello', page.uri.to_s)
11
+ end
12
+
13
+ def test_get_with_referer
14
+ class << @agent
15
+ attr_reader :request
16
+ alias :old_set_headers :set_headers
17
+ def set_headers(u, request, cur_page)
18
+ old_set_headers(u, request, cur_page)
19
+ @request = request
20
+ end
21
+ end
22
+ @agent.get('http://localhost/', URI.parse('http://google.com/'))
23
+ assert_equal 'http://google.com/', @agent.request['Referer']
24
+
25
+ @agent.get('http://localhost/', [], 'http://tenderlovemaking.com/')
26
+ assert_equal 'http://tenderlovemaking.com/', @agent.request['Referer']
27
+ end
28
+
8
29
  def test_weird_url
9
30
  assert_nothing_raised {
10
31
  @agent.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
data/test/tc_page.rb CHANGED
@@ -5,6 +5,15 @@ class TestPage < Test::Unit::TestCase
5
5
  @agent = WWW::Mechanize.new
6
6
  end
7
7
 
8
+ def test_page_gets_yielded
9
+ pages = nil
10
+ @agent.get("http://localhost/file_upload.html") { |page|
11
+ pages = page
12
+ }
13
+ assert pages
14
+ assert_equal('File Upload Form', pages.title)
15
+ end
16
+
8
17
  def test_title
9
18
  page = @agent.get("http://localhost/file_upload.html")
10
19
  assert_equal('File Upload Form', page.title)
data/test/tc_upload.rb CHANGED
@@ -6,6 +6,18 @@ class UploadMechTest < Test::Unit::TestCase
6
6
  @page = @agent.get("http://localhost/file_upload.html")
7
7
  end
8
8
 
9
+ def test_upload_with_post_param
10
+ page = @agent.post('http://localhost/file_upload', {
11
+ :name => 'Some file',
12
+ :userfile1 => File.open(__FILE__)
13
+ })
14
+ assert_match(
15
+ "Content-Disposition: form-data; name=\"userfile1\"; filename=\"#{File.basename(__FILE__)}\"",
16
+ page.body
17
+ )
18
+ assert page.body.length > File.read(__FILE__).length
19
+ end
20
+
9
21
  def test_form_enctype
10
22
  assert_equal('multipart/form-data', @page.forms[0].enctype)
11
23
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mechanize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-06 00:00:00 -08:00
12
+ date: 2008-03-12 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency