mechanize 2.1.1 → 2.2

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.

@@ -0,0 +1,8 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizeImage < Mechanize::TestCase
4
+
5
+ # empty subclass, no tests
6
+
7
+ end
8
+
@@ -34,6 +34,14 @@ class TestMechanizeLink < Mechanize::TestCase
34
34
  # HACK no assertion
35
35
  end
36
36
 
37
+ def test_click_empty_href
38
+ page = @mech.get("http://google.com/tc_links.html?q=test#anchor")
39
+ link = page.link_with(:text => 'empty href')
40
+
41
+ new_page = link.click
42
+ assert_equal "http://google.com/tc_links.html?q=test", new_page.uri.to_s
43
+ end
44
+
37
45
  def test_text_alt_text
38
46
  page = @mech.get("http://localhost/alt_text.html")
39
47
  assert_equal(5, page.links.length)
@@ -61,25 +61,26 @@ class TestMechanizePage < Mechanize::TestCase
61
61
  assert_equal "File Upload Form", page.iframes.first.content.title
62
62
  end
63
63
 
64
- def test_images
64
+ def test_image_with
65
65
  page = html_page <<-BODY
66
66
  <img src="a.jpg">
67
+ <img src="b.jpg">
68
+ <img src="c.png">
67
69
  BODY
68
70
 
69
- assert_equal page.images.first.url, "http://example/a.jpg"
71
+ assert_equal "http://example/b.jpg",
72
+ page.image_with(:src => 'b.jpg').url.to_s
70
73
  end
71
74
 
72
- def test_images_base
75
+ def test_images_with
73
76
  page = html_page <<-BODY
74
- <head>
75
- <base href="http://other.example/">
76
- </head>
77
- <body>
78
- <img src="a.jpg">
79
- </body>
77
+ <img src="a.jpg">
78
+ <img src="b.jpg">
79
+ <img src="c.png">
80
80
  BODY
81
81
 
