bbc-capybara-mechanize 0.4.2 → 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.mdown +2 -10
- data/lib/capybara/mechanize/browser.rb +201 -174
- data/lib/capybara/mechanize/driver.rb +1 -1
- data/lib/capybara/mechanize/version.rb +2 -2
- data/lib/capybara/spec/extended_test_app.rb +24 -20
- data/spec/driver/remote_mechanize_driver_spec.rb +25 -37
- data/spec/relative_urls_spec.rb +68 -0
- data/spec/session/mechanize_spec.rb +0 -39
- data/spec/session/remote_mechanize_spec.rb +21 -0
- data/spec/spec_helper.rb +25 -15
- metadata +10 -11
data/README.mdown
CHANGED
@@ -39,21 +39,13 @@ Note that I haven't tested this case for my self yet. The Capybara tests pass fo
|
|
39
39
|
|
40
40
|
## Running tests
|
41
41
|
|
42
|
-
Until this library is merged with capybara there needs to be local app and you need to add the following to your host file:
|
43
|
-
|
44
|
-
127.0.0.1 capybara-testapp.heroku.com
|
45
|
-
|
46
42
|
Run bundler
|
47
43
|
|
48
44
|
bundle install
|
49
45
|
|
50
|
-
Run the app with the following line:
|
51
|
-
|
52
|
-
bundle exec ruby -rrubygems lib/capybara/spec/extended_test_app.rb
|
53
|
-
|
54
46
|
Then you are ready to run the test like so
|
55
47
|
|
56
|
-
rake spec
|
48
|
+
bundle exec rake spec
|
57
49
|
|
58
50
|
Todo
|
59
51
|
----
|
@@ -73,4 +65,4 @@ Note on Patches/Pull Requests
|
|
73
65
|
|
74
66
|
Copyright
|
75
67
|
---------
|
76
|
-
Copyright (c) 2010 Jeroen van Dijk. See LICENSE for details.
|
68
|
+
Copyright (c) 2010-2012 Jeroen van Dijk. See LICENSE for details.
|
@@ -3,16 +3,15 @@ require 'mechanize'
|
|
3
3
|
|
4
4
|
class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
|
5
5
|
extend Forwardable
|
6
|
-
|
7
|
-
attr_reader :driver
|
8
|
-
|
6
|
+
|
9
7
|
def_delegator :agent, :scheme_handlers
|
10
8
|
def_delegator :agent, :scheme_handlers=
|
11
|
-
|
9
|
+
|
12
10
|
def initialize(driver)
|
13
11
|
@agent = ::Mechanize.new
|
14
12
|
@agent.redirect_ok = false
|
15
13
|
@agent.user_agent = default_user_agent
|
14
|
+
#@agent.agent.max_history = 0
|
16
15
|
if !driver.options.empty?
|
17
16
|
if driver.options[:certificate]
|
18
17
|
if !driver.options[:certificate].is_a? Hash
|
@@ -22,6 +21,9 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
|
|
22
21
|
@agent.cert = driver.options[:certificate][:cer]
|
23
22
|
@agent.key = driver.options[:certificate][:key]
|
24
23
|
end
|
24
|
+
if driver.options[:ca]
|
25
|
+
@agent.ca_file = driver.options[:ca]
|
26
|
+
end
|
25
27
|
end
|
26
28
|
if driver.options[:user_agent]
|
27
29
|
@agent.user_agent = driver.options[:user_agent]
|
@@ -45,221 +47,246 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
|
|
45
47
|
end
|
46
48
|
super
|
47
49
|
end
|
48
|
-
|
49
|
-
def auth(username, password)
|
50
|
-
@agent.basic_auth(username, password)
|
51
|
-
end
|
52
|
-
|
50
|
+
|
53
51
|
def reset_host!
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
52
|
+
@last_remote_uri = nil
|
53
|
+
@last_request_remote = nil
|
54
|
+
super
|
55
|
+
end
|
58
56
|
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
def current_url
|
58
|
+
last_request_remote? ? remote_response.current_url : super
|
59
|
+
end
|
62
60
|
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
def last_response
|
62
|
+
last_request_remote? ? remote_response : super
|
63
|
+
end
|
66
64
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
65
|
+
def follow_redirects!
|
66
|
+
5.times do
|
67
|
+
follow_redirect! if last_response.redirect?
|
68
|
+
end
|
69
|
+
raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
|
70
|
+
end
|
73
71
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
72
|
+
def follow_redirect!
|
73
|
+
unless last_response.redirect?
|
74
|
+
raise "Last response was not a redirect. Cannot follow_redirect!"
|
75
|
+
end
|
78
76
|
|
79
|
-
|
80
|
-
|
77
|
+
get(last_location_header)
|
78
|
+
end
|
81
79
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
80
|
+
def process(method, path, *options)
|
81
|
+
reset_cache!
|
82
|
+
send(method, path, *options)
|
83
|
+
follow_redirects!
|
84
|
+
end
|
87
85
|
|
88
|
-
|
89
|
-
|
86
|
+
def process_without_redirect(method, path, attributes, headers)
|
87
|
+
path = @last_path if path.nil? || path.empty?
|
90
88
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
89
|
+
if remote?(path)
|
90
|
+
process_remote_request(method, path, attributes, headers)
|
91
|
+
else
|
92
|
+
register_local_request
|
95
93
|
|
96
|
-
|
94
|
+
path = determine_path(path)
|
97
95
|
|
98
|
-
|
99
|
-
|
100
|
-
|
96
|
+
reset_cache!
|
97
|
+
send("racktest_#{method}", path, attributes, env.merge(headers))
|
98
|
+
end
|
101
99
|
|
102
|
-
|
103
|
-
|
100
|
+
@last_path = path
|
101
|
+
end
|
104
102
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
103
|
+
# TODO path Capybara to move this into its own method
|
104
|
+
def determine_path(path)
|
105
|
+
new_uri = URI.parse(path)
|
106
|
+
current_uri = URI.parse(current_url)
|
109
107
|
|
110
|
-
|
111
|
-
|
112
|
-
|
108
|
+
if new_uri.host
|
109
|
+
@current_host = new_uri.scheme + '://' + new_uri.host
|
110
|
+
end
|
113
111
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
end
|
124
|
-
end
|
125
|
-
path = current_host + path
|
112
|
+
if new_uri.relative?
|
113
|
+
path = request_path + path if path.start_with?('?')
|
114
|
+
|
115
|
+
unless path.start_with?('/')
|
116
|
+
folders = request_path.split('/')
|
117
|
+
if folders.empty?
|
118
|
+
path = '/' + path
|
119
|
+
else
|
120
|
+
path = (folders[0, folders.size - 1] << path).join('/')
|
126
121
|
end
|
127
|
-
path
|
128
122
|
end
|
123
|
+
path = current_host + path
|
124
|
+
end
|
125
|
+
path
|
126
|
+
end
|
129
127
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
128
|
+
alias :racktest_get :get
|
129
|
+
def get(path, attributes = {}, headers = {})
|
130
|
+
process_without_redirect(:get, path, attributes, headers)
|
131
|
+
end
|
134
132
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
133
|
+
alias :racktest_post :post
|
134
|
+
def post(path, attributes = {}, headers = {})
|
135
|
+
process_without_redirect(:post, path, post_data(attributes), headers)
|
136
|
+
end
|
139
137
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
138
|
+
alias :racktest_put :put
|
139
|
+
def put(path, attributes = {}, headers = {})
|
140
|
+
process_without_redirect(:put, path, attributes, headers)
|
141
|
+
end
|
144
142
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
143
|
+
alias :racktest_delete :delete
|
144
|
+
def delete(path, attributes = {}, headers = {})
|
145
|
+
process_without_redirect(:delete, path, attributes, headers)
|
146
|
+
end
|
149
147
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
end
|
163
|
-
memo
|
164
|
-
end
|
148
|
+
def post_data(params)
|
149
|
+
params.inject({}) do |memo, param|
|
150
|
+
case param
|
151
|
+
when Hash
|
152
|
+
param.each {|attribute, value| memo[attribute] = value }
|
153
|
+
memo
|
154
|
+
when Array
|
155
|
+
case param.last
|
156
|
+
when Hash
|
157
|
+
param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
|
158
|
+
else
|
159
|
+
memo[param.first] = param.last
|
165
160
|
end
|
161
|
+
memo
|
166
162
|
end
|
163
|
+
end
|
164
|
+
end
|
167
165
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
166
|
+
def remote?(url)
|
167
|
+
if Capybara.app_host
|
168
|
+
true
|
169
|
+
else
|
170
|
+
host = URI.parse(url).host
|
173
171
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
end
|
179
|
-
end
|
172
|
+
if host.nil?
|
173
|
+
last_request_remote?
|
174
|
+
else
|
175
|
+
!Capybara::Mechanize.local_hosts.include?(host)
|
180
176
|
end
|
177
|
+
end
|
178
|
+
end
|
181
179
|
|
182
|
-
|
180
|
+
attr_reader :agent
|
183
181
|
|
184
|
-
|
182
|
+
private
|
185
183
|
|
186
|
-
|
187
|
-
|
188
|
-
|
184
|
+
def last_location_header
|
185
|
+
last_request_remote? ? remote_response.page.response['Location'] : last_response['Location']
|
186
|
+
end
|
189
187
|
|
190
|
-
|
191
|
-
|
192
|
-
|
188
|
+
def last_request_remote?
|
189
|
+
!!@last_request_remote
|
190
|
+
end
|
193
191
|
|
194
|
-
|
195
|
-
|
196
|
-
|
192
|
+
def register_local_request
|
193
|
+
@last_remote_uri = nil
|
194
|
+
@last_request_remote = false
|
195
|
+
end
|
196
|
+
|
197
|
+
def process_remote_request(method, url, attributes, headers)
|
198
|
+
if remote?(url)
|
199
|
+
uri = URI.parse(url)
|
200
|
+
uri = resolve_relative_url(url) if uri.host.nil?
|
201
|
+
@last_remote_uri = uri
|
202
|
+
url = uri.to_s
|
203
|
+
|
204
|
+
reset_cache!
|
205
|
+
begin
|
206
|
+
args = []
|
207
|
+
args << attributes unless attributes.empty?
|
208
|
+
args << headers unless headers.empty?
|
209
|
+
@agent.send(method, url, *args)
|
210
|
+
rescue => e
|
211
|
+
raise "Received the following error for a #{method.to_s.upcase} request to #{url}: '#{e.message}'"
|
197
212
|
end
|
213
|
+
@last_request_remote = true
|
214
|
+
end
|
215
|
+
end
|
198
216
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
217
|
+
# paths are combined "intelligently" as close as they would be by the browser, but still interpreting
|
218
|
+
# routes from rails like "/user/login" as relative to a host including a possible subdirectory:
|
219
|
+
# eg: app_host = localhost/mysite/ url = / result = http://localhost/mysite/
|
220
|
+
# eg: app_host = localhost/mysite/ url = /home result = http://localhost/mysite/home
|
221
|
+
# eg: app_host = localhost/mysite/ url = /user/login result = http://localhost/mysite/user/login
|
222
|
+
# eg: app_host = localhost/mysite/ url = /mysite/login result = http://localhost/mysite/login
|
223
|
+
# **** notice that 'mysite' is not repeated! *****
|
224
|
+
def resolve_relative_url(url)
|
225
|
+
if @last_remote_uri
|
226
|
+
base_uri = @last_remote_uri
|
227
|
+
path_prefix = ''
|
228
|
+
else
|
229
|
+
host = Capybara.app_host || Capybara.default_host
|
230
|
+
# do this in text so we don't get the host interpretted as the scheme.
|
231
|
+
host = "http://#{host}" unless host =~ %r{^https?://.*}
|
232
|
+
base_uri = URI.parse(host)
|
233
|
+
base_uri.path = '/' if base_uri.path.nil? || base_uri.path.empty?
|
234
|
+
if base_uri.path.length > 1
|
235
|
+
path_prefix = base_uri.path
|
236
|
+
path_prefix += '/' unless path_prefix.end_with?('/') || url.start_with?('/')
|
237
|
+
|
238
|
+
# if the prefix is already in the path, don't let it be there twice.
|
239
|
+
if url.start_with?(path_prefix)
|
240
|
+
path_prefix = ''
|
221
241
|
end
|
242
|
+
else
|
243
|
+
path_prefix = ''
|
222
244
|
end
|
245
|
+
end
|
246
|
+
uri = URI.parse(path_prefix + url)
|
247
|
+
result_uri = base_uri.merge uri
|
248
|
+
end
|
223
249
|
|
224
|
-
def remote_response
|
225
|
-
ResponseProxy.new(@agent.current_page) if @agent.current_page
|
226
|
-
end
|
227
250
|
|
228
|
-
|
229
|
-
|
230
|
-
|
251
|
+
def remote_response
|
252
|
+
ResponseProxy.new(@agent.current_page) if @agent.current_page
|
253
|
+
end
|
231
254
|
|
232
|
-
|
233
|
-
|
255
|
+
def default_user_agent
|
256
|
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.853.0 Safari/535.2"
|
257
|
+
end
|
234
258
|
|
235
|
-
|
259
|
+
class ResponseProxy
|
260
|
+
extend Forwardable
|
236
261
|
|
237
|
-
|
262
|
+
def_delegator :page, :body
|
238
263
|
|
239
|
-
|
240
|
-
@page = page
|
241
|
-
end
|
264
|
+
attr_reader :page
|
242
265
|
|
243
|
-
|
244
|
-
|
245
|
-
|
266
|
+
def initialize(page)
|
267
|
+
@page = page
|
268
|
+
end
|
246
269
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
headers["cache-control"] = "no-cache"
|
251
|
-
headers["content-type"].gsub!(';charset=utf-8', '') if headers["content-type"]
|
252
|
-
headers
|
253
|
-
end
|
270
|
+
def current_url
|
271
|
+
page.uri.to_s
|
272
|
+
end
|
254
273
|
|
255
|
-
|
256
|
-
|
257
|
-
|
274
|
+
def headers
|
275
|
+
# Hax the content-type contains utf8, so Capybara specs are failing, need to ask mailinglist
|
276
|
+
headers = page.response
|
277
|
+
headers["content-type"].gsub!(';charset=utf-8', '') if headers["content-type"]
|
278
|
+
headers
|
279
|
+
end
|
258
280
|
|
259
|
-
|
260
|
-
|
261
|
-
|
281
|
+
def status
|
282
|
+
page.code.to_i
|
283
|
+
end
|
262
284
|
|
263
|
-
|
285
|
+
def redirect?
|
286
|
+
status >= 300 && status < 400
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
end
|
264
292
|
|
265
|
-
end
|
@@ -2,22 +2,6 @@ require 'capybara/spec/test_app'
|
|
2
2
|
|
3
3
|
class ExtendedTestApp < TestApp#< Sinatra::Base
|
4
4
|
set :environment, :production # so we don't get debug info that makes our test pass!
|
5
|
-
|
6
|
-
helpers do
|
7
|
-
|
8
|
-
def protected!
|
9
|
-
unless authorized?
|
10
|
-
response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
|
11
|
-
throw(:halt, [401, "Not authorized\n"])
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def authorized?
|
16
|
-
@auth ||= Rack::Auth::Basic::Request.new(request.env)
|
17
|
-
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'password']
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
5
|
|
22
6
|
get %r{/redirect_to/(.*)} do
|
23
7
|
redirect params[:captures]
|
@@ -50,10 +34,29 @@ class ExtendedTestApp < TestApp#< Sinatra::Base
|
|
50
34
|
post '/request_info/*' do
|
51
35
|
current_request_info
|
52
36
|
end
|
37
|
+
|
38
|
+
get '/host' do
|
39
|
+
"Current host is #{request.scheme}://#{request.host}:#{request.port}"
|
40
|
+
end
|
41
|
+
|
42
|
+
get '/subsite/relative_link_to_host' do
|
43
|
+
%{<a href="/subsite/request_info2/host">host</a>}
|
44
|
+
end
|
45
|
+
|
46
|
+
get '/subsite/local_link_to_host' do
|
47
|
+
%{<a href="request_info2/host">host</a>}
|
48
|
+
end
|
49
|
+
|
50
|
+
get '/subsite/request_info2/*' do
|
51
|
+
"subsite: " + current_request_info
|
52
|
+
end
|
53
|
+
|
54
|
+
get '/redirect_with_http_param' do
|
55
|
+
redirect '/redirect_target?foo=http'
|
56
|
+
end
|
53
57
|
|
54
|
-
get '/
|
55
|
-
|
56
|
-
%{Successfully authenticated}
|
58
|
+
get '/redirect_target' do
|
59
|
+
%{correct redirect}
|
57
60
|
end
|
58
61
|
|
59
62
|
private
|
@@ -61,4 +64,5 @@ class ExtendedTestApp < TestApp#< Sinatra::Base
|
|
61
64
|
def current_request_info
|
62
65
|
"Current host is #{request.url}, method #{request.request_method.downcase}"
|
63
66
|
end
|
64
|
-
end
|
67
|
+
end
|
68
|
+
|
@@ -29,6 +29,24 @@ describe Capybara::Mechanize::Driver do
|
|
29
29
|
@driver.post("#{REMOTE_TEST_URL}/form", {:form => "success"})
|
30
30
|
@driver.body.should include('success')
|
31
31
|
end
|
32
|
+
|
33
|
+
it "should throw an error when bad proxy option is passed" do
|
34
|
+
running do
|
35
|
+
Capybara::Mechanize::Driver.new(ExtendedTestApp, {:proxy => BAD_PROXY}).browser
|
36
|
+
end.should raise_error("ProxyError: You have entered an invalid proxy address #{BAD_PROXY}. e.g. (http|https)://proxy.com(:port)")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not throw an error when good proxy option is passed" do
|
40
|
+
running do
|
41
|
+
Capybara::Mechanize::Driver.new(ExtendedTestApp, {:proxy => GOOD_PROXY}).browser
|
42
|
+
end.should_not raise_error()
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not throw an error when good proxy with port option is passed" do
|
46
|
+
running do
|
47
|
+
Capybara::Mechanize::Driver.new(ExtendedTestApp, {:proxy => PROXY_WITH_PORT}).browser
|
48
|
+
end.should_not raise_error()
|
49
|
+
end
|
32
50
|
|
33
51
|
context "for a post request" do
|
34
52
|
|
@@ -48,6 +66,13 @@ describe Capybara::Mechanize::Driver do
|
|
48
66
|
|
49
67
|
end
|
50
68
|
|
69
|
+
describe "redirect" do
|
70
|
+
it "should handle redirects with http-params" do
|
71
|
+
@driver.visit "#{REMOTE_TEST_URL}/redirect_with_http_param"
|
72
|
+
@driver.body.should include('correct redirect')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
51
76
|
it_should_behave_like "driver"
|
52
77
|
it_should_behave_like "driver with header support"
|
53
78
|
it_should_behave_like "driver with status code support"
|
@@ -56,40 +81,3 @@ describe Capybara::Mechanize::Driver do
|
|
56
81
|
end
|
57
82
|
|
58
83
|
end
|
59
|
-
|
60
|
-
describe "Capybara::Mechanize::Driver, browser" do
|
61
|
-
|
62
|
-
before(:each) do
|
63
|
-
Capybara.app_host = REMOTE_TEST_URL
|
64
|
-
end
|
65
|
-
|
66
|
-
after(:each) do
|
67
|
-
Capybara.app_host = nil
|
68
|
-
end
|
69
|
-
|
70
|
-
context "in remote mode" do
|
71
|
-
it "should not throw an error when empty option is passed" do
|
72
|
-
running do
|
73
|
-
Capybara::Mechanize::Driver.new(ExtendedTestApp, {})
|
74
|
-
end.should_not raise_error()
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should throw an error when bad proxy option is passed" do
|
78
|
-
running do
|
79
|
-
Capybara::Mechanize::Driver.new(ExtendedTestApp, {:proxy => BAD_PROXY}).browser
|
80
|
-
end.should raise_error("ProxyError: You have entered an invalid proxy address #{BAD_PROXY}. e.g. (http|https)://proxy.com(:port)")
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should not throw an error when good proxy option is passed" do
|
84
|
-
running do
|
85
|
-
Capybara::Mechanize::Driver.new(ExtendedTestApp, {:proxy => GOOD_PROXY}).browser
|
86
|
-
end.should_not raise_error()
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should not throw an error when good proxy with port option is passed" do
|
90
|
-
running do
|
91
|
-
Capybara::Mechanize::Driver.new(ExtendedTestApp, {:proxy => PROXY_WITH_PORT}).browser
|
92
|
-
end.should_not raise_error()
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "rspec"
|
2
|
+
require 'capybara'
|
3
|
+
require 'capybara/mechanize/browser'
|
4
|
+
require File.expand_path('../spec_helper.rb', __FILE__)
|
5
|
+
|
6
|
+
describe_internally Capybara::Mechanize::Browser do
|
7
|
+
describe "Resolving Relative URLs" do
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
@original_app_host = Capybara.app_host
|
11
|
+
@driver = Object.new
|
12
|
+
@browser = Capybara::Mechanize::Browser.new(@driver)
|
13
|
+
end
|
14
|
+
|
15
|
+
after(:all) do
|
16
|
+
Capybara.app_host = @original_app_host
|
17
|
+
@browser.reset_host!
|
18
|
+
end
|
19
|
+
|
20
|
+
context "resolving on 'http://localhost'" do
|
21
|
+
before(:all) do
|
22
|
+
Capybara.app_host ='http://localhost'
|
23
|
+
end
|
24
|
+
|
25
|
+
it "resolves '/'" do
|
26
|
+
@browser.resolve_relative_url('/').to_s.should == 'http://localhost/'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "resolves '/home'" do
|
30
|
+
@browser.resolve_relative_url('/home').to_s.should == 'http://localhost/home'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "resolves 'home'" do
|
34
|
+
@browser.resolve_relative_url('home').to_s.should == 'http://localhost/home'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "resolves 'user/login'" do
|
38
|
+
@browser.resolve_relative_url('user/login').to_s.should == 'http://localhost/user/login'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "resolving on 'http://localhost/subsite'" do
|
43
|
+
before() do
|
44
|
+
Capybara.app_host='http://localhost/subsite'
|
45
|
+
end
|
46
|
+
|
47
|
+
it "resolves '/'" do
|
48
|
+
@browser.resolve_relative_url('/').to_s.should == 'http://localhost/subsite/'
|
49
|
+
end
|
50
|
+
|
51
|
+
it "resolves '/home'" do
|
52
|
+
@browser.resolve_relative_url('/home').to_s.should == 'http://localhost/subsite/home'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "resolves 'home'" do
|
56
|
+
@browser.resolve_relative_url('home').to_s.should == 'http://localhost/subsite/home'
|
57
|
+
end
|
58
|
+
|
59
|
+
it "resolves 'user/login'" do
|
60
|
+
@browser.resolve_relative_url('user/login').to_s.should == 'http://localhost/subsite/user/login'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "resolves '/subsite/user/login'" do
|
64
|
+
@browser.resolve_relative_url('/subsite/user/login').to_s.should == 'http://localhost/subsite/user/login'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -56,43 +56,4 @@ describe Capybara::Session do
|
|
56
56
|
it_should_behave_like "session with headers support"
|
57
57
|
it_should_behave_like "session with status code support"
|
58
58
|
end
|
59
|
-
|
60
|
-
context "Custom user agent" do
|
61
|
-
|
62
|
-
before do
|
63
|
-
@session = Capybara::Session.new(:mechanize, TestApp)
|
64
|
-
@session.driver.options[:respect_data_method] = true
|
65
|
-
Capybara.default_host = 'http://www.local.com'
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should allow setting of custom user agents" do
|
69
|
-
@session.driver.options[:user_agent] = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"
|
70
|
-
@session.visit("#{REMOTE_TEST_URL}/request_info/user_agent")
|
71
|
-
@session.body.should include("Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3")
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context "Basic HTTP Auth" do
|
76
|
-
|
77
|
-
before do
|
78
|
-
@session = Capybara::Session.new(:mechanize, TestApp)
|
79
|
-
@session.driver.options[:respect_data_method] = true
|
80
|
-
Capybara.default_host = 'http://www.local.com'
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should allow basic authentication to be setup" do
|
84
|
-
@session.driver.options[:username] = "admin"
|
85
|
-
@session.driver.options[:password] = "password"
|
86
|
-
@session.visit("#{REMOTE_TEST_URL}/basic_auth/")
|
87
|
-
@session.body.should include("Successfully authenticated")
|
88
|
-
end
|
89
|
-
|
90
|
-
it "should allow basic authentication via auth method" do
|
91
|
-
@session.driver.browser.auth("admin", "password")
|
92
|
-
@session.visit("#{REMOTE_TEST_URL}/basic_auth/")
|
93
|
-
@session.body.should include("Successfully authenticated")
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
97
|
-
|
98
59
|
end
|
@@ -35,6 +35,8 @@ describe Capybara::Session do
|
|
35
35
|
@session.body.should include('The requested object was deleted')
|
36
36
|
end
|
37
37
|
end
|
38
|
+
|
39
|
+
|
38
40
|
|
39
41
|
# Pending: Still 16 failing tests here (result is 706 examples, 16 failures, instead of 385 examples)
|
40
42
|
# it_should_behave_like "session"
|
@@ -42,5 +44,24 @@ describe Capybara::Session do
|
|
42
44
|
it_should_behave_like "session without javascript support"
|
43
45
|
it_should_behave_like "session with headers support"
|
44
46
|
it_should_behave_like "session with status code support"
|
47
|
+
|
48
|
+
|
49
|
+
context "remote app in a sub-domain" do
|
50
|
+
before :each do
|
51
|
+
Capybara.app_host = "#{REMOTE_TEST_URL}/subsite"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "follows relative link correctly" do
|
55
|
+
@session.visit "/relative_link_to_host"
|
56
|
+
@session.click_link "host"
|
57
|
+
@session.body.should include('request_info2/host')
|
58
|
+
end
|
59
|
+
|
60
|
+
it "follows local link correctly" do
|
61
|
+
@session.visit "/local_link_to_host"
|
62
|
+
@session.click_link "host"
|
63
|
+
@session.body.should include('request_info2/host')
|
64
|
+
end
|
65
|
+
end
|
45
66
|
end
|
46
67
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
require 'bundler/setup'
|
2
2
|
require 'capybara'
|
3
|
+
require 'capybara/dsl'
|
3
4
|
require 'capybara/mechanize'
|
4
|
-
require '
|
5
|
+
require 'capybara/spec/extended_test_app'
|
5
6
|
|
6
7
|
require 'sinatra'
|
7
8
|
|
8
|
-
require 'capybara/spec/extended_test_app'
|
9
|
-
|
10
9
|
# TODO move this stuff into capybara
|
11
10
|
require 'capybara/spec/driver'
|
12
11
|
require 'capybara/spec/session'
|
@@ -14,24 +13,35 @@ require 'capybara/spec/session'
|
|
14
13
|
alias :running :lambda
|
15
14
|
|
16
15
|
Capybara.default_wait_time = 0 # less timeout so tests run faster
|
16
|
+
Capybara.app = ExtendedTestApp
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
Artifice.activate_with(ExtendedTestApp)
|
21
|
-
end
|
18
|
+
rack_server = Capybara::Server.new(Capybara.app)
|
19
|
+
rack_server.boot
|
22
20
|
|
21
|
+
RSpec.configure do |config|
|
23
22
|
config.after do
|
24
23
|
Capybara.default_selector = :xpath
|
25
24
|
Capybara::Mechanize.local_hosts = nil
|
26
25
|
end
|
27
|
-
|
28
|
-
config.after(:all) do
|
29
|
-
Artifice.deactivate
|
30
|
-
end
|
31
26
|
# config.filter_run :focus => true
|
32
27
|
end
|
33
28
|
|
34
|
-
REMOTE_TEST_URL = "http://localhost"
|
35
|
-
|
36
|
-
|
37
|
-
|
29
|
+
REMOTE_TEST_URL = "http://localhost:#{rack_server.port}"
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
# for testing private methods, courtesy of
|
34
|
+
# http://kailuowang.blogspot.com.au/2010/08/testing-private-methods-in-rspec.html
|
35
|
+
def describe_internally *args, &block
|
36
|
+
example = describe *args, &block
|
37
|
+
klass = args[0]
|
38
|
+
if klass.is_a? Class
|
39
|
+
saved_private_instance_methods = klass.private_instance_methods
|
40
|
+
example.before do
|
41
|
+
klass.class_eval { public *saved_private_instance_methods }
|
42
|
+
end
|
43
|
+
example.after do
|
44
|
+
klass.class_eval { private *saved_private_instance_methods }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bbc-capybara-mechanize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 5
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 5
|
10
|
+
version: 0.4.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jeroen van Dijk
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2012-
|
19
|
+
date: 2012-08-02 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
@@ -24,12 +24,11 @@ dependencies:
|
|
24
24
|
requirements:
|
25
25
|
- - ~>
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
hash:
|
27
|
+
hash: 5
|
28
28
|
segments:
|
29
29
|
- 2
|
30
|
-
-
|
31
|
-
|
32
|
-
version: 2.5.1
|
30
|
+
- 3
|
31
|
+
version: "2.3"
|
33
32
|
prerelease: false
|
34
33
|
type: :runtime
|
35
34
|
name: mechanize
|
@@ -40,12 +39,11 @@ dependencies:
|
|
40
39
|
requirements:
|
41
40
|
- - ~>
|
42
41
|
- !ruby/object:Gem::Version
|
43
|
-
hash:
|
42
|
+
hash: 13
|
44
43
|
segments:
|
45
44
|
- 1
|
46
45
|
- 1
|
47
|
-
|
48
|
-
version: 1.1.2
|
46
|
+
version: "1.1"
|
49
47
|
prerelease: false
|
50
48
|
type: :runtime
|
51
49
|
name: capybara
|
@@ -67,6 +65,7 @@ files:
|
|
67
65
|
- lib/capybara/spec/extended_test_app.rb
|
68
66
|
- spec/driver/mechanize_driver_spec.rb
|
69
67
|
- spec/driver/remote_mechanize_driver_spec.rb
|
68
|
+
- spec/relative_urls_spec.rb
|
70
69
|
- spec/session/mechanize_spec.rb
|
71
70
|
- spec/session/remote_mechanize_spec.rb
|
72
71
|
- spec/spec_helper.rb
|