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.

@@ -14,6 +14,8 @@
14
14
  <!-- End escaped bug -->
15
15
  <a href="unusual&&%3F%3F%23%23.html">unusual characters</a>
16
16
 
17
+ <a href="">empty href</a>
18
+
17
19
  <a href="javascript:new_page('1')">javascript link</a>
18
20
  </body>
19
21
  </html>
@@ -275,16 +275,45 @@ but not <a href="/" rel="me nofollow">this</a>!
275
275
  assert_equal 'GET', page.header['X-Request-Method']
276
276
  end
277
277
 
278
- #def test_download
279
- # Dir.mktmpdir do |dir|
280
- # file = "#{dir}/download"
281
- # open file, 'w' do |io|
282
- # @mech.download 'http://example', io
283
- # end
278
+ def test_download
279
+ page = nil
280
+
281
+ in_tmpdir do
282
+ open 'download', 'w' do |io|
283
+ page = @mech.download 'http://example', io
284
+
285
+ refute io.closed?
286
+ end
287
+
288
+ assert_operator 1, :<=, File.stat('download').size
289
+ end
290
+
291
+ assert_empty @mech.history
292
+ assert_kind_of Mechanize::Page, page
293
+ end
294
+
295
+ def test_download_filename
296
+ page = nil
284
297
 
285
- # assert_equal 1, File.stat(file).size
286
- # end
287
- #end
298
+ in_tmpdir do
299
+ page = @mech.download 'http://example', 'download'
300
+
301
+ assert_operator 1, :<=, File.stat('download').size
302
+ end
303
+
304
+ assert_empty @mech.history
305
+ assert_kind_of Mechanize::Page, page
306
+ end
307
+
308
+ def test_download_filename_error
309
+ in_tmpdir do
310
+ assert_raises Mechanize::UnauthorizedError do
311
+ @mech.download 'http://example/digest_auth', 'download'
312
+ end
313
+
314
+ refute File.exist? 'download'
315
+ end
316
+ end
288
317
 
289
318
  def test_get
290
319
  uri = URI 'http://localhost'
@@ -957,7 +986,7 @@ but not <a href="/" rel="me nofollow">this</a>!
957
986
  assert_equal 'user', @mech.proxy_user
958
987
  assert_equal 'pass', @mech.proxy_pass
959
988
 
960
- refute_same http, @mech.agent.http
989
+ assert_equal URI('http://user:pass@localhost:8080'), http.proxy_uri
961
990
  end
962
991
 
963
992
  def test_submit_bad_form_method
@@ -0,0 +1,49 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizeDirectorySaver < Mechanize::TestCase
4
+
5
+ def setup
6
+ super
7
+
8
+ @uri = URI 'http://example/relative/tc_relative_links.html'
9
+ @io = StringIO.new 'hello world'
10
+ end
11
+
12
+ def test_self_save_to
13
+ in_tmpdir do
14
+ saver = Mechanize::DirectorySaver.save_to 'dir'
15
+
16
+ saver.new @uri, nil, @io, 200
17
+
18
+ assert File.exist? 'dir/tc_relative_links.html'
19
+ refute File.exist? 'dir/relative'
20
+ end
21
+ end
22
+
23
+ def test_self_save_to_cd
24
+ in_tmpdir do
25
+ saver = Mechanize::DirectorySaver.save_to 'dir'
26
+
27
+ FileUtils.mkdir 'other'
28
+
29
+ Dir.chdir 'other' do
30
+ saver.new @uri, nil, @io, 200
31
+ end
32
+
33
+ assert File.exist? 'dir/tc_relative_links.html'
34
+ refute File.exist? 'dir/relative'
35
+ end
36
+ end
37
+
38
+ def test_initialize_no_save_dir
39
+ in_tmpdir do
40
+ e = assert_raises Mechanize::Error do
41
+ Mechanize::DirectorySaver.new @uri, nil, @io, 200
42
+ end
43
+
44
+ assert_match %r%no save directory specified%, e.message
45
+ end
46
+ end
47
+
48
+ end
49
+
@@ -2,17 +2,23 @@ require 'mechanize/test_case'
2
2
 
3
3
  class TestMechanizeFileRequest < Mechanize::TestCase
4
4
 
5
- def test_initialize
6
- uri = URI.parse 'http://example/'
5
+ def setup
6
+ @uri = URI.parse 'file:///nonexistent'
7
7
 
8
- r = Mechanize::FileRequest.new uri
8
+ @r = Mechanize::FileRequest.new @uri
9
+ end
9
10
 
10
- assert_equal uri, r.uri
11
- assert_equal '/', r.path
11
+ def test_initialize
12
+ assert_equal @uri, @r.uri
13
+ assert_equal '/nonexistent', @r.path
14
+
15
+ assert_respond_to @r, :[]=
16
+ assert_respond_to @r, :add_field
17
+ assert_respond_to @r, :each_header
18
+ end
12
19
 
