capybara-mechanize 0.3.0.rc3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.mdown +1 -1
- data/lib/capybara/mechanize/browser.rb +41 -12
- data/lib/capybara/mechanize/version.rb +1 -1
- data/lib/capybara/spec/extended_test_app.rb +23 -0
- data/spec/driver/remote_mechanize_driver_spec.rb +7 -0
- data/spec/relative_urls_spec.rb +68 -0
- data/spec/session/remote_mechanize_spec.rb +21 -0
- data/spec/spec_helper.rb +25 -12
- metadata +11 -15
data/README.mdown
CHANGED
@@ -15,7 +15,7 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def reset_host!
|
18
|
-
@
|
18
|
+
@last_remote_uri = nil
|
19
19
|
@last_request_remote = nil
|
20
20
|
super
|
21
21
|
end
|
@@ -156,21 +156,16 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
|
|
156
156
|
end
|
157
157
|
|
158
158
|
def register_local_request
|
159
|
-
@
|
159
|
+
@last_remote_uri = nil
|
160
160
|
@last_request_remote = false
|
161
161
|
end
|
162
162
|
|
163
163
|
def process_remote_request(method, url, attributes, headers)
|
164
164
|
if remote?(url)
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
url = File.join(remote_host, url)
|
170
|
-
url = "http://#{url}" unless url.include?("http")
|
171
|
-
else
|
172
|
-
@last_remote_host = "#{remote_uri.host}:#{remote_uri.port}"
|
173
|
-
end
|
165
|
+
uri = URI.parse(url)
|
166
|
+
uri = resolve_relative_url(url) if uri.host.nil?
|
167
|
+
@last_remote_uri = uri
|
168
|
+
url = uri.to_s
|
174
169
|
|
175
170
|
reset_cache!
|
176
171
|
begin
|
@@ -185,6 +180,40 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
|
|
185
180
|
end
|
186
181
|
end
|
187
182
|
|
183
|
+
# paths are combined "intelligently" as close as they would be by the browser, but still interpreting
|
184
|
+
# routes from rails like "/user/login" as relative to a host including a possible subdirectory:
|
185
|
+
# eg: app_host = localhost/mysite/ url = / result = http://localhost/mysite/
|
186
|
+
# eg: app_host = localhost/mysite/ url = /home result = http://localhost/mysite/home
|
187
|
+
# eg: app_host = localhost/mysite/ url = /user/login result = http://localhost/mysite/user/login
|
188
|
+
# eg: app_host = localhost/mysite/ url = /mysite/login result = http://localhost/mysite/login
|
189
|
+
# **** notice that 'mysite' is not repeated! *****
|
190
|
+
def resolve_relative_url(url)
|
191
|
+
if @last_remote_uri
|
192
|
+
base_uri = @last_remote_uri
|
193
|
+
path_prefix = ''
|
194
|
+
else
|
195
|
+
host = Capybara.app_host || Capybara.default_host
|
196
|
+
# do this in text so we don't get the host interpretted as the scheme.
|
197
|
+
host = "http://#{host}" unless host =~ %r{^https?://.*}
|
198
|
+
base_uri = URI.parse(host)
|
199
|
+
base_uri.path = '/' if base_uri.path.nil? || base_uri.path.empty?
|
200
|
+
if base_uri.path.length > 1
|
201
|
+
path_prefix = base_uri.path
|
202
|
+
path_prefix += '/' unless path_prefix.end_with?('/') || url.start_with?('/')
|
203
|
+
|
204
|
+
# if the prefix is already in the path, don't let it be there twice.
|
205
|
+
if url.start_with?(path_prefix)
|
206
|
+
path_prefix = ''
|
207
|
+
end
|
208
|
+
else
|
209
|
+
path_prefix = ''
|
210
|
+
end
|
211
|
+
end
|
212
|
+
uri = URI.parse(path_prefix + url)
|
213
|
+
result_uri = base_uri.merge uri
|
214
|
+
end
|
215
|
+
|
216
|
+
|
188
217
|
def remote_response
|
189
218
|
ResponseProxy.new(@agent.current_page) if @agent.current_page
|
190
219
|
end
|
@@ -220,7 +249,7 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
|
|
220
249
|
end
|
221
250
|
|
222
251
|
def redirect?
|
223
|
-
|
252
|
+
status >= 300 && status < 400
|
224
253
|
end
|
225
254
|
|
226
255
|
end
|
@@ -35,6 +35,29 @@ class ExtendedTestApp < TestApp#< Sinatra::Base
|
|
35
35
|
current_request_info
|
36
36
|
end
|
37
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
|
57
|
+
|
58
|
+
get '/redirect_target' do
|
59
|
+
%{correct redirect}
|
60
|
+
end
|
38
61
|
|
39
62
|
private
|
40
63
|
|
@@ -48,6 +48,13 @@ describe Capybara::Mechanize::Driver do
|
|
48
48
|
|
49
49
|
end
|
50
50
|
|
51
|
+
describe "redirect" do
|
52
|
+
it "should handle redirects with http-params" do
|
53
|
+
@driver.visit "#{REMOTE_TEST_URL}/redirect_with_http_param"
|
54
|
+
@driver.body.should include('correct redirect')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
51
58
|
it_should_behave_like "driver"
|
52
59
|
it_should_behave_like "driver with header support"
|
53
60
|
it_should_behave_like "driver with status code support"
|
@@ -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
|
@@ -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,21 +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"
|
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,39 +1,38 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capybara-mechanize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jeroen van Dijk
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
default_executable:
|
12
|
+
date: 2012-03-14 00:00:00.000000000Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: mechanize
|
17
|
-
requirement: &
|
16
|
+
requirement: &70180442685720 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ~>
|
21
20
|
- !ruby/object:Gem::Version
|
22
|
-
version: 2.
|
21
|
+
version: '2.3'
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *70180442685720
|
26
25
|
- !ruby/object:Gem::Dependency
|
27
26
|
name: capybara
|
28
|
-
requirement: &
|
27
|
+
requirement: &70180442685240 !ruby/object:Gem::Requirement
|
29
28
|
none: false
|
30
29
|
requirements:
|
31
30
|
- - ~>
|
32
31
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1.1
|
32
|
+
version: '1.1'
|
34
33
|
type: :runtime
|
35
34
|
prerelease: false
|
36
|
-
version_requirements: *
|
35
|
+
version_requirements: *70180442685240
|
37
36
|
description: RackTest driver for Capybara, but with remote request support thanks
|
38
37
|
to mechanize
|
39
38
|
email: jeroen@jeevidee.nl
|
@@ -49,11 +48,11 @@ files:
|
|
49
48
|
- lib/capybara/spec/extended_test_app.rb
|
50
49
|
- spec/driver/mechanize_driver_spec.rb
|
51
50
|
- spec/driver/remote_mechanize_driver_spec.rb
|
51
|
+
- spec/relative_urls_spec.rb
|
52
52
|
- spec/session/mechanize_spec.rb
|
53
53
|
- spec/session/remote_mechanize_spec.rb
|
54
54
|
- spec/spec_helper.rb
|
55
55
|
- README.mdown
|
56
|
-
has_rdoc: true
|
57
56
|
homepage: https://github.com/jeroenvandijk/capybara-mechanize
|
58
57
|
licenses: []
|
59
58
|
post_install_message:
|
@@ -67,9 +66,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
66
|
- - ! '>='
|
68
67
|
- !ruby/object:Gem::Version
|
69
68
|
version: '0'
|
70
|
-
segments:
|
71
|
-
- 0
|
72
|
-
hash: -3177128084939450317
|
73
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
70
|
none: false
|
75
71
|
requirements:
|
@@ -78,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
74
|
version: '0'
|
79
75
|
requirements: []
|
80
76
|
rubyforge_project:
|
81
|
-
rubygems_version: 1.
|
77
|
+
rubygems_version: 1.8.15
|
82
78
|
signing_key:
|
83
79
|
specification_version: 3
|
84
80
|
summary: RackTest driver for Capybara with remote request support
|