-
Notifications
You must be signed in to change notification settings - Fork 56
/
mechanize_driver_spec.rb
215 lines (172 loc) · 6.37 KB
/
mechanize_driver_spec.rb
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
require 'spec_helper'
describe Capybara::Mechanize::Driver, 'local' do
let(:driver) { Capybara::Mechanize::Driver.new(ExtendedTestApp) }
describe ':headers option' do
it 'should always set headers' do
driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})
driver.visit('/get_header')
driver.html.should include('foobar')
end
it 'should keep headers on link clicks' do
driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})
driver.visit('/header_links')
driver.find('.//a').first.click
driver.html.should include('foobar')
end
it 'should keep headers on form submit' do
driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})
driver.visit('/header_links')
driver.find('.//input').first.click
driver.html.should include('foobar')
end
it 'should keep headers on redirects' do
driver = Capybara::RackTest::Driver.new(TestApp, :headers => {'HTTP_FOO' => 'foobar'})
driver.visit('/get_header_via_redirect')
driver.html.should include('foobar')
end
end
describe ':follow_redirects option' do
it "defaults to following redirects" do
driver = Capybara::RackTest::Driver.new(TestApp)
driver.visit('/redirect')
driver.response.header['Location'].should be_nil
driver.browser.current_url.should match %r{/landed$}
end
it "is possible to not follow redirects" do
driver = Capybara::RackTest::Driver.new(TestApp, :follow_redirects => false)
driver.visit('/redirect')
driver.response.header['Location'].should match %r{/redirect_again$}
driver.browser.current_url.should match %r{/redirect$}
end
end
describe ':redirect_limit option' do
context "with default redirect limit" do
let(:driver) { Capybara::RackTest::Driver.new(TestApp) }
it "should follow 5 redirects" do
driver.visit("/redirect/5/times")
driver.html.should include('redirection complete')
end
it "should not follow more than 6 redirects" do
expect do
driver.visit("/redirect/6/times")
end.to raise_error(Capybara::InfiniteRedirectError)
end
end
context "with 21 redirect limit" do
let(:driver) { Capybara::RackTest::Driver.new(TestApp, :redirect_limit => 21) }
it "should follow 21 redirects" do
driver.visit("/redirect/21/times")
driver.html.should include('redirection complete')
end
it "should not follow more than 21 redirects" do
expect do
driver.visit("/redirect/22/times")
end.to raise_error(Capybara::InfiniteRedirectError)
end
end
end
it "should default to local mode for relative paths" do
driver.should_not be_remote('/')
end
it "should default to local mode for the default host" do
driver.should_not be_remote('http://www.example.com')
end
context "with an app_host" do
before do
Capybara.app_host = 'http://www.remote.com'
end
after do
Capybara.app_host = nil
end
it "should treat urls as remote" do
driver.should be_remote('http://www.remote.com')
end
end
context "with a default url, no app host" do
before do
Capybara.default_host = 'www.local.com'
end
it "should allow local hosts to be set" do
Capybara::Mechanize.local_hosts = ['subdomain.local.com']
driver.should_not be_remote('http://subdomain.local.com')
end
it "should treat urls with the same host names as local" do
driver.should_not be_remote('http://www.local.com')
end
it "should treat other urls as remote" do
driver.should be_remote('http://www.remote.com')
end
it "should treat relative paths as remote if the previous request was remote" do
driver.visit(REMOTE_TEST_URL)
driver.should be_remote('/some_relative_link')
end
it "should treat relative paths as local if the previous request was local" do
driver.visit('http://www.local.com')
driver.should_not be_remote('/some_relative_link')
end
it "should receive the right host" do
driver.visit('http://www.local.com/host')
should_be_a_local_get
end
it "should consider relative paths to be local when the previous request was local" do
driver.visit('http://www.local.com/host')
driver.visit('/host')
should_be_a_local_get
driver.should_not be_remote('/first_local')
end
it "should consider relative paths to be remote when the previous request was remote" do
driver.visit("#{REMOTE_TEST_URL}/host")
driver.get('/host')
should_be_a_remote_get
driver.should be_remote('/second_remote')
end
it "should always switch to the right context" do
driver.visit('http://www.local.com/host')
driver.get('/host')
driver.get("#{REMOTE_TEST_URL}/host")
driver.get('/host')
driver.get('http://www.local.com/host')
should_be_a_local_get
driver.should_not be_remote('/second_local')
end
it "should follow redirects from local to remote" do
driver.visit("http://www.local.com/redirect_to/#{REMOTE_TEST_URL}/host")
should_be_a_remote_get
end
it "should follow redirects from remote to local" do
driver.visit("#{REMOTE_TEST_URL}/redirect_to/http://www.local.com/host")
should_be_a_local_get
end
after do
Capybara.default_host = nil
end
it "should raise a useful error for sites that return a 404, because it is probably a misconfiguration" do
expect {
driver.visit("http://iamreallysurethatthisdoesntexist.com/canttouchthis")
}.to raise_error(%r{Received the following error for a GET request to http://iamreallysurethatthisdoesntexist.com/canttouchthis:})
end
end
it "should include the right host when remote" do
driver.visit("#{REMOTE_TEST_URL}/host")
should_be_a_remote_get
end
describe '#reset!' do
before do
Capybara.default_host = 'http://www.local.com'
end
it 'should reset remote host' do
driver.visit("#{REMOTE_TEST_URL}/host")
should_be_a_remote_get
driver.reset!
driver.visit("/host")
should_be_a_local_get
end
end
def should_be_a_remote_get
driver.current_url.should include(REMOTE_TEST_URL)
end
def should_be_a_local_get
driver.current_url.should include("www.local.com")
end
#
end