13
- assert_respond_to r, :[]=
14
- assert_respond_to r, :add_field
15
- assert_respond_to r, :each_header
20
+ def test_response_body_permitted_eh
21
+ assert @r.response_body_permitted?
16
22
  end
17
23
 
18
24
  end
@@ -31,6 +31,25 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
31
31
  realm
32
32
  end
33
33
 
34
+ def test_certificate_equals
35
+ cert_path = File.expand_path '../data/server.crt', __FILE__
36
+ cert = OpenSSL::X509::Certificate.new File.read cert_path
37
+
38
+ @agent.certificate = cert
39
+
40
+ assert_equal cert.to_pem, @agent.certificate.to_pem
41
+ end
42
+
43
+ def test_certificate_equals_file
44
+ cert_path = File.expand_path '../data/server.crt', __FILE__
45
+
46
+ cert = OpenSSL::X509::Certificate.new File.read cert_path
47
+
48
+ @agent.certificate = cert_path
49
+
50
+ assert_equal cert.to_pem, @agent.certificate.to_pem
51
+ end
52
+
34
53
  def test_connection_for_file
35
54
  uri = URI.parse 'file:///nonexistent'
36
55
  conn = @agent.connection_for uri
@@ -72,20 +91,18 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
72
91
  assert_equal 'identity', @req['accept-encoding']
73
92
  end
74
93
 
75
- def test_fetch_hooks
76
- @agent.pre_connect_hooks << proc do |agent, request|
77
- assert_equal '/index.html', request.path
78
- assert_equal @agent, agent
79
- end
94
+ def test_fetch_file_nonexistent
95
+ in_tmpdir do
96
+ nonexistent = File.join Dir.pwd, 'nonexistent'
80
97
 
81
- @agent.post_connect_hooks << proc do |agent, uri, response, body|
82
- assert_equal @agent, agent
83
- assert_equal URI('http://example/index.html'), uri
84
- assert_equal '200', response.code
85
- assert_kind_of String, body
86
- end
98
+ uri = URI.parse "file://#{nonexistent}"
87
99
 
88
- @agent.fetch URI 'http://example/index.html'
100
+ e = assert_raises Mechanize::ResponseCodeError do
101
+ @agent.fetch uri
102
+ end
103
+
104
+ assert_equal '404 => Net::HTTPNotFound', e.message
105
+ end
89
106
  end
90
107
 
91
108
  def test_fetch_file_plus
@@ -114,18 +131,28 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
114
131
  assert_kind_of Mechanize::Page, page
115
132
  end
116
133
 
117
- def test_fetch_file_nonexistent
118
- in_tmpdir do
119
- nonexistent = File.join Dir.pwd, 'nonexistent'
134
+ def test_fetch_head_gzip
135
+ uri = @uri + '/gzip?file=index.html'
120
136
 
121
- uri = URI.parse "file://#{nonexistent}"
137
+ page = @agent.fetch uri, :head
122
138
 
123
- e = assert_raises Mechanize::ResponseCodeError do
124
- @agent.fetch uri
125
- end
139
+ assert_kind_of Mechanize::Page, page
140
+ end
126
141
 
127
- assert_equal '404 => Net::HTTPNotFound', e.message
142
+ def test_fetch_hooks
143
+ @agent.pre_connect_hooks << proc do |agent, request|
144
+ assert_equal '/index.html', request.path
145
+ assert_equal @agent, agent
146
+ end
147
+
148
+ @agent.post_connect_hooks << proc do |agent, uri, response, body|
149
+ assert_equal @agent, agent
150
+ assert_equal URI('http://example/index.html'), uri
151
+ assert_equal '200', response.code
152
+ assert_kind_of String, body
128
153
  end
154
+
155
+ @agent.fetch URI 'http://example/index.html'
129
156
  end
130
157
 
131
158
  def test_fetch_post_connect_hook
@@ -145,6 +172,88 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
145
172
  assert_equal '500', e.response_code
146
173
  end
147
174
 
