File: test_follow_meta.rb

package info (click to toggle)
libwww-mechanize-ruby 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 956 kB
  • ctags: 883
  • sloc: ruby: 6,621; makefile: 4
file content (119 lines) | stat: -rw-r--r-- 3,749 bytes parent folder | download
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
require 'helper'

class FollowMetaTest < Test::Unit::TestCase
  def setup
    @agent = Mechanize.new
  end

  def test_dont_follow_meta_in_body
    @agent.follow_meta_refresh = true
    requests = []
    @agent.pre_connect_hooks << lambda { |params|
      requests << params[:request]
    }

    page = @agent.get('http://localhost/tc_meta_in_body.html')
    assert_equal 1, requests.length
  end

  def test_dont_follow_meta_by_default
    page = @agent.get('http://localhost/tc_follow_meta.html')
    assert_equal('http://localhost/tc_follow_meta.html', page.uri.to_s)
    assert_equal(1, page.meta.length)
  end

  def test_meta_refresh_does_not_send_referer
    @agent.follow_meta_refresh = true
    requests = []
    @agent.pre_connect_hooks << lambda { |params|
      requests << params[:request]
    }

    page = @agent.get('http://localhost/tc_follow_meta.html')
    assert_nil requests[1]['referer']
  end

  def test_follow_meta_if_set
    @agent.follow_meta_refresh = true

    page = @agent.get('http://localhost/tc_follow_meta.html')

    assert_equal(2, @agent.history.length)
    assert_equal('http://localhost/tc_follow_meta.html',
                 @agent.history[0].uri.to_s)
    assert_equal('http://localhost/index.html', page.uri.to_s)
    assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
  end

  def test_follow_meta_with_empty_url
    @agent.follow_meta_refresh = true

    page = @agent.get('http://localhost/refresh_with_empty_url')

    assert_equal(3, @agent.history.length)
    assert_equal('http://localhost/refresh_with_empty_url',
                 @agent.history[0].uri.to_s)
    assert_equal('http://localhost/refresh_with_empty_url',
                 @agent.history[1].uri.to_s)
    assert_equal('http://localhost/index.html', page.uri.to_s)
    assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
  end

  def test_follow_meta_without_url
    @agent.follow_meta_refresh = true

    page = @agent.get('http://localhost/refresh_without_url')

    assert_equal(3, @agent.history.length)
    assert_equal('http://localhost/refresh_without_url',
                 @agent.history[0].uri.to_s)
    assert_equal('http://localhost/refresh_without_url',
                 @agent.history[1].uri.to_s)
    assert_equal('http://localhost/index.html', page.uri.to_s)
    assert_equal('http://localhost/index.html', @agent.history.last.uri.to_s)
  end

  def test_always_follow_302
    @agent.follow_meta_refresh = false
    page = @agent.get('http://localhost/response_code?code=302&ct=test/xml')
    assert_equal('http://localhost/index.html', page.uri.to_s)
    assert_equal(2, @agent.history.length)
  end

  def test_infinite_refresh_throws_exception
    @agent.follow_meta_refresh = true
    assert_raises(Mechanize::RedirectLimitReachedError) {
      begin
        @agent.get('http://localhost/infinite_refresh')
      rescue Mechanize::RedirectLimitReachedError => ex
        raise ex
      end
    }
  end

  def test_dont_honor_http_refresh_by_default
    page = @agent.get('http://localhost/http_refresh?refresh_time=0')
    assert_equal('http://localhost/http_refresh?refresh_time=0', page.uri.to_s)
  end

  def test_honor_http_refresh_if_set
    @agent.follow_meta_refresh = true
    page = @agent.get('http://localhost/http_refresh?refresh_time=0')
    assert_equal('http://localhost/index.html', page.uri.to_s)
    assert_equal(2, @agent.history.length)
  end

  def test_honor_http_refresh_delay_if_set
    @agent.follow_meta_refresh = true
    class << @agent
      attr_accessor :slept
      def sleep *args
        @slept = args
      end
    end

    page = @agent.get('http://localhost/http_refresh?refresh_time=1')
    assert_equal [1], @agent.slept
  end

end