mechanize 0.6.5 → 0.6.6
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 +10 -0
- data/Manifest.txt +2 -1
- data/lib/mechanize.rb +99 -92
- data/lib/mechanize/cookie.rb +5 -3
- data/lib/mechanize/form.rb +1 -1
- data/lib/mechanize/form_elements.rb +1 -1
- data/lib/mechanize/history.rb +62 -0
- data/lib/mechanize/page_elements.rb +6 -2
- data/lib/mechanize/rexml.rb +3 -3
- data/test/htdocs/alt_text.html +1 -0
- data/test/tc_cookie_class.rb +1 -1
- data/test/tc_history.rb +125 -0
- data/test/tc_links.rb +2 -1
- data/test/tc_mech.rb +14 -1
- data/test/test_all.rb +1 -0
- metadata +4 -3
- data/lib/mechanize/hpricot.rb +0 -9
data/CHANGELOG.txt
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
= Mechanize CHANGELOG
|
2
2
|
|
3
|
+
== 0.6.6
|
4
|
+
|
5
|
+
* Removing hpricot overrides
|
6
|
+
* Fixed a bug where alt text can be nil. Thanks Yannick!
|
7
|
+
* Unparseable expiration dates in cookies are now treated as session cookies
|
8
|
+
* Caching connections
|
9
|
+
* Requests now default to keep alive
|
10
|
+
* [#9434] Fixed bug where html entities weren't decoded
|
11
|
+
* [#9150] Updated mechanize history to deal with redirects
|
12
|
+
|
3
13
|
== 0.6.5
|
4
14
|
|
5
15
|
* Copying headers to a hash to prevent memory leaks
|
data/Manifest.txt
CHANGED
@@ -16,7 +16,7 @@ lib/mechanize/cookie.rb
|
|
16
16
|
lib/mechanize/errors.rb
|
17
17
|
lib/mechanize/form.rb
|
18
18
|
lib/mechanize/form_elements.rb
|
19
|
-
lib/mechanize/
|
19
|
+
lib/mechanize/history.rb
|
20
20
|
lib/mechanize/inspect.rb
|
21
21
|
lib/mechanize/list.rb
|
22
22
|
lib/mechanize/net-overrides/net/http.rb
|
@@ -83,6 +83,7 @@ test/tc_form_no_inputname.rb
|
|
83
83
|
test/tc_forms.rb
|
84
84
|
test/tc_frames.rb
|
85
85
|
test/tc_gzipping.rb
|
86
|
+
test/tc_history.rb
|
86
87
|
test/tc_html_unscape_forms.rb
|
87
88
|
test/tc_if_modified_since.rb
|
88
89
|
test/tc_links.rb
|
data/lib/mechanize.rb
CHANGED
@@ -28,12 +28,12 @@ require 'uri'
|
|
28
28
|
require 'webrick/httputils'
|
29
29
|
require 'zlib'
|
30
30
|
require 'stringio'
|
31
|
-
require 'mechanize/hpricot'
|
32
31
|
require 'mechanize/cookie'
|
33
32
|
require 'mechanize/errors'
|
34
33
|
require 'mechanize/pluggable_parsers'
|
35
34
|
require 'mechanize/form'
|
36
35
|
require 'mechanize/form_elements'
|
36
|
+
require 'mechanize/history'
|
37
37
|
require 'mechanize/list'
|
38
38
|
require 'mechanize/page'
|
39
39
|
require 'mechanize/page_elements'
|
@@ -62,7 +62,7 @@ class Mechanize
|
|
62
62
|
##
|
63
63
|
# The version of Mechanize you are using.
|
64
64
|
|
65
|
-
VERSION = '0.6.
|
65
|
+
VERSION = '0.6.6'
|
66
66
|
|
67
67
|
##
|
68
68
|
# User Agent aliases
|
@@ -80,7 +80,6 @@ class Mechanize
|
|
80
80
|
|
81
81
|
attr_accessor :cookie_jar
|
82
82
|
attr_accessor :log
|
83
|
-
attr_accessor :max_history
|
84
83
|
attr_accessor :open_timeout, :read_timeout
|
85
84
|
attr_accessor :user_agent
|
86
85
|
attr_accessor :watch_for_set
|
@@ -89,6 +88,7 @@ class Mechanize
|
|
89
88
|
attr_accessor :cert
|
90
89
|
attr_accessor :pass
|
91
90
|
attr_accessor :redirect_ok
|
91
|
+
attr_accessor :keep_alive_time
|
92
92
|
|
93
93
|
attr_reader :history
|
94
94
|
attr_reader :pluggable_parser
|
@@ -97,9 +97,8 @@ class Mechanize
|
|
97
97
|
|
98
98
|
def initialize
|
99
99
|
# attr_accessors
|
100
|
-
@cookie_jar
|
100
|
+
@cookie_jar = CookieJar.new
|
101
101
|
@log = nil
|
102
|
-
@max_history = nil
|
103
102
|
@open_timeout = nil
|
104
103
|
@read_timeout = nil
|
105
104
|
@user_agent = AGENT_ALIASES['Mechanize']
|
@@ -111,7 +110,7 @@ class Mechanize
|
|
111
110
|
@redirect_ok = true # Should we follow redirects?
|
112
111
|
|
113
112
|
# attr_readers
|
114
|
-
@history =
|
113
|
+
@history = WWW::Mechanize::History.new
|
115
114
|
@pluggable_parser = PluggableParser.new
|
116
115
|
|
117
116
|
# Basic Auth variables
|
@@ -124,9 +123,16 @@ class Mechanize
|
|
124
123
|
@proxy_port = nil
|
125
124
|
@proxy_user = nil
|
126
125
|
|
126
|
+
# Connection Cache & Keep alive
|
127
|
+
@connection_cache = {}
|
128
|
+
@keep_alive_time = 300
|
129
|
+
|
127
130
|
yield self if block_given?
|
128
131
|
end
|
129
132
|
|
133
|
+
def max_history=(length); @history.max_size = length; end
|
134
|
+
def max_history; @history.max_size; end
|
135
|
+
|
130
136
|
# Sets the proxy address, port, user, and password
|
131
137
|
def set_proxy(addr, port, user = nil, pass = nil)
|
132
138
|
@proxy_addr, @proxy_port, @proxy_user, @proxy_pass = addr, port, user, pass
|
@@ -241,9 +247,10 @@ class Mechanize
|
|
241
247
|
|
242
248
|
# Returns a visited page for the url passed in, otherwise nil
|
243
249
|
def visited_page(url)
|
244
|
-
|
245
|
-
|
246
|
-
|
250
|
+
if url.respond_to? :href
|
251
|
+
url = url.href
|
252
|
+
end
|
253
|
+
@history.visited_page(to_absolute_uri(url))
|
247
254
|
end
|
248
255
|
|
249
256
|
# Runs given block, then resets the page history as it was before. self is
|
@@ -261,6 +268,8 @@ class Mechanize
|
|
261
268
|
|
262
269
|
protected
|
263
270
|
def set_headers(uri, request, cur_page)
|
271
|
+
request.add_field('Connection', 'keep-alive')
|
272
|
+
request.add_field('Keep-Alive', keep_alive_time.to_s)
|
264
273
|
request.add_field('Accept-Encoding', 'gzip,identity')
|
265
274
|
request.add_field('Accept-Language', 'en-us,en;q0.5')
|
266
275
|
request.add_field('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')
|
@@ -304,8 +313,8 @@ class Mechanize
|
|
304
313
|
url.scan(/%[0-9A-Fa-f]{2}/)
|
305
314
|
).map { |x,y|
|
306
315
|
"#{URI.escape(x)}#{y}"
|
307
|
-
}.join('')
|
308
|
-
)
|
316
|
+
}.join('').gsub(/%23/, '#')
|
317
|
+
)
|
309
318
|
)
|
310
319
|
end
|
311
320
|
|
@@ -357,15 +366,19 @@ class Mechanize
|
|
357
366
|
|
358
367
|
page = nil
|
359
368
|
|
360
|
-
http_obj =
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
369
|
+
http_obj = @connection_cache["#{uri.host}:#{uri.port}"]
|
370
|
+
if http_obj.nil? || ! http_obj.started?
|
371
|
+
http_obj = @connection_cache["#{uri.host}:#{uri.port}"] =
|
372
|
+
Net::HTTP.new( uri.host,
|
373
|
+
uri.port,
|
374
|
+
@proxy_addr,
|
375
|
+
@proxy_port,
|
376
|
+
@proxy_user,
|
377
|
+
@proxy_pass
|
378
|
+
)
|
379
|
+
end
|
367
380
|
|
368
|
-
if uri.scheme == 'https'
|
381
|
+
if uri.scheme == 'https' && ! http_obj.started?
|
369
382
|
http_obj.use_ssl = true
|
370
383
|
http_obj.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
371
384
|
if @ca_file
|
@@ -387,86 +400,86 @@ class Mechanize
|
|
387
400
|
end
|
388
401
|
end
|
389
402
|
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
http.read_timeout = @read_timeout if @read_timeout
|
394
|
-
|
395
|
-
# Send the request
|
396
|
-
http.request(request, *request_data) {|response|
|
397
|
-
if log
|
398
|
-
response.each_header {|k,v|
|
399
|
-
log.debug("response-header: #{ k } => #{ v }")
|
400
|
-
}
|
401
|
-
end
|
403
|
+
# Specify timeouts if given
|
404
|
+
http_obj.open_timeout = @open_timeout if @open_timeout
|
405
|
+
http_obj.read_timeout = @read_timeout if @read_timeout
|
402
406
|
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
}
|
408
|
-
|
407
|
+
# Send the request
|
408
|
+
http_obj.request(request, *request_data) {|response|
|
409
|
+
if log
|
410
|
+
response.each_header {|k,v|
|
411
|
+
log.debug("response-header: #{ k } => #{ v }")
|
412
|
+
}
|
413
|
+
end
|
409
414
|
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
body.write(part)
|
415
|
-
log.debug("Read #{total} bytes") if log
|
415
|
+
(response.get_fields('Set-Cookie')||[]).each do |cookie|
|
416
|
+
Cookie::parse(uri, cookie, log) { |c|
|
417
|
+
log.debug("saved cookie: #{c}") if log
|
418
|
+
@cookie_jar.add(uri, c)
|
416
419
|
}
|
417
|
-
|
420
|
+
end
|
418
421
|
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
422
|
+
body = StringIO.new
|
423
|
+
total = 0
|
424
|
+
response.read_body { |part|
|
425
|
+
total += part.length
|
426
|
+
body.write(part)
|
427
|
+
log.debug("Read #{total} bytes") if log
|
428
|
+
}
|
429
|
+
body.rewind
|
424
430
|
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
431
|
+
content_type = nil
|
432
|
+
unless response['Content-Type'].nil?
|
433
|
+
data = response['Content-Type'].match(/^([^;]*)/)
|
434
|
+
content_type = data[1].downcase unless data.nil?
|
435
|
+
end
|
436
|
+
|
437
|
+
response_body =
|
438
|
+
if encoding = response['Content-Encoding']
|
439
|
+
case encoding.downcase
|
440
|
+
when 'gzip'
|
441
|
+
log.debug('gunzip body') if log
|
442
|
+
Zlib::GzipReader.new(body).read
|
434
443
|
else
|
435
|
-
|
444
|
+
raise 'Unsupported content encoding'
|
436
445
|
end
|
446
|
+
else
|
447
|
+
body.read
|
448
|
+
end
|
437
449
|
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
+
# Find our pluggable parser
|
451
|
+
page = @pluggable_parser.parser(content_type).new(
|
452
|
+
uri,
|
453
|
+
response,
|
454
|
+
response_body,
|
455
|
+
response.code
|
456
|
+
) { |parser|
|
457
|
+
parser.mech = self if parser.respond_to? :mech=
|
458
|
+
if parser.respond_to?(:watch_for_set=) && @watch_for_set
|
459
|
+
parser.watch_for_set = @watch_for_set
|
460
|
+
end
|
461
|
+
}
|
450
462
|
|
451
|
-
|
463
|
+
log.info("status: #{ page.code }") if log
|
452
464
|
|
453
|
-
|
465
|
+
res_klass = Net::HTTPResponse::CODE_TO_OBJ[page.code.to_s]
|
454
466
|
|
455
|
-
|
467
|
+
return page if res_klass <= Net::HTTPSuccess
|
456
468
|
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
469
|
+
if res_klass == Net::HTTPNotModified
|
470
|
+
log.debug("Got cached page") if log
|
471
|
+
return visited_page(uri)
|
472
|
+
elsif res_klass <= Net::HTTPRedirection
|
473
|
+
return page unless follow_redirect?
|
474
|
+
log.info("follow redirect to: #{ response['Location'] }") if log
|
475
|
+
from_uri = page.uri
|
476
|
+
abs_uri = to_absolute_uri(response['Location'].to_s, page)
|
477
|
+
page = fetch_page(abs_uri, fetch_request(abs_uri), page)
|
478
|
+
@history.push(page, from_uri)
|
479
|
+
return page
|
480
|
+
end
|
467
481
|
|
468
|
-
|
469
|
-
}
|
482
|
+
raise ResponseCodeError.new(page), "Unhandled response", caller
|
470
483
|
}
|
471
484
|
end
|
472
485
|
|
@@ -484,12 +497,6 @@ class Mechanize
|
|
484
497
|
|
485
498
|
def add_to_history(page)
|
486
499
|
@history.push(page)
|
487
|
-
if @max_history and @history.length > @max_history
|
488
|
-
while @history.length > @max_history
|
489
|
-
@history[0] = nil
|
490
|
-
@history.shift
|
491
|
-
end
|
492
|
-
end
|
493
500
|
end
|
494
501
|
|
495
502
|
# :stopdoc:
|
data/lib/mechanize/cookie.rb
CHANGED
@@ -23,10 +23,12 @@ module WWW
|
|
23
23
|
when "domain" then cookie.domain = value.sub(/^\./, '')
|
24
24
|
when "path" then cookie.path = value
|
25
25
|
when 'expires'
|
26
|
-
|
27
|
-
Time::parse(value)
|
26
|
+
begin
|
27
|
+
cookie.expires = Time::parse(value)
|
28
28
|
rescue
|
29
|
-
|
29
|
+
if log
|
30
|
+
log.warn("Couldn't parse expires: #{value}")
|
31
|
+
end
|
30
32
|
end
|
31
33
|
when "max-age" then
|
32
34
|
begin
|
data/lib/mechanize/form.rb
CHANGED
@@ -148,7 +148,7 @@ module WWW
|
|
148
148
|
(@elements_node/'textarea').each do |node|
|
149
149
|
next if node.attributes.nil?
|
150
150
|
next if node.attributes['name'].nil?
|
151
|
-
@fields << Field.new(node.attributes['name'], node.
|
151
|
+
@fields << Field.new(node.attributes['name'], node.inner_text)
|
152
152
|
end
|
153
153
|
|
154
154
|
# Find all select tags
|
@@ -218,7 +218,7 @@ module WWW
|
|
218
218
|
|
219
219
|
def initialize(node, select_list)
|
220
220
|
node.attributes ||= {}
|
221
|
-
@text = node.
|
221
|
+
@text = node.inner_text
|
222
222
|
@value = Util.html_unescape(node.attributes['value'])
|
223
223
|
@selected = node.attributes.has_key?('selected') ? true : false
|
224
224
|
@select_list = select_list # The select list this option belongs to
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module WWW
|
2
|
+
class Mechanize
|
3
|
+
##
|
4
|
+
# This class manages history for your mechanize object.
|
5
|
+
class History < Array
|
6
|
+
attr_accessor :max_size
|
7
|
+
|
8
|
+
def initialize(max_size = nil)
|
9
|
+
@max_size = max_size
|
10
|
+
@history_index = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def push(page, uri = nil)
|
14
|
+
super(page)
|
15
|
+
@history_index[(uri ? uri : page.uri).to_s] = page
|
16
|
+
if @max_size && self.length > @max_size
|
17
|
+
while self.length > @max_size
|
18
|
+
self.shift
|
19
|
+
end
|
20
|
+
end
|
21
|
+
self
|
22
|
+
end
|
23
|
+
alias :<< :push
|
24
|
+
|
25
|
+
def visited?(url)
|
26
|
+
! visited_page(url).nil?
|
27
|
+
end
|
28
|
+
|
29
|
+
def visited_page(url)
|
30
|
+
@history_index[(url.respond_to?(:uri) ? url.uri : url).to_s]
|
31
|
+
end
|
32
|
+
|
33
|
+
def clear
|
34
|
+
@history_index.clear
|
35
|
+
super
|
36
|
+
end
|
37
|
+
|
38
|
+
def shift
|
39
|
+
return nil if length == 0
|
40
|
+
page = self[0]
|
41
|
+
self[0] = nil
|
42
|
+
super
|
43
|
+
remove_from_index(page)
|
44
|
+
page
|
45
|
+
end
|
46
|
+
|
47
|
+
def pop
|
48
|
+
return nil if length == 0
|
49
|
+
page = super
|
50
|
+
remove_from_index(page)
|
51
|
+
page
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
def remove_from_index(page)
|
56
|
+
@history_index.each do |k,v|
|
57
|
+
@history_index.delete(k) if v == page
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -21,7 +21,7 @@ module WWW
|
|
21
21
|
node.attributes ||= {}
|
22
22
|
@node = node
|
23
23
|
@href = node.attributes['href']
|
24
|
-
@text = node.
|
24
|
+
@text = node.inner_text
|
25
25
|
@page = page
|
26
26
|
@mech = mech
|
27
27
|
@attributes = node.attributes
|
@@ -31,7 +31,11 @@ module WWW
|
|
31
31
|
@text = ''
|
32
32
|
(node/'img').each do |e|
|
33
33
|
e.attributes ||= {}
|
34
|
-
@text << (
|
34
|
+
@text << (
|
35
|
+
(e.attributes.has_key?('alt') && e.attributes['alt']) ?
|
36
|
+
e.attributes['alt'] :
|
37
|
+
''
|
38
|
+
)
|
35
39
|
end
|
36
40
|
end
|
37
41
|
|
data/lib/mechanize/rexml.rb
CHANGED
@@ -90,11 +90,11 @@ end
|
|
90
90
|
#
|
91
91
|
# collect_text_recursively.flatten.join("")
|
92
92
|
|
93
|
-
def
|
93
|
+
def inner_text
|
94
94
|
collect_text_recursively.flatten.join("")
|
95
95
|
end
|
96
96
|
|
97
|
-
alias :text :
|
97
|
+
alias :text :inner_text
|
98
98
|
|
99
99
|
end
|
100
100
|
|
@@ -163,7 +163,7 @@ def extract_from_table(root_node, headers, header_tags = %w(td th))
|
|
163
163
|
|
164
164
|
header_nodes = headers.collect { |header|
|
165
165
|
root_node.find_first_recursive {|node|
|
166
|
-
header_tags.include?(node.name.downcase) and header === node.
|
166
|
+
header_tags.include?(node.name.downcase) and header === node.inner_text
|
167
167
|
}
|
168
168
|
}
|
169
169
|
|
data/test/htdocs/alt_text.html
CHANGED
data/test/tc_cookie_class.rb
CHANGED
@@ -90,7 +90,7 @@ class CookieClassTest < Test::Unit::TestCase
|
|
90
90
|
dates.each do |date|
|
91
91
|
cookie = "PREF=1; expires=#{date}"
|
92
92
|
WWW::Mechanize::Cookie.parse(url, cookie) { |cookie|
|
93
|
-
assert_equal(true, cookie.expires
|
93
|
+
assert_equal(true, cookie.expires.nil?)
|
94
94
|
}
|
95
95
|
end
|
96
96
|
end
|
data/test/tc_history.rb
ADDED
@@ -0,0 +1,125 @@
|
|
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 TestHistory < Test::Unit::TestCase
|
9
|
+
include TestMethods
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@agent = WWW::Mechanize.new
|
13
|
+
@history = WWW::Mechanize::History.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_push
|
17
|
+
assert_equal(0, @history.length)
|
18
|
+
|
19
|
+
page = @agent.get("http://localhost/tc_bad_links.html")
|
20
|
+
x = @history.push(page)
|
21
|
+
assert_equal(x, @history)
|
22
|
+
assert_equal(1, @history.length)
|
23
|
+
assert(@history.visited?(page))
|
24
|
+
assert(@history.visited?(page.uri))
|
25
|
+
assert(@history.visited?(page.uri.to_s))
|
26
|
+
assert_equal(page, @history.visited_page(page))
|
27
|
+
assert_equal(page, @history.visited_page(page.uri))
|
28
|
+
assert_equal(page, @history.visited_page(page.uri.to_s))
|
29
|
+
|
30
|
+
@history.push(@agent.get("/tc_bad_links.html"))
|
31
|
+
assert_equal(2, @history.length)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_shift
|
35
|
+
assert_equal(0, @history.length)
|
36
|
+
page = @agent.get("http://localhost/tc_bad_links.html")
|
37
|
+
@history.push(page)
|
38
|
+
assert_equal(1, @history.length)
|
39
|
+
|
40
|
+
@history.push(@agent.get("/tc_bad_links.html"))
|
41
|
+
assert_equal(2, @history.length)
|
42
|
+
|
43
|
+
@history.push(@agent.get("/index.html"))
|
44
|
+
assert_equal(3, @history.length)
|
45
|
+
|
46
|
+
page2 = @history.shift
|
47
|
+
assert_equal(page, page2)
|
48
|
+
assert_equal(2, @history.length)
|
49
|
+
|
50
|
+
@history.shift
|
51
|
+
assert_equal(1, @history.length)
|
52
|
+
assert_equal(false, @history.visited?(page))
|
53
|
+
|
54
|
+
@history.shift
|
55
|
+
assert_equal(0, @history.length)
|
56
|
+
|
57
|
+
assert_nil(@history.shift)
|
58
|
+
assert_equal(0, @history.length)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_pop
|
62
|
+
assert_equal(0, @history.length)
|
63
|
+
page = @agent.get("http://localhost/tc_bad_links.html")
|
64
|
+
@history.push(page)
|
65
|
+
assert_equal(1, @history.length)
|
66
|
+
|
67
|
+
page2 = @agent.get("/index.html")
|
68
|
+
@history.push(page2)
|
69
|
+
assert_equal(2, @history.length)
|
70
|
+
assert_equal(page2, @history.pop)
|
71
|
+
assert_equal(1, @history.length)
|
72
|
+
assert_equal(true, @history.visited?(page))
|
73
|
+
assert_equal(false, @history.visited?(page2))
|
74
|
+
assert_equal(page, @history.pop)
|
75
|
+
assert_equal(0, @history.length)
|
76
|
+
assert_equal(false, @history.visited?(page))
|
77
|
+
assert_equal(false, @history.visited?(page2))
|
78
|
+
assert_nil(@history.pop)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_max_size
|
82
|
+
@history = WWW::Mechanize::History.new(10)
|
83
|
+
1.upto(20) do |i|
|
84
|
+
page = @agent.get('http://localhost/index.html')
|
85
|
+
@history.push page
|
86
|
+
assert_equal(true, @history.visited?(page))
|
87
|
+
if i < 10
|
88
|
+
assert_equal(i, @history.length)
|
89
|
+
else
|
90
|
+
assert_equal(10, @history.length)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
@history.clear
|
95
|
+
@history.max_size = 5
|
96
|
+
1.upto(20) do |i|
|
97
|
+
page = @agent.get('http://localhost/index.html')
|
98
|
+
@history.push page
|
99
|
+
assert_equal(true, @history.visited?(page))
|
100
|
+
if i < 5
|
101
|
+
assert_equal(i, @history.length)
|
102
|
+
else
|
103
|
+
assert_equal(5, @history.length)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
@history.max_size = 0
|
108
|
+
1.upto(20) do |i|
|
109
|
+
page = @agent.get('http://localhost/index.html')
|
110
|
+
@history.push page
|
111
|
+
assert_equal(false, @history.visited?(page))
|
112
|
+
assert_equal(0, @history.length)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_clear
|
117
|
+
page = nil
|
118
|
+
20.times { @history.push(page = @agent.get('http://localhost/index.html')) }
|
119
|
+
assert_equal(20, @history.length)
|
120
|
+
assert_equal(true, @history.visited?(page))
|
121
|
+
@history.clear
|
122
|
+
assert_equal(0, @history.length)
|
123
|
+
assert_equal(false, @history.visited?(page))
|
124
|
+
end
|
125
|
+
end
|
data/test/tc_links.rb
CHANGED
@@ -26,7 +26,7 @@ class LinksMechTest < Test::Unit::TestCase
|
|
26
26
|
|
27
27
|
def test_alt_text
|
28
28
|
page = @agent.get("http://localhost:#{PORT}/alt_text.html")
|
29
|
-
assert_equal(
|
29
|
+
assert_equal(5, page.links.length)
|
30
30
|
assert_equal(1, page.meta.length)
|
31
31
|
|
32
32
|
assert_equal('', page.meta.first.text)
|
@@ -34,6 +34,7 @@ class LinksMechTest < Test::Unit::TestCase
|
|
34
34
|
assert_equal('', page.links.href('no_alt_text.html').first.text)
|
35
35
|
assert_equal('no image', page.links.href('no_image.html').first.text)
|
36
36
|
assert_equal('', page.links.href('no_text.html').first.text)
|
37
|
+
assert_equal('', page.links.href('nil_alt_text.html').first.text)
|
37
38
|
end
|
38
39
|
|
39
40
|
def test_click_link
|
data/test/tc_mech.rb
CHANGED
@@ -14,7 +14,12 @@ class TestMechMethods < Test::Unit::TestCase
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_weird_url
|
17
|
-
|
17
|
+
assert_nothing_raised {
|
18
|
+
@agent.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
|
19
|
+
}
|
20
|
+
assert_nothing_raised {
|
21
|
+
@agent.get('http://localhost/?a=b&b=c&c=d')
|
22
|
+
}
|
18
23
|
end
|
19
24
|
|
20
25
|
def test_history
|
@@ -48,6 +53,14 @@ class TestMechMethods < Test::Unit::TestCase
|
|
48
53
|
@agent.visited?("http://localhost/content_type_test?ct=text/html"))
|
49
54
|
end
|
50
55
|
|
56
|
+
def test_visited_after_redirect
|
57
|
+
@agent.get("http://localhost/response_code?code=302")
|
58
|
+
assert_equal("http://localhost/index.html",
|
59
|
+
@agent.current_page.uri.to_s)
|
60
|
+
assert_equal(true,
|
61
|
+
@agent.visited?('http://localhost/response_code?code=302'))
|
62
|
+
end
|
63
|
+
|
51
64
|
def test_max_history
|
52
65
|
@agent.max_history = 10
|
53
66
|
0.upto(10) do |i|
|
data/test/test_all.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: mechanize
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.6.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.6.6
|
7
|
+
date: 2007-03-24 00:00:00 -07:00
|
8
8
|
summary: Mechanize provides automated web-browsing
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -51,7 +51,7 @@ files:
|
|
51
51
|
- lib/mechanize/errors.rb
|
52
52
|
- lib/mechanize/form.rb
|
53
53
|
- lib/mechanize/form_elements.rb
|
54
|
-
- lib/mechanize/
|
54
|
+
- lib/mechanize/history.rb
|
55
55
|
- lib/mechanize/inspect.rb
|
56
56
|
- lib/mechanize/list.rb
|
57
57
|
- lib/mechanize/net-overrides/net/http.rb
|
@@ -118,6 +118,7 @@ files:
|
|
118
118
|
- test/tc_forms.rb
|
119
119
|
- test/tc_frames.rb
|
120
120
|
- test/tc_gzipping.rb
|
121
|
+
- test/tc_history.rb
|
121
122
|
- test/tc_html_unscape_forms.rb
|
122
123
|
- test/tc_if_modified_since.rb
|
123
124
|
- test/tc_links.rb
|