175
+ def test_get_meta_refresh_header_follow_self
176
+ @agent.follow_meta_refresh = true
177
+ @agent.follow_meta_refresh_self = true
178
+
179
+ page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, '',
180
+ 200, @mech)
181
+ @res.instance_variable_set :@header, 'refresh' => ['0']
182
+
183
+ refresh = @agent.get_meta_refresh @res, @uri, page
184
+
185
+ assert_equal [0.0, URI('http://example/')], refresh
186
+ end
187
+
188
+ def test_get_meta_refresh_header_no_follow
189
+ page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, '',
190
+ 200, @mech)
191
+ @res.instance_variable_set :@header, 'refresh' => ['0']
192
+
193
+ refresh = @agent.get_meta_refresh @res, @uri, page
194
+
195
+ assert_nil refresh
196
+ end
197
+
198
+ def test_get_meta_refresh_header_no_follow_self
199
+ @agent.follow_meta_refresh = true
200
+
201
+ page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, '',
202
+ 200, @mech)
203
+ @res.instance_variable_set :@header, 'refresh' => ['0']
204
+
205
+ refresh = @agent.get_meta_refresh @res, @uri, page
206
+
207
+ assert_nil refresh
208
+ end
209
+
210
+ def test_get_meta_refresh_meta_follow_self
211
+ @agent.follow_meta_refresh = true
212
+ @agent.follow_meta_refresh_self = true
213
+
214
+ body = <<-BODY
215
+ <title></title>
216
+ <meta http-equiv="refresh" content="0">
217
+ BODY
218
+
219
+ page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, body,
220
+ 200, @mech)
221
+
222
+ refresh = @agent.get_meta_refresh @res, @uri, page
223
+
224
+ assert_equal [0, nil], refresh
225
+ end
226
+
227
+ def test_get_meta_refresh_meta_no_follow
228
+ body = <<-BODY
229
+ <title></title>
230
+ <meta http-equiv="refresh" content="0">
231
+ BODY
232
+
233
+ page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, body,
234
+ 200, @mech)
235
+
236
+ refresh = @agent.get_meta_refresh @res, @uri, page
237
+
238
+ assert_nil refresh
239
+ end
240
+
241
+ def test_get_meta_refresh_meta_no_follow_self
242
+ @agent.follow_meta_refresh = true
243
+
244
+ body = <<-BODY
245
+ <title></title>
246
+ <meta http-equiv="refresh" content="0">
247
+ BODY
248
+
249
+ page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, body,
250
+ 200, @mech)
251
+
252
+ refresh = @agent.get_meta_refresh @res, @uri, page
253
+
254
+ assert_nil refresh
255
+ end
256
+
148
257
  def test_get_robots
149
258
  robotstxt = @agent.get_robots 'http://localhost/robots.txt'
150
259
  refute_equal '', robotstxt
@@ -153,6 +262,17 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
153
262
  assert_equal '', robotstxt
154
263
  end
155
264
 
265
+ def test_hook_content_encoding_response
266
+ @mech.content_encoding_hooks << lambda{|agent, uri, response, response_body_io|
267
+ response['content-encoding'] = 'gzip' if response['content-encoding'] == 'agzip'}
268
+
269
+ @res.instance_variable_set :@header, 'content-encoding' => %w[agzip]
270
+ body_io = StringIO.new 'part'
271
+ @agent.hook_content_encoding @res, @uri, body_io
272
+
273
+ assert_equal 'gzip', @res['content-encoding']
274
+ end
275
+
156
276
  def test_http_request_file
157
277
  uri = URI.parse 'file:///nonexistent'
158
278
  request = @agent.http_request uri, :get
@@ -176,7 +296,6 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
176
296
  end
177
297
 
178
298
  def test_idle_timeout_equals
179
- @agent.set_http
180
299
  @agent.idle_timeout = 1
181
300
 
182
301
  assert_equal 1, @agent.http.idle_timeout
@@ -211,69 +330,6 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
211
330
  end
212
331
  end
213
332
 
214
- def test_request_cookies
215
- uri = URI.parse 'http://host.example.com'
216
- Mechanize::Cookie.parse uri, 'hello=world domain=.example.com' do |cookie|
217
- @agent.cookie_jar.add uri, cookie
218
- end
219
-
220
- @agent.request_cookies @req, uri
221
-
222
- assert_equal 'hello=world domain=.example.com', @req['Cookie']
223
- end
224
-
225
- def test_request_cookies_none
226
- @agent.request_cookies @req, @uri
227
-
228
- assert_nil @req['Cookie']
229
- end
230
-
231
- def test_request_cookies_many
232
- uri = URI.parse 'http://host.example.com'
233
- cookie_str = 'a=b domain=.example.com, c=d domain=.example.com'
234
- Mechanize::Cookie.parse uri, cookie_str do |cookie|
235
- @agent.cookie_jar.add uri, cookie
236
- end
237
-
238
- @agent.request_cookies @req, uri
239
-
240
- expected = cookie_str.sub ', ', '; '
241
-
242
- assert_equal expected, @req['Cookie']
243
- end
244
-
245
- def test_request_cookies_wrong_domain
246
- uri = URI.parse 'http://host.example.com'
247
- Mechanize::Cookie.parse uri, 'hello=world domain=.example.com' do |cookie|
248
- @agent.cookie_jar.add uri, cookie
249
- end
250
-
251
- @agent.request_cookies @req, @uri
252
-
253
- assert_nil @req['Cookie']
254
- end
255
-
256
- def test_request_host
257
- @agent.request_host @req, @uri
258
-
259
- assert_equal 'example', @req['host']
260
- end
261
-
262
- def test_request_host_nonstandard
263
- @uri.port = 81
264
-
265
- @agent.request_host @req, @uri
266
-
267
- assert_equal 'example:81', @req['host']
268
- end
269
-
270
- def test_request_language_charset
271
- @agent.request_language_charset @req
272
-
273
- assert_equal 'en-us,en;q=0.5', @req['accept-language']
274
- assert_equal 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', @req['accept-charset']
275
- end
276
-
277
333
  def test_request_add_headers