82
- assert_equal page.images.first.url, "http://other.example/a.jpg"
82
+ images = page.images_with(:src => /jpg\Z/).map { |img| img.url.to_s }
83
+ assert_equal %w[http://example/a.jpg http://example/b.jpg], images
83
84
  end
84
85
 
85
86
  def test_links
@@ -0,0 +1,183 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizePageImage < Mechanize::TestCase
4
+
5
+ def setup
6
+ super
7
+
8
+ @uri = URI 'http://example/'
9
+ @src = (@uri + 'a.jpg').to_s
10
+
11
+ @empty_page = Mechanize::Page.new(@uri, { 'content-type' => 'text/html' },
12
+ '', 200, @mech)
13
+ end
14
+
15
+ def img attributes
16
+ img = node 'img', attributes
17
+
18
+ Mechanize::Page::Image.new img, @empty_page
19
+ end
20
+
21
+ def test_initialize
22
+ image = img("src" => "a.jpg", "alt" => "alt", "width" => "100",
23
+ "height" => "200", "title" => "title", "id" => "id",
24
+ "class" => "class")
25
+
26
+ assert_equal "a.jpg", image.src
27
+ assert_equal "alt", image.alt
28
+ assert_equal "100", image.width
29
+ assert_equal "200", image.height
30
+ assert_equal "title", image.title
31
+ assert_equal "id", image.dom_id
32
+ assert_equal "class", image.dom_class
33
+ end
34
+
35
+ def test_initialize_no_attributes
36
+ image = img({})
37
+
38
+ assert_nil image.src
39
+ assert_nil image.alt
40
+ assert_nil image.width
41
+ assert_nil image.height
42
+ assert_nil image.title
43
+ assert_nil image.dom_id
44
+ assert_nil image.dom_class
45
+ end
46
+
47
+ def test_caption
48
+ assert_equal "", img("src" => @src).caption
49
+ assert_equal "alt", img("src" => @src, "alt" => "alt").caption
50
+ assert_equal "title", img("src" => @src, "title" => "title").caption
51
+ assert_equal "title", img("src" => @src,
52
+ "alt" => "alt", "title" => "title").caption
53
+ end
54
+
55
+ def test_url
56
+ assert_equal "http://example/a.jpg", img('src' => @src).url
57
+ end
58
+
59
+ def test_url_base
60
+ page = html_page <<-BODY
61
+ <head>
62
+ <base href="http://other.example/">
63
+ </head>
64
+ <body>
65
+ <img src="a.jpg">
66
+ </body>
67
+ BODY
68
+
69
+ assert_equal "http://other.example/a.jpg", page.images.first.url
70
+ end
71
+
72
+ def test_extname
73
+ assert_equal ".jpg", img("src" => "a.jpg").extname
74
+ assert_equal ".PNG", img("src" => "a.PNG").extname
75
+ assert_equal ".aaa", img("src" => "unknown.aaa").extname
76
+ assert_equal "", img("src" => "nosuffiximage").extname
77
+
78
+ assert_nil img("width" => "1", "height" => "1").extname
79
+
80
+ assert_equal ".jpg", img("src" => "a.jpg?cache_buster").extname
81
+ end
82
+
83
+ def test_mime_type
84
+ assert_equal "image/jpeg", img("src" => "a.jpg").mime_type
85
+ assert_equal "image/png", img("src" => "a.PNG").mime_type
86
+
87
+ assert_nil img("src" => "unknown.aaa").mime_type
88
+ assert_nil img("src" => "nosuffiximage").mime_type
89
+ end
90
+
91
+ def test_fetch
92
+ image = img "src" => "http://localhost/button.jpg"
93
+
94
+ fetched = image.fetch
95
+
96
+ assert_equal fetched, @mech.page
97
+ assert_equal "http://localhost/button.jpg", fetched.uri.to_s
98
+ assert_equal "http://example/", requests.first['Referer']
99
+ assert @mech.visited? "http://localhost/button.jpg"
100
+ end
101
+
102
+ def test_fetch_referer_http_page_rel_src
103
+ # | rel-src http-src https-src
104
+ # http page | *page* page page
105
+ # https page | page empty empty
106
+ page = html_page '<img src="./button.jpg">'
107
+ page.images.first.fetch
108
+
109
+ assert_equal 'http', page.uri.scheme
110
+ assert_equal true, page.images.first.relative?
111
+ assert_equal "http://example/", requests.first['Referer']
112
+ end
113
+
114
+ def test_fetch_referer_http_page_abs_src
115
+ # | rel-src http-src https-src
116
+ # http page | page *page* *page*
117
+ # https page | page empty empty
118
+ page = html_page '<img src="http://localhost/button.jpg">'
119
+ page.images.first.fetch
120
+
121
+ assert_equal 'http', page.uri.scheme
122
+ assert_equal false, page.images.first.relative?
123
+ assert_equal "http://example/", requests.first['Referer']
124
+ end
125
+
126
+ def test_fetch_referer_https_page_rel_src
127
+ # | rel-src http-src https-src
128
+ # http page | page page page
129
+ # https page | *page* empty empty
130
+ page = html_page '<img src="./button.jpg">'
131
+ page.uri = URI 'https://example/'
132
+ page.images.first.fetch
133
+
134
+ assert_equal 'https', page.uri.scheme
135
+ assert_equal true, page.images.first.relative?
136
+ assert_equal "https://example/", requests.first['Referer']
137
+ end
138
+
139
+ def test_fetch_referer_https_page_abs_src
140
+ # | rel-src http-src https-src
141
+ # http page | page page page
142
+ # https page | page *empty* *empty*
143
+ page = html_page '<img src="http://localhost/button.jpg">'
144
+ page.uri = URI 'https://example/'
145
+ page.images.first.fetch
146
+
147
+ assert_equal 'https', page.uri.scheme
148
+ assert_equal false, page.images.first.relative?
149
+ assert_equal nil, requests.first['Referer']
150
+ end
151
+
152
+ def test_image_referer_http_page_abs_src
153
+ page = html_page '<img src="http://localhost/button.jpg">'
154
+
155
+ assert_equal 'http', page.uri.scheme
156
+ assert_equal @uri, page.images.first.image_referer.uri
157
+ end
158
+
159
+ def test_image_referer_http_page_rel_src
160
+ page = html_page '<img src="./button.jpg">'
161
+
162
+ assert_equal 'http', page.uri.scheme
163
+ assert_equal @uri, page.images.first.image_referer.uri
164
+ end
165
+
166
+ def test_image_referer_https_page_abs_src
167
+ page = html_page '<img src="http://localhost/button.jpg">'
168
+ page.uri = URI 'https://example/'
169
+
170
+ assert_equal 'https', page.uri.scheme
171
+ assert_nil page.images.first.image_referer.uri
172
+ end
173
+
174
+ def test_image_referer_https_page_rel_src
175
+ page = html_page '<img src="./button.jpg">'
176
+ page.uri = URI 'https://example/'
177
+
178
+ assert_equal 'https', page.uri.scheme
179
+ assert_equal URI('https://example/'), page.images.first.image_referer.uri
180
+ end
181
+
182
+ end
183
+
@@ -21,7 +21,7 @@ class TestMechanizePageMetaRefresh < Mechanize::TestCase
21
21
 
22
22
  def util_meta_refresh page
23
23
  node = page.search('meta').first
24
- @MR.from_node node, page, @uri
24
+ @MR.from_node node, page
25
25
  end
26
26
 
27
27
  def test_class_parse
@@ -63,10 +63,20 @@ class TestMechanizePageMetaRefresh < Mechanize::TestCase
63
63
  assert_equal "http://example/here/", uri.to_s
64
64
  assert link_self
65
65
 
66
+ delay, uri, link_self = @MR.parse " 0 ; ", @uri
67
+ assert_equal "0", delay
68
+ assert_equal "http://example/here/", uri.to_s
69
+ assert link_self
70
+
66
71
  delay, uri, link_self = @MR.parse "0; UrL=http://localhost:8080/path", @uri
67
72
  assert_equal "0", delay
68
73
  assert_equal "http://localhost:8080/path", uri.to_s
69
74
  refute link_self
75
+
76
+ delay, uri, link_self = @MR.parse "0 ; UrL = http://localhost:8080/path", @uri
77
+ assert_equal "0", delay
78
+ assert_equal "http://localhost:8080/path", uri.to_s
79
+ refute link_self
70
80
  end
71
81
 
72
82
  def test_class_parse_funky
@@ -93,17 +103,17 @@ class TestMechanizePageMetaRefresh < Mechanize::TestCase
93
103
  page = util_page 5, 'test'
94
104
  link = util_meta_refresh page
95
105
  assert_equal 5, link.delay
96
- assert_equal 'http://example/here/test', link.href
106
+ assert_equal 'test', link.href
97
107
 
98
108
  page = util_page 5, '/test'
99
109
  link = util_meta_refresh page
100
110
  assert_equal 5, link.delay
101
- assert_equal 'http://example/test', link.href
111
+ assert_equal '/test', link.href
102
112
 
103
113
  page = util_page 5, nil
104
114
  link = util_meta_refresh page
105
115
  assert_equal 5, link.delay
106
- assert_equal 'http://example/here/', link.href
116
+ assert_equal nil, link.href
107
117
 
108
118
  page = util_page 5, @uri
109
119
  link = util_meta_refresh page
@@ -133,5 +143,11 @@ class TestMechanizePageMetaRefresh < Mechanize::TestCase
133
143
  assert_nil util_meta_refresh page
134
144
  end
135
145
 
146
+ def test_meta_refresh_click_sends_no_referer
147
+ page = util_page 0, '/referer'
148
+ link = util_meta_refresh page
149
+ refreshed = link.click
150
+ assert_equal '', refreshed.body
151
+ end
136
152
  end
137
153
 
@@ -32,6 +32,21 @@ class TestMechanizePluggableParser < Mechanize::TestCase
32
32
  assert_nil @pp['text/xml']
33
33
 
34
34
  assert_equal Mechanize::File, @pp.parser('text/xml')
35
+ assert_equal Mechanize::File, @pp.parser(nil)
36
+ end
37
+
38
+ def test_parser_mime
39
+ @pp['image/png'] = :png
40
+
41
+ assert_equal :png, @pp.parser('x-image/x-png')
42
+ assert_equal :png, @pp.parser('image/png')
43
+ assert_equal Mechanize::Image, @pp.parser('image')
44
+ end
45
+
46
+ def test_parser_bogus
47
+ assert_nil @pp['bogus']
48
+
49
+ assert_equal Mechanize::File, @pp.parser('bogus')
35
50
  end
36
51
 
37
52
  def test_pdf
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mechanize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 1
9
- - 1
10
- version: 2.1.1
8
+ - 2
9
+ version: "2.2"
11
10
  platform: ruby
12
11
  authors:
13
12
  - Eric Hodel
@@ -39,7 +38,7 @@ cert_chain:
39
38
  YJY7T/W2n+eWy8WuPhzVUkyzguj0bQe27NDeabgCh2mHd4Hynk2AkYh8MQ==
40
39
  -----END CERTIFICATE-----
41
40
 
42
- date: 2012-02-04 00:00:00 Z
41
+ date: 2012-02-13 00:00:00 Z
43
42
  dependencies:
44
43
  - !ruby/object:Gem::Dependency
45
44
  name: net-http-digest_auth
@@ -72,25 +71,48 @@ dependencies:
72
71
  requirements:
73
72
  - - ~>
74
73
  - !ruby/object:Gem::Version
75
- hash: 11
74
+ hash: 9
76
75
  segments:
77
76
  - 2
78
- - 4
79
- version: "2.4"
77
+ - 5
78
+ version: "2.5"
80
79
  - - ">="
81
80
  - !ruby/object:Gem::Version
82
- hash: 29
81
+ hash: 31
83
82
  segments:
84
83
  - 2
85
- - 4
86
- - 1
87
- version: 2.4.1
84
+ - 5
85
+ - 2
86
+ version: 2.5.2
88
87
  type: :runtime
89
88
  version_requirements: *id002
90
89
  - !ruby/object:Gem::Dependency
91
- name: nokogiri
90
+ name: mime-types
92
91
  prerelease: false
93
92
  requirement: &id003 !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ~>
96
+ - !ruby/object:Gem::Version
97
+ hash: 45
98
+ segments:
99
+ - 1
100
+ - 17
101
+ version: "1.17"
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 87
105
+ segments:
106
+ - 1
107
+ - 17
108
+ - 2
109
+ version: 1.17.2
110
+ type: :runtime
111
+ version_requirements: *id003
112
+ - !ruby/object:Gem::Dependency
113
+ name: nokogiri
114
+ prerelease: false
115
+ requirement: &id004 !ruby/object:Gem::Requirement
94
116
  none: false
95
117
  requirements:
96
118
  - - ~>
@@ -101,11 +123,11 @@ dependencies:
101
123
  - 4
102
124
  version: "1.4"
103
125
  type: :runtime
104
- version_requirements: *id003
126
+ version_requirements: *id004
105
127
  - !ruby/object:Gem::Dependency
106
128
  name: ntlm-http
107
129
  prerelease: false
108
- requirement: &id004 !ruby/object:Gem::Requirement
130
+ requirement: &id005 !ruby/object:Gem::Requirement
109
131
  none: false
110
132
  requirements:
111
133
  - - ~>
@@ -124,11 +146,11 @@ dependencies:
124
146
  - 1
125
147
  version: 0.1.1
126
148
  type: :runtime
127
- version_requirements: *id004
149
+ version_requirements: *id005
128
150
  - !ruby/object:Gem::Dependency
129
151
  name: webrobots
130
152
  prerelease: false
131
- requirement: &id005 !ruby/object:Gem::Requirement
153
+ requirement: &id006 !ruby/object:Gem::Requirement
132
154
  none: false
133
155
  requirements:
134
156
  - - ~>
@@ -147,11 +169,11 @@ dependencies:
147
169
  - 9
148
170
  version: 0.0.9
149
171
  type: :runtime
150
- version_requirements: *id005
172
+ version_requirements: *id006
151
173
  - !ruby/object:Gem::Dependency
152
174
  name: domain_name
153
175
  prerelease: false
154
- requirement: &id006 !ruby/object:Gem::Requirement
176
+ requirement: &id007 !ruby/object:Gem::Requirement
155
177
  none: false
156
178
  requirements:
157
179
  - - ~>
@@ -170,11 +192,11 @@ dependencies:
170
192
  - 1
171
193
  version: 0.5.1
172
194
  type: :runtime
173
- version_requirements: *id006
195
+ version_requirements: *id007
174
196
  - !ruby/object:Gem::Dependency
175
197
  name: minitest
176
198
  prerelease: false
177
- requirement: &id007 !ruby/object:Gem::Requirement
199
+ requirement: &id008 !ruby/object:Gem::Requirement
178
200
  none: false
179
201
  requirements:
180
202
  - - ~>
@@ -185,11 +207,11 @@ dependencies:
185
207
  - 10
186
208
  version: "2.10"
187
209
  type: :development
188
- version_requirements: *id007
210
+ version_requirements: *id008
189
211
  - !ruby/object:Gem::Dependency
190
212
  name: rdoc
191
213
  prerelease: false
192
- requirement: &id008 !ruby/object:Gem::Requirement
214
+ requirement: &id009 !ruby/object:Gem::Requirement
193
215
  none: false
194
216
  requirements:
195
217
  - - ~>
@@ -200,11 +222,11 @@ dependencies:
200
222
  - 10
201
223
  version: "3.10"
202
224
  type: :development
203
- version_requirements: *id008
225
+ version_requirements: *id009
204
226
  - !ruby/object:Gem::Dependency
205
227
  name: hoe
206
228
  prerelease: false
207
- requirement: &id009 !ruby/object:Gem::Requirement
229
+ requirement: &id010 !ruby/object:Gem::Requirement
208
230
  none: false
209
231
  requirements:
210
232
  - - ~>
@@ -215,7 +237,7 @@ dependencies:
215
237
  - 13
216
238
  version: "2.13"
217
239
  type: :development
218
- version_requirements: *id009
240
+ version_requirements: *id010
219
241
  description: |-
220
242
  The Mechanize library is used for automating interaction with websites.
221
243
  Mechanize automatically stores and sends cookies, follows redirects,
@@ -288,6 +310,7 @@ files:
288
310
  - lib/mechanize/http/auth_realm.rb
289
311
  - lib/mechanize/http/content_disposition_parser.rb
290
312
  - lib/mechanize/http/www_authenticate_parser.rb
313
+ - lib/mechanize/image.rb
291
314
  - lib/mechanize/monkey_patch.rb
292
315
  - lib/mechanize/page.rb
293
316
  - lib/mechanize/page/base.rb
@@ -385,20 +408,23 @@ files:
385
408
  - test/test_mechanize_http_auth_realm.rb
386
409
  - test/test_mechanize_http_content_disposition_parser.rb
387
410
  - test/test_mechanize_http_www_authenticate_parser.rb
411
+ - test/test_mechanize_image.rb
388
412
  - test/test_mechanize_link.rb
389
413
  - test/test_mechanize_page.rb
390
414
  - test/test_mechanize_page_encoding.rb
391
415
  - test/test_mechanize_page_frame.rb
416
+ - test/test_mechanize_page_image.rb
392
417
  - test/test_mechanize_page_link.rb
393
418
  - test/test_mechanize_page_meta_refresh.rb
394
419
  - test/test_mechanize_parser.rb
395
420
  - test/test_mechanize_pluggable_parser.rb
396
421
  - test/test_mechanize_redirect_limit_reached_error.rb
397
422
  - test/test_mechanize_redirect_not_get_or_head_error.rb
423
+ - test/test_mechanize_response_read_error.rb
398
424
  - test/test_mechanize_subclass.rb
399
425
  - test/test_mechanize_util.rb
400
426
  - test/test_multi_select.rb
401
- - test/test_mechanize_response_read_error.rb
427
+ - test/test_mechanize_directory_saver.rb
402
428
  - .gemtest
403
429
  homepage: http://mechanize.rubyforge.org
404
430
  licenses: []
@@ -440,6 +466,7 @@ test_files:
440
466
  - test/test_mechanize.rb
441
467
  - test/test_mechanize_cookie.rb
442
468
  - test/test_mechanize_cookie_jar.rb
469
+ - test/test_mechanize_directory_saver.rb
443
470
  - test/test_mechanize_download.rb
444
471
  - test/test_mechanize_file.rb
445
472
  - test/test_mechanize_file_connection.rb
@@ -464,10 +491,12 @@ test_files:
464
491
  - test/test_mechanize_http_auth_realm.rb
465
492
  - test/test_mechanize_http_content_disposition_parser.rb
466
493
  - test/test_mechanize_http_www_authenticate_parser.rb
494
+ - test/test_mechanize_image.rb
467
495
  - test/test_mechanize_link.rb
468
496
  - test/test_mechanize_page.rb
469
497
  - test/test_mechanize_page_encoding.rb
470
498
  - test/test_mechanize_page_frame.rb
499
+ - test/test_mechanize_page_image.rb
471
500
  - test/test_mechanize_page_link.rb
472
501
  - test/test_mechanize_page_meta_refresh.rb
473
502
  - test/test_mechanize_parser.rb