1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
require "helper"
class TestMechMethods < Test::Unit::TestCase
def setup
@agent = Mechanize.new
end
def test_get_with_tilde
page = @agent.get('http://localhost/?foo=~2')
assert_equal('http://localhost/?foo=~2', page.uri.to_s)
end
def test_parser_can_be_set
@agent.html_parser = {}
assert_raises(NoMethodError) {
@agent.get('http://localhost/?foo=~2').links
}
end
def test_submit_takes_arbirary_headers
page = @agent.get('http://localhost:2000/form_no_action.html')
assert form = page.forms.first
form.action = '/http_headers'
page = @agent.submit(form, nil, { 'foo' => 'bar' })
headers = Hash[*(
page.body.split("\n").map { |x| x.split('|') }.flatten
)]
assert_equal 'bar', headers['foo']
end
def test_get_with_params
page = @agent.get('http://localhost/', { :q => 'hello' })
assert_equal('http://localhost/?q=hello', page.uri.to_s)
end
def test_get_with_upper_http
page = @agent.get('HTTP://localhost/', { :q => 'hello' })
assert_equal('HTTP://localhost/?q=hello', page.uri.to_s)
end
def test_get_no_referer
requests = []
@agent.pre_connect_hooks << lambda { |params|
requests << params[:request]
}
@agent.get('http://localhost/')
@agent.get('http://localhost/')
assert_equal(2, requests.length)
requests.each do |request|
assert_nil request['referer']
end
end
def test_with_anchor
page = @agent.get('http://localhost/?foo=bar"')
assert_equal('http://localhost/?foo=bar%22', page.uri.to_s)
end
def test_post_connect_hook_gets_called
response = nil
@agent.post_connect_hooks << lambda { |params|
response = params[:response]
}
@agent.get('http://localhost/')
assert(response)
end
def test_get_with_referer
request = nil
@agent.pre_connect_hooks << lambda { |params|
request = params[:request]
}
@agent.get('http://localhost/', URI.parse('http://google.com/'))
assert_equal 'http://google.com/', request['Referer']
@agent.get('http://localhost/', [], 'http://tenderlovemaking.com/')
assert_equal 'http://tenderlovemaking.com/', request['Referer']
end
def test_get_with_file_referer
assert_nothing_raised do
@agent.get('http://localhost', [], Mechanize::File.new(URI.parse('http://tenderlovemaking.com/crossdomain.xml')))
end
end
def test_weird_url
assert_nothing_raised {
@agent.get('http://localhost/?action=bing&bang=boom=1|a=|b=|c=')
}
assert_nothing_raised {
@agent.get('http://localhost/?a=b&b=c&c=d')
}
assert_nothing_raised {
@agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
}
end
unless RUBY_VERSION >= '1.9.0'
def test_kcode_url
$KCODE = 'u'
page = @agent.get("http://localhost/?a=#{[0xd6].pack('U')}")
assert_not_nil(page)
assert_equal('http://localhost/?a=%D6', page.uri.to_s)
$KCODE = 'NONE'
end
end
def test_history
0.upto(25) do |i|
assert_equal(i, @agent.history.size)
page = @agent.get("http://localhost/")
end
page = @agent.get("http://localhost/form_test.html")
assert_equal("http://localhost/form_test.html",
@agent.history.last.uri.to_s)
assert_equal("http://localhost/",
@agent.history[-2].uri.to_s)
assert_equal("http://localhost/",
@agent.history[-2].uri.to_s)
assert_equal(true, @agent.visited?("http://localhost/"))
assert_equal(true, @agent.visited?("/form_test.html"))
assert_equal(false, @agent.visited?("http://google.com/"))
assert_equal(true, @agent.visited?(page.links.first))
end
def test_visited
@agent.get("http://localhost/content_type_test?ct=application/pdf")
assert_equal(true,
@agent.visited?("http://localhost/content_type_test?ct=application/pdf"))
assert_equal(false,
@agent.visited?("http://localhost/content_type_test"))
assert_equal(false,
@agent.visited?("http://localhost/content_type_test?ct=text/html"))
end
def test_visited_after_redirect
@agent.get("http://localhost/response_code?code=302")
assert_equal("http://localhost/index.html",
@agent.current_page.uri.to_s)
assert_equal(true,
@agent.visited?('http://localhost/response_code?code=302'))
end
def test_max_history
@agent.max_history = 10
0.upto(10) do |i|
assert_equal(i, @agent.history.size)
page = @agent.get("http://localhost/")
end
0.upto(10) do |i|
assert_equal(10, @agent.history.size)
page = @agent.get("http://localhost/")
end
end
def test_max_history_order
@agent.max_history = 2
assert_equal(0, @agent.history.length)
@agent.get('http://localhost/form_test.html')
assert_equal(1, @agent.history.length)
@agent.get('http://localhost/empty_form.html')
assert_equal(2, @agent.history.length)
@agent.get('http://localhost/tc_checkboxes.html')
assert_equal(2, @agent.history.length)
assert_equal('http://localhost/empty_form.html', @agent.history[0].uri.to_s)
assert_equal('http://localhost/tc_checkboxes.html',
@agent.history[1].uri.to_s)
end
def test_back_button
0.upto(5) do |i|
assert_equal(i, @agent.history.size)
page = @agent.get("http://localhost/")
end
page = @agent.get("http://localhost/form_test.html")
assert_equal("http://localhost/form_test.html",
@agent.history.last.uri.to_s)
assert_equal("http://localhost/",
@agent.history[-2].uri.to_s)
assert_equal(7, @agent.history.size)
@agent.back
assert_equal(6, @agent.history.size)
assert_equal("http://localhost/",
@agent.history.last.uri.to_s)
end
def test_google
page = @agent.get("http://localhost/google.html")
search = page.forms.find { |f| f.name == "f" }
assert_not_nil(search)
assert_not_nil(search.field_with(:name => 'q'))
assert_not_nil(search.field_with(:name => 'hl'))
assert_not_nil(search.fields.find { |f| f.name == 'ie' })
end
def test_click
@agent.user_agent_alias = 'Mac Safari'
page = @agent.get("http://localhost/frame_test.html")
link = page.link_with(:text => "Form Test")
assert_not_nil(link)
page = @agent.click(link)
assert_equal("http://localhost/form_test.html",
@agent.history.last.uri.to_s)
end
def test_click_hpricot
page = @agent.get("http://localhost/frame_test.html")
link = (page/"//a[@class='bar']").first
assert_not_nil(link)
page = @agent.click(link)
assert_equal("http://localhost/form_test.html",
@agent.history.last.uri.to_s)
end
def test_click_hpricot_frame
page = @agent.get("http://localhost/frame_test.html")
link = (page/"//frame[@name='frame2']").first
assert_not_nil(link)
page = @agent.click(link)
assert_equal("http://localhost/form_test.html",
@agent.history.last.uri.to_s)
end
def test_new_find
page = @agent.get("http://localhost/frame_test.html")
assert_equal(3, page.frames.size)
find_orig = page.frames.find_all { |f| f.name == 'frame1' }
find1 = page.frames_with(:name => 'frame1')
find_orig.zip(find1).each { |a,b|
assert_equal(a, b)
}
end
def test_get_file
page = @agent.get("http://localhost/frame_test.html")
content_length = page.header['Content-Length']
page_as_string = @agent.get_file("http://localhost/frame_test.html")
assert_equal(content_length.to_i, page_as_string.length.to_i)
end
def test_transact
page = @agent.get("http://localhost/frame_test.html")
assert_equal(1, @agent.history.length)
@agent.transact { |a|
5.times {
@agent.get("http://localhost/frame_test.html")
}
assert_equal(6, @agent.history.length)
}
assert_equal(1, @agent.history.length)
end
end
|