278
334
  @agent.request_add_headers @req, 'Content-Length' => 300
279
335
 
@@ -314,12 +370,6 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
314
370
  assert_equal 'unknown header symbol content_length', e.message
315
371
  end
316
372
 
317
- def test_request_auth_none
318
- @agent.request_auth @req, @uri
319
-
320
- assert_nil @req['Authorization']
321
- end
322
-
323
373
  def test_request_auth_basic
324
374
  @agent.user = 'user'
325
375
  @agent.password = 'password'
@@ -357,38 +407,107 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
357
407
  assert_match %r%qop=auth%, @req['Authorization']
358
408
  end
359
409
 
360
- def test_request_referer
361
- referer = URI.parse 'http://old.example'
362
-
363
- @agent.request_referer @req, @uri, referer
410
+ def test_request_auth_none
411
+ @agent.request_auth @req, @uri
364
412
 
365
- assert_equal 'http://old.example', @req['referer']
413
+ assert_nil @req['Authorization']
366
414
  end
367
415
 
368
- def test_request_referer_https
369
- uri = URI.parse 'https://example'
370
- referer = URI.parse 'https://old.example'
416
+ def test_request_cookies
417
+ uri = URI.parse 'http://host.example.com'
418
+ Mechanize::Cookie.parse uri, 'hello=world domain=.example.com' do |cookie|
419
+ @agent.cookie_jar.add uri, cookie
420
+ end
371
421
 
372
- @agent.request_referer @req, uri, referer
422
+ @agent.request_cookies @req, uri
373
423
 
374
- assert_equal 'https://old.example', @req['referer']
424
+ assert_equal 'hello=world domain=.example.com', @req['Cookie']
375
425
  end
376
426
 
377
- def test_request_referer_https_downgrade
378
- referer = URI.parse 'https://old.example'
379
-
380
- @agent.request_referer @req, @uri, referer
381
-
382
- assert_nil @req['referer']
383
- end
427
+ def test_request_cookies_many
428
+ uri = URI.parse 'http://host.example.com'
429
+ cookie_str = 'a=b domain=.example.com, c=d domain=.example.com'
430
+ Mechanize::Cookie.parse uri, cookie_str do |cookie|
431
+ @agent.cookie_jar.add uri, cookie
432
+ end
384
433
 
385
- def test_request_referer_https_downgrade_case
386
- uri = URI.parse 'http://example'
387
- referer = URI.parse 'httpS://old.example'
434
+ @agent.request_cookies @req, uri
388
435
 
389
- @agent.request_referer @req, uri, referer
436
+ expected = cookie_str.sub ', ', '; '
390
437
 
391
- assert_nil @req['referer']
438
+ assert_equal expected, @req['Cookie']
439
+ end
440
+
441
+ def test_request_cookies_none
442
+ @agent.request_cookies @req, @uri
443
+
444
+ assert_nil @req['Cookie']
445
+ end
446
+
447
+ def test_request_cookies_wrong_domain
448
+ uri = URI.parse 'http://host.example.com'
449
+ Mechanize::Cookie.parse uri, 'hello=world domain=.example.com' do |cookie|
450
+ @agent.cookie_jar.add uri, cookie
451
+ end
452
+
453
+ @agent.request_cookies @req, @uri
454
+
455
+ assert_nil @req['Cookie']
456
+ end
457
+
458
+ def test_request_host
459
+ @agent.request_host @req, @uri
460
+
461
+ assert_equal 'example', @req['host']
462
+ end
463
+
464
+ def test_request_host_nonstandard
465
+ @uri.port = 81
466
+
467
+ @agent.request_host @req, @uri
468
+
469
+ assert_equal 'example:81', @req['host']
470
+ end
471
+
472
+ def test_request_language_charset
473
+ @agent.request_language_charset @req
474
+
475
+ assert_equal 'en-us,en;q=0.5', @req['accept-language']
476
+ assert_equal 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', @req['accept-charset']
477
+ end
478
+
479
+ def test_request_referer
480
+ referer = URI.parse 'http://old.example'
481
+
482
+ @agent.request_referer @req, @uri, referer
483
+
484
+ assert_equal 'http://old.example', @req['referer']
485
+ end
486
+
487
+ def test_request_referer_https
488
+ uri = URI.parse 'https://example'
489
+ referer = URI.parse 'https://old.example'
490
+
491
+ @agent.request_referer @req, uri, referer
492
+
493
+ assert_equal 'https://old.example', @req['referer']
494
+ end
495
+
496
+ def test_request_referer_https_downgrade
497
+ referer = URI.parse 'https://old.example'
498
+
499
+ @agent.request_referer @req, @uri, referer
500
+
501
+ assert_nil @req['referer']
502
+ end
503
+
504
+ def test_request_referer_https_downgrade_case
505
+ uri = URI.parse 'http://example'
506
+ referer = URI.parse 'httpS://old.example'
507
+
508
+ @agent.request_referer @req, uri, referer
509
+
510
+ assert_nil @req['referer']
392
511
  end
393
512
 
394
513
  def test_request_referer_https_upgrade
@@ -761,16 +880,6 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
761
880
  body_io.close! unless body_io.closed?
762
881
  end
763
882
 
764
- def test_response_content_encoding_x_gzip
765
- @res.instance_variable_set :@header, 'content-encoding' => %w[x-gzip]
766
- body_io = StringIO.new \
767
- "\037\213\b\0002\002\225M\000\003+H,*\001\000\306p\017I\004\000\000\000"
768
-
769
- body = @agent.response_content_encoding @res, body_io
770
-
771
- assert_equal 'part', body.read
772
- end
773
-
774
883
  def test_response_content_encoding_unknown
775
884
  @res.instance_variable_set :@header, 'content-encoding' => %w[unknown]
776
885
  body = StringIO.new 'part'
@@ -782,97 +891,14 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
782
891
  assert_equal 'unsupported content-encoding: unknown', e.message
783
892
  end
784
893
 
785
- def test_get_meta_refresh_header_follow_self
786
- @agent.follow_meta_refresh = true
787
- @agent.follow_meta_refresh_self = true
788
-
789
- page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, '',
790
- 200, @mech)
791
- @res.instance_variable_set :@header, 'refresh' => ['0']
792
-
793
- refresh = @agent.get_meta_refresh @res, @uri, page
794
-
795
- assert_equal [0.0, URI('http://example/')], refresh
796
- end
797
-
798
- def test_get_meta_refresh_header_no_follow
799
- page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, '',
800
- 200, @mech)
801
- @res.instance_variable_set :@header, 'refresh' => ['0']
802
-
803
- refresh = @agent.get_meta_refresh @res, @uri, page
804
-
805
- assert_nil refresh
806
- end
807
-
808
- def test_get_meta_refresh_header_no_follow_self
809
- @agent.follow_meta_refresh = true
810
-
811
- page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, '',
812
- 200, @mech)
813
- @res.instance_variable_set :@header, 'refresh' => ['0']
814
-
815
- refresh = @agent.get_meta_refresh @res, @uri, page
816
-
817
- assert_nil refresh
818
- end
819
-
820
- def test_get_meta_refresh_meta_follow_self
821
- @agent.follow_meta_refresh = true
822
- @agent.follow_meta_refresh_self = true
823
-
824
- body = <<-BODY
825
- <title></title>
826
- <meta http-equiv="refresh" content="0">
827
- BODY
828
-
829
- page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, body,
830
- 200, @mech)
831
-
832
- refresh = @agent.get_meta_refresh @res, @uri, page
833
-
834
- assert_equal [0, 'http://example/'], refresh
835
- end
836
-
837
- def test_get_meta_refresh_meta_no_follow
838
- body = <<-BODY
839
- <title></title>
840
- <meta http-equiv="refresh" content="0">
841
- BODY
842
-
843
- page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, body,
844
- 200, @mech)
845
-
846
- refresh = @agent.get_meta_refresh @res, @uri, page
847
-
848
- assert_nil refresh
849
- end
850
-
851
- def test_get_meta_refresh_meta_no_follow_self
852
- @agent.follow_meta_refresh = true
853
-
854
- body = <<-BODY
855
- <title></title>
856
- <meta http-equiv="refresh" content="0">
857
- BODY
858
-
859
- page = Mechanize::Page.new(@uri, {'content-type' => 'text/html'}, body,
860
- 200, @mech)
861
-
862
- refresh = @agent.get_meta_refresh @res, @uri, page
863
-
864
- assert_nil refresh
865
- end
866
-
867
- def test_hook_content_encoding_response
868
- @mech.content_encoding_hooks << lambda{|agent, uri, response, response_body_io|
869
- response['content-encoding'] = 'gzip' if response['content-encoding'] == 'agzip'}
894
+ def test_response_content_encoding_x_gzip
895
+ @res.instance_variable_set :@header, 'content-encoding' => %w[x-gzip]
896
+ body_io = StringIO.new \
897
+ "\037\213\b\0002\002\225M\000\003+H,*\001\000\306p\017I\004\000\000\000"
870
898
 
871
- @res.instance_variable_set :@header, 'content-encoding' => %w[agzip]
872
- body_io = StringIO.new 'part'
873
- @agent.hook_content_encoding @res, @uri, body_io
899
+ body = @agent.response_content_encoding @res, body_io
874
900
 
875
- assert_equal 'gzip', @res['content-encoding']
901
+ assert_equal 'part', body.read
876
902
  end
877
903
 
878
904
  def test_response_cookies
@@ -963,38 +989,102 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
963
989
  end
964
990
  end
965
991
 
966
- def test_response_read
967
- def @res.read_body() yield 'part' end
968
- def @res.content_length() 4 end
992
+ def test_response_parse
993
+ body = '<title>hi</title>'
994
+ @res.instance_variable_set :@header, 'content-type' => %w[text/html]
969
995
 
970
- io = @agent.response_read @res, @req, @uri
996
+ page = @agent.response_parse @res, body, @uri
971
997
 
972
- body = io.read
998
+ assert_instance_of Mechanize::Page, page
999
+ assert_equal @mech, page.mech
1000
+ end
973
1001
 
974
- assert_equal 'part', body
975
- assert_equal Encoding::BINARY, body.encoding if body.respond_to? :encoding
1002
+ def test_response_parse_content_type_case
1003
+ body = '<title>hi</title>'
1004
+ @res.instance_variable_set(:@header, 'content-type' => %w[text/HTML])
1005
+
1006
+ page = @agent.response_parse @res, body, @uri
1007
+
1008
+ assert_instance_of Mechanize::Page, page
1009
+
1010
+ assert_equal 'text/HTML', page.content_type
976
1011
  end
977
1012
 
978
- def test_response_read_large
979
- def @res.read_body() yield 'a' * 10241 end
980
- def @res.content_length() 10241 end
1013
+ def test_response_parse_content_type_encoding
1014
+ body = '<title>hi</title>'
1015
+ @res.instance_variable_set(:@header,
1016
+ 'content-type' =>
1017
+ %w[text/html;charset=ISO-8859-1])
981
1018
 
982
- io = @agent.response_read @res, @req, @uri
1019
+ page = @agent.response_parse @res, body, @uri
983
1020
 
984
- assert_kind_of Tempfile, io
985
- assert_equal 10241, io.stat.size
1021
+ assert_instance_of Mechanize::Page, page
1022
+ assert_equal @mech, page.mech
1023
+
1024
+ assert_equal 'ISO-8859-1', page.encoding
1025
+ assert_equal 'ISO-8859-1', page.parser.encoding
986
1026
  end
987
1027
 
988
- def test_response_read_large_chunked
989
- def @res.read_body
990
- 11.times do yield 'a' * 1024 end
991
- end
992
- def @res.content_length() end
1028
+ def test_response_parse_content_type_encoding_broken_iso_8859_1
1029
+ body = '<title>hi</title>'
1030
+ @res.instance_variable_set(:@header,
1031
+ 'content-type' =>
1032
+ %w[text/html; charset=ISO_8859-1])
1033
+
1034
+ page = @agent.response_parse @res, body, @uri
1035
+
1036
+ assert_instance_of Mechanize::Page, page
1037
+ assert_equal 'ISO_8859-1', page.encoding
1038
+ end
1039
+
1040
+ def test_response_parse_content_type_encoding_broken_utf_8
1041
+ body = '<title>hi</title>'
1042
+ @res.instance_variable_set(:@header,
1043
+ 'content-type' =>
1044
+ %w[text/html; charset=UTF8])
1045
+
1046
+ page = @agent.response_parse @res, body, @uri
1047
+
1048
+ assert_instance_of Mechanize::Page, page
1049
+ assert_equal 'UTF8', page.encoding
1050
+ assert_equal 'UTF8', page.parser.encoding
1051
+ end
1052
+
1053
+ def test_response_parse_content_type_encoding_garbage
1054
+ body = '<title>hi</title>'
1055
+ @res.instance_variable_set(:@header,
1056
+ 'content-type' =>
1057
+ %w[text/html; charset=garbage_charset])
1058
+
1059
+ page = @agent.response_parse @res, body, @uri
1060
+
1061
+ assert_instance_of Mechanize::Page, page
1062
+ assert_equal @mech, page.mech
1063
+ end
1064
+
1065
+ def test_response_parse_content_type_encoding_semicolon
1066
+ body = '<title>hi</title>'
1067
+ @res.instance_variable_set(:@header,
1068
+ 'content-type' =>
1069
+ %w[text/html;charset=UTF-8;])
1070
+
1071
+ page = @agent.response_parse @res, body, @uri
1072
+
1073
+ assert_instance_of Mechanize::Page, page
1074
+
1075
+ assert_equal 'UTF-8', page.encoding
1076
+ end
1077
+
1078
+ def test_response_read
1079
+ def @res.read_body() yield 'part' end
1080
+ def @res.content_length() 4 end
993
1081
 
994
1082
  io = @agent.response_read @res, @req, @uri
995
1083
 
996
- assert_kind_of Tempfile, io
997
- assert_equal 11264, io.stat.size
1084
+ body = io.read
1085
+
1086
+ assert_equal 'part', body
1087
+ assert_equal Encoding::BINARY, body.encoding if body.respond_to? :encoding
998
1088
  end
999
1089
 
1000
1090
  def test_response_read_content_length_head
@@ -1068,6 +1158,28 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
1068
1158
  end
1069
1159
  end
1070
1160
 
1161
+ def test_response_read_large
1162
+ def @res.read_body() yield 'a' * 10241 end
1163
+ def @res.content_length() 10241 end
1164
+
1165
+ io = @agent.response_read @res, @req, @uri
1166
+
1167
+ assert_kind_of Tempfile, io
1168
+ assert_equal 10241, io.stat.size
1169
+ end
1170
+
1171
+ def test_response_read_large_chunked
1172
+ def @res.read_body
1173
+ 11.times do yield 'a' * 1024 end
1174
+ end
1175
+ def @res.content_length() end
1176
+
1177
+ io = @agent.response_read @res, @req, @uri
1178
+
1179
+ assert_kind_of Tempfile, io
1180
+ assert_equal 11264, io.stat.size
1181
+ end
1182
+
1071
1183
  def test_response_read_no_body
1072
1184
  req = Net::HTTP::Options.new '/'
1073
1185
 
@@ -1105,6 +1217,19 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
1105
1217
  assert_equal 'http://example/referer', requests.first['Referer']
1106
1218
  end
1107
1219
 
1220
+ def test_response_redirect_malformed
1221
+ @agent.redirect_ok = true
1222
+ referer = page 'http://example/referer'
1223
+
1224
+ page = fake_page
1225
+ page = @agent.response_redirect({ 'Location' => '/index.html?q=あ' }, :get,
1226
+ page, 0, referer)
1227
+
1228
+ assert_equal URI('http://fake.example/index.html?q=%E3%81%82'), page.uri
1229
+
1230
+ assert_equal 'http://example/referer', requests.first['Referer']
1231
+ end
1232
+
1108
1233
  def test_response_redirect_limit
1109
1234
  @agent.redirect_ok = true
1110
1235
  referer = page 'http://example/referer'
@@ -1149,90 +1274,12 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
1149
1274
  assert_equal URI('http://fake.example/'), page.uri
1150
1275
  end
1151
1276
 
1152
- def test_response_parse
1153
- body = '<title>hi</title>'
1154
- @res.instance_variable_set :@header, 'content-type' => %w[text/html]
1155
-
1156
- page = @agent.response_parse @res, body, @uri
1157
-
1158
- assert_instance_of Mechanize::Page, page
1159
- assert_equal @mech, page.mech
1160
- end
1161
-
1162
- def test_response_parse_content_type_case
1163
- body = '<title>hi</title>'
1164
- @res.instance_variable_set(:@header, 'content-type' => %w[text/HTML])
1165
-
1166
- page = @agent.response_parse @res, body, @uri
1167
-
1168
- assert_instance_of Mechanize::Page, page
1169
-
1170
- assert_equal 'text/HTML', page.content_type
1171
- end
1172
-
1173
- def test_response_parse_content_type_encoding
1174
- body = '<title>hi</title>'
1175
- @res.instance_variable_set(:@header,
1176
- 'content-type' =>
1177
- %w[text/html;charset=ISO-8859-1])
1178
-
1179
- page = @agent.response_parse @res, body, @uri
1180
-
1181
- assert_instance_of Mechanize::Page, page
1182
- assert_equal @mech, page.mech
1183
-
1184
- assert_equal 'ISO-8859-1', page.encoding
1185
- assert_equal 'ISO-8859-1', page.parser.encoding
1186
- end
1187
-
1188
- def test_response_parse_content_type_encoding_garbage
1189
- body = '<title>hi</title>'
1190
- @res.instance_variable_set(:@header,
1191
- 'content-type' =>
1192
- %w[text/html; charset=garbage_charset])
1193
-
1194
- page = @agent.response_parse @res, body, @uri
1195
-
1196
- assert_instance_of Mechanize::Page, page
1197
- assert_equal @mech, page.mech
1198
- end
1199
-
1200
- def test_response_parse_content_type_encoding_broken_iso_8859_1
1201
- body = '<title>hi</title>'
1202
- @res.instance_variable_set(:@header,
1203
- 'content-type' =>
1204
- %w[text/html; charset=ISO_8859-1])
1205
-
1206
- page = @agent.response_parse @res, body, @uri
1207
-
1208
- assert_instance_of Mechanize::Page, page
1209
- assert_equal 'ISO_8859-1', page.encoding
1210
- end
1211
-
1212
- def test_response_parse_content_type_encoding_broken_utf_8
1213
- body = '<title>hi</title>'
1214
- @res.instance_variable_set(:@header,
1215
- 'content-type' =>
1216
- %w[text/html; charset=UTF8])
1217
-
1218
- page = @agent.response_parse @res, body, @uri
1219
-
1220
- assert_instance_of Mechanize::Page, page
1221
- assert_equal 'UTF8', page.encoding
1222
- assert_equal 'UTF8', page.parser.encoding
1223
- end
1224
-
1225
- def test_response_parse_content_type_encoding_semicolon
1226
- body = '<title>hi</title>'
1227
- @res.instance_variable_set(:@header,
1228
- 'content-type' =>
1229
- %w[text/html;charset=UTF-8;])
1230
-
1231
- page = @agent.response_parse @res, body, @uri
1277
+ def test_retry_change_request_equals
1278
+ refute @agent.http.retry_change_requests
1232
1279
 
1233
- assert_instance_of Mechanize::Page, page
1280
+ @agent.retry_change_requests = true
1234
1281
 
1235
- assert_equal 'UTF-8', page.encoding
1282
+ assert @agent.http.retry_change_requests
1236
1283
  end
1237
1284
 
1238
1285
  def test_robots_allowed_eh
@@ -1258,63 +1305,6 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
1258
1305
  end
1259
1306
  end
1260
1307
 
1261
- def test_set_http
1262
- @agent.set_http
1263
-
1264
- assert_equal 'mechanize', @agent.http.name
1265
- refute @agent.http.retry_change_requests
1266
- end
1267
-
1268
- def test_set_http_idle_timeout
1269
- @agent.idle_timeout = 1
1270
- @agent.set_http
1271
-
1272
- assert_equal 'mechanize', @agent.http.name
1273
- assert_equal 1, @agent.http.idle_timeout
1274
- end
1275
-
1276
- def test_set_http_ssl
1277
- in_tmpdir do
1278
- store = OpenSSL::X509::Store.new
1279
- @agent.cert = ssl_certificate
1280
- @agent.key = ssl_private_key
1281
- @agent.cert_store = store
1282
- @agent.ca_file = '.'
1283
- @agent.verify_callback = proc { |ok, context| }
1284
-
1285
- @agent.set_http
1286
-
1287
- http = @agent.http
1288
-
1289
- assert_equal ssl_certificate, http.certificate
1290
- assert_equal ssl_private_key, http.private_key
1291
- assert_equal store, http.cert_store
1292
- assert_equal '.', http.ca_file
1293
- assert_equal OpenSSL::SSL::VERIFY_PEER, http.verify_mode
1294
- assert http.verify_callback
1295
- end
1296
- end
1297
-
1298
- def test_set_http_ssl_verify_none
1299
- in_tmpdir do
1300
- @agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
1301
-
1302
- @agent.set_http
1303
-
1304
- http = @agent.http
1305
-
1306
- assert_equal OpenSSL::SSL::VERIFY_NONE, http.verify_mode
1307
- end
1308
- end
1309
-
1310
- def test_set_http_retry_change_request
1311
- @agent.retry_change_requests = true
1312
- @agent.set_http
1313
-
1314
- assert_equal 'mechanize', @agent.http.name
1315
- assert @agent.http.retry_change_requests
1316
- end
1317
-
1318
1308
  def test_set_proxy
1319
1309
  @agent.set_proxy 'www.example.com', 9001, 'joe', 'lol'
1320
1310
 
@@ -1350,5 +1340,36 @@ class TestMechanizeHttpAgent < Mechanize::TestCase
1350
1340
  assert_equal 'invalid value for port: "nonexistent service"', e.message
1351
1341
  end
1352
1342
 
1343
+ def test_ssl
1344
+ in_tmpdir do
1345
+ store = OpenSSL::X509::Store.new
1346
+ @agent.ca_file = '.'
1347
+ @agent.cert_store = store
1348
+ @agent.certificate = ssl_certificate
1349
+ @agent.private_key = ssl_private_key
1350
+ @agent.ssl_version = 'SSLv3' if RUBY_VERSION > '1.9'
1351
+ @agent.verify_callback = proc { |ok, context| }
1352
+
1353
+ http = @agent.http
1354
+
1355
+ assert_equal '.', http.ca_file
1356
+ assert_equal store, http.cert_store
1357
+ assert_equal ssl_certificate, http.certificate
1358
+ assert_equal ssl_private_key, http.private_key
1359
+ assert_equal 'SSLv3', http.ssl_version if
1360
+ RUBY_VERSION > '1.9'
1361
+ assert_equal OpenSSL::SSL::VERIFY_PEER, http.verify_mode
1362
+ assert http.verify_callback
1363
+ end
1364
+ end
1365
+
1366
+ def test_verify_none_equals
1367
+ @agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
1368
+
1369
+ http = @agent.http
1370
+
1371
+ assert_equal OpenSSL::SSL::VERIFY_NONE, http.verify_mode
1372
+ end
1373
+
1353
1374
  end
1354
1375