capybara-mechanize 0.3.0.rc2 → 0.3.0.rc3

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 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-2011 Jeroen van Dijk. See LICENSE for details.
@@ -3,78 +3,69 @@ require 'mechanize'
3
3
 
4
4
  class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
5
5
  extend Forwardable
6
-
6
+
7
7
  def_delegator :agent, :scheme_handlers
8
8
  def_delegator :agent, :scheme_handlers=
9
-
10
- def initialize(app, options)
9
+
10
+ def initialize(driver)
11
11
  @agent = ::Mechanize.new
12
12
  @agent.redirect_ok = false
13
-
13
+ @agent.user_agent = default_user_agent
14
14
  super
15
15
  end
16
-
16
+
17
17
  def reset_host!
18
18
  @last_remote_host = nil
19
19
  @last_request_remote = nil
20
20
  super
21
21
  end
22
-
22
+
23
23
  def current_url
24
24
  last_request_remote? ? remote_response.current_url : super
25
25
  end
26
-
26
+
27
27
  def last_response
28
28
  last_request_remote? ? remote_response : super
29
29
  end
30
-
30
+
31
31
  def follow_redirects!
32
32
  5.times do
33
33
  follow_redirect! if last_response.redirect?
34
34
  end
35
35
  raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
36
36
  end
37
-
38
- # TODO see how this can be cleaned up
37
+
39
38
  def follow_redirect!
40
39
  unless last_response.redirect?
41
40
  raise "Last response was not a redirect. Cannot follow_redirect!"
42
41
  end
43
-
44
- location = if last_request_remote?
45
- remote_response.page.response['Location']
46
- else
47
- last_response['Location']
48
- end
49
-
50
- get(location)
42
+
43
+ get(last_location_header)
51
44
  end
52
-
53
-
45
+
54
46
  def process(method, path, *options)
55
47
  reset_cache!
56
- process_without_redirect(method, path, *options)
48
+ send(method, path, *options)
57
49
  follow_redirects!
58
50
  end
59
-
60
- def process_without_redirect(method, path, *options)
51
+
52
+ def process_without_redirect(method, path, attributes, headers)
53
+ path = @last_path if path.nil? || path.empty?
54
+
61
55
  if remote?(path)
62
- process_remote_request(method, path, *options)
56
+ process_remote_request(method, path, attributes, headers)
63
57
  else
64
58
  register_local_request
65
-
66
-
59
+
67
60
  path = determine_path(path)
68
-
69
- attributes, headers = *options
70
- attributes ||= {}
71
- headers ||= {}
72
-
61
+
73
62
  reset_cache!
74
63
  send("racktest_#{method}", path, attributes, env.merge(headers))
75
64
  end
65
+
66
+ @last_path = path
76
67
  end
77
-
68
+
78
69
  # TODO path Capybara to move this into its own method
79
70
  def determine_path(path)
80
71
  new_uri = URI.parse(path)
@@ -83,13 +74,17 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
83
74
  if new_uri.host
84
75
  @current_host = new_uri.scheme + '://' + new_uri.host
85
76
  end
86
-
77
+
87
78
  if new_uri.relative?
88
79
  path = request_path + path if path.start_with?('?')
89
-
80
+
90
81
  unless path.start_with?('/')
91
82
  folders = request_path.split('/')
92
- path = (folders[0, folders.size - 1] << path).join('/')
83
+ if folders.empty?
84
+ path = '/' + path
85
+ else
86
+ path = (folders[0, folders.size - 1] << path).join('/')
87
+ end
93
88
  end
94
89
  path = current_host + path
95
90
  end
@@ -97,25 +92,25 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
97
92
  end
98
93
 
99
94
  alias :racktest_get :get
100
- def get(path, attributes = {})
101
- process_without_redirect(:get, path, attributes)
95
+ def get(path, attributes = {}, headers = {})
96
+ process_without_redirect(:get, path, attributes, headers)
102
97
  end
103
98
 
104
99
  alias :racktest_post :post
105
100
  def post(path, attributes = {}, headers = {})
106
101
  process_without_redirect(:post, path, post_data(attributes), headers)
107
102
  end
108
-
103
+
109
104
  alias :racktest_put :put
110
- def put(method, path, attributes = {}, headers = {})
105
+ def put(path, attributes = {}, headers = {})
111
106
  process_without_redirect(:put, path, attributes, headers)
112
107
  end
113
-
108
+
114
109
  alias :racktest_delete :delete
115
- def delete(method, path, attributes = {}, headers = {})
110
+ def delete(path, attributes = {}, headers = {})
116
111
  process_without_redirect(:delete, path, attributes, headers)
117
112
  end
118
-
113
+
119
114
  def post_data(params)
120
115
  params.inject({}) do |memo, param|
121
116
  case param
@@ -133,13 +128,13 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
133
128
  end
134
129
  end
135
130
  end
136
-
131
+
137
132
  def remote?(url)
138
133
  if Capybara.app_host
139
134
  true
140
135
  else
141
136
  host = URI.parse(url).host
142
-
137
+
143
138
  if host.nil?
144
139
  last_request_remote?
145
140
  else
@@ -147,24 +142,28 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
147
142
  end
148
143
  end
149
144
  end
150
-
145
+
151
146
  attr_reader :agent
152
-
147
+
153
148
  private
154
-
149
+
150
+ def last_location_header
151
+ last_request_remote? ? remote_response.page.response['Location'] : last_response['Location']
152
+ end
153
+
155
154
  def last_request_remote?
156
155
  !!@last_request_remote
157
156
  end
158
-
157
+
159
158
  def register_local_request
160
159
  @last_remote_host = nil
161
160
  @last_request_remote = false
162
161
  end
163
-
164
- def process_remote_request(method, url, *options)
162
+
163
+ def process_remote_request(method, url, attributes, headers)
165
164
  if remote?(url)
166
165
  remote_uri = URI.parse(url)
167
-
166
+
168
167
  if remote_uri.host.nil?
169
168
  remote_host = @last_remote_host || Capybara.app_host || Capybara.default_host
170
169
  url = File.join(remote_host, url)
@@ -172,52 +171,59 @@ class Capybara::Mechanize::Browser < Capybara::RackTest::Browser
172
171
  else
173
172
  @last_remote_host = "#{remote_uri.host}:#{remote_uri.port}"
174
173
  end
175
-
174
+
176
175
  reset_cache!
177
176
  begin
178
- @agent.send *( [method, url] + options)
177
+ args = []
178
+ args << attributes unless attributes.empty?
179
+ args << headers unless headers.empty?
180
+ @agent.send(method, url, *args)
179
181
  rescue => e
180
182
  raise "Received the following error for a #{method.to_s.upcase} request to #{url}: '#{e.message}'"
181
183
  end
182
184
  @last_request_remote = true
183
185
  end
184
186
  end
185
-
187
+
186
188
  def remote_response
187
189
  ResponseProxy.new(@agent.current_page) if @agent.current_page
188
190
  end
189
-
191
+
192
+ def default_user_agent
193
+ "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"
194
+ end
195
+
190
196
  class ResponseProxy
191
197
  extend Forwardable
192
-
198
+
193
199
  def_delegator :page, :body
194
-
200
+
195
201
  attr_reader :page
196
-
202
+
197
203
  def initialize(page)
198
204
  @page = page
199
205
  end
200
-
206
+
201
207
  def current_url
202
208
  page.uri.to_s
203
209
  end
204
-
210
+
205
211
  def headers
206
- # Hax the content-type contains utf8, so Capybara specs are failing, need to ask mailinglist
212
+ # Hax the content-type contains utf8, so Capybara specs are failing, need to ask mailinglist
207
213
  headers = page.response
208
214
  headers["content-type"].gsub!(';charset=utf-8', '') if headers["content-type"]
209
215
  headers
210
216
  end
211
-
217
+
212
218
  def status
213
219
  page.code.to_i
214
- end
215
-
220
+ end
221
+
216
222
  def redirect?
217
223
  [301, 302].include?(status)
218
224
  end
219
-
220
- end
221
-
225
+
226
+ end
227
+
222
228
  end
223
229
 
@@ -15,7 +15,7 @@ class Capybara::Mechanize::Driver < Capybara::RackTest::Driver
15
15
  end
16
16
 
17
17
  def browser
18
- @browser ||= Capybara::Mechanize::Browser.new(app, options)
18
+ @browser ||= Capybara::Mechanize::Browser.new(self)
19
19
  end
20
20
 
21
21
  end
@@ -0,0 +1,5 @@
1
+ module Capybara
2
+ module Mechanize
3
+ VERSION = '0.3.0.rc3'
4
+ end
5
+ end
@@ -2,27 +2,44 @@ 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
-
5
+
6
6
  get %r{/redirect_to/(.*)} do
7
7
  redirect params[:captures]
8
8
  end
9
-
10
- get '/host' do
11
- "Current host is #{request.scheme}://#{request.host}:#{request.port}, method get"
12
- end
13
-
9
+
14
10
  get '/form_with_relative_action_to_host' do
15
- %{<form action="/host" method="post">
11
+ %{<form action="/request_info/host" method="post">
12
+ <input type="submit" value="submit" />
13
+ </form>}
14
+ end
15
+
16
+ get '/request_info/form_with_no_action' do
17
+ %{<form method="post">
16
18
  <input type="submit" value="submit" />
17
19
  </form>}
18
20
  end
19
-
21
+
20
22
  get '/relative_link_to_host' do
21
- %{<a href="/host">host</a>}
23
+ %{<a href="/request_info/host">host</a>}
22
24
  end
23
-
24
- post '/host' do
25
- "Current host is #{request.scheme}://#{request.host}:#{request.port}, method post"
25
+
26
+ get '/request_info/user_agent' do
27
+ request.user_agent
28
+ end
29
+
30
+ get '/request_info/*' do
31
+ current_request_info
32
+ end
33
+
34
+ post '/request_info/*' do
35
+ current_request_info
26
36
  end
37
+
38
+
39
+ private
40
+
41
+ def current_request_info
42
+ "Current host is #{request.url}, method #{request.request_method.downcase}"
43
+ end
27
44
  end
28
45
 
@@ -114,10 +114,10 @@ describe "Capybara::Driver::Mechanize, in local model" do
114
114
  Capybara.default_host = nil
115
115
  end
116
116
 
117
- it "should raise a useful error for sites that return a 404, because it is probably a misconfiguration", :focus => true do
117
+ it "should raise a useful error for sites that return a 404, because it is probably a misconfiguration" do
118
118
  lambda {
119
119
  @driver.visit("http://iamreallysurethatthisdoesntexist.com/canttouchthis")
120
- }.should raise_error("Received the following error for a GET request to http://iamreallysurethatthisdoesntexist.com/canttouchthis: 'getaddrinfo: nodename nor servname provided, or not known'")
120
+ }.should raise_error(%r{Received the following error for a GET request to http://iamreallysurethatthisdoesntexist.com/canttouchthis:})
121
121
  end
122
122
  end
123
123
 
@@ -39,6 +39,15 @@ describe Capybara::Mechanize::Driver do
39
39
 
40
40
  end
41
41
 
42
+ context "process remote request" do
43
+
44
+ it "should transform nested map in post data" do
45
+ @driver.submit(:post, "#{REMOTE_TEST_URL}/form", {:form => {:key => "value"}})
46
+ @driver.body.should include('key: value')
47
+ end
48
+
49
+ end
50
+
42
51
  it_should_behave_like "driver"
43
52
  it_should_behave_like "driver with header support"
44
53
  it_should_behave_like "driver with status code support"
@@ -4,6 +4,7 @@ describe Capybara::Session do
4
4
  context 'with mechanize driver' do
5
5
  before do
6
6
  @session = Capybara::Session.new(:mechanize, TestApp)
7
+ @session.driver.options[:respect_data_method] = true
7
8
  Capybara.default_host = 'http://www.local.com'
8
9
  end
9
10
 
@@ -26,17 +27,28 @@ describe Capybara::Session do
26
27
  @session.body.should include('The requested object was deleted')
27
28
  end
28
29
  end
29
-
30
+
30
31
  it "should use the last remote url when following relative links" do
31
32
  @session.visit("#{REMOTE_TEST_URL}/relative_link_to_host")
32
33
  @session.click_link "host"
33
- @session.body.should include("Current host is #{REMOTE_TEST_URL}, method get")
34
+ @session.body.should include("Current host is #{REMOTE_TEST_URL}/request_info/host, method get")
34
35
  end
35
-
36
+
36
37
  it "should use the last remote url when submitting a form with a relative action" do
37
38
  @session.visit("#{REMOTE_TEST_URL}/form_with_relative_action_to_host")
38
39
  @session.click_button "submit"
39
- @session.body.should include("Current host is #{REMOTE_TEST_URL}, method post")
40
+ @session.body.should include("Current host is #{REMOTE_TEST_URL}/request_info/host, method post")
41
+ end
42
+
43
+ it "should use the last url when submitting a form with no action" do
44
+ @session.visit("#{REMOTE_TEST_URL}/request_info/form_with_no_action")
45
+ @session.click_button "submit"
46
+ @session.body.should include("Current host is #{REMOTE_TEST_URL}/request_info/form_with_no_action, method post")
47
+ end
48
+
49
+ it "should send correct user agent" do
50
+ @session.visit("#{REMOTE_TEST_URL}/request_info/user_agent")
51
+ @session.body.should include("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")
40
52
  end
41
53
 
42
54
  it_should_behave_like "session"
@@ -44,4 +56,4 @@ describe Capybara::Session do
44
56
  it_should_behave_like "session with headers support"
45
57
  it_should_behave_like "session with status code support"
46
58
  end
47
- end
59
+ end
@@ -13,6 +13,7 @@ describe Capybara::Session do
13
13
 
14
14
  before do
15
15
  @session = Capybara::Session.new(:mechanize)
16
+ @session.driver.options[:respect_data_method] = true
16
17
  end
17
18
 
18
19
  describe '#driver' do
@@ -42,4 +43,4 @@ describe Capybara::Session do
42
43
  it_should_behave_like "session with headers support"
43
44
  it_should_behave_like "session with status code support"
44
45
  end
45
- end
46
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'bundler/setup'
2
2
  require 'capybara'
3
3
  require 'capybara/mechanize'
4
+ require 'artifice'
4
5
 
5
6
  require 'sinatra'
6
7
 
@@ -15,17 +16,19 @@ alias :running :lambda
15
16
  Capybara.default_wait_time = 0 # less timeout so tests run faster
16
17
 
17
18
  RSpec.configure do |config|
19
+ config.before(:all) do
20
+ Artifice.activate_with(ExtendedTestApp)
21
+ end
22
+
18
23
  config.after do
19
24
  Capybara.default_selector = :xpath
20
25
  Capybara::Mechanize.local_hosts = nil
21
26
  end
27
+
28
+ config.after(:all) do
29
+ Artifice.deactivate
30
+ end
22
31
  # config.filter_run :focus => true
23
32
  end
24
33
 
25
- # Until this library is merged with capybara there needs to be a local app and you need to add
26
- # Install pow (get.pow.cx) and run add a symlink in ~/.pow with ln -s lib/capybara/spec capybara-testapp.heroku
27
- if ENV['HOME'] =~ /jvandijk/ # TODO don't tie it to my personal stuff :)
28
- REMOTE_TEST_URL = "http://capybara-testapp.heroku.dev:80"
29
- else
30
- REMOTE_TEST_URL = "http://capybara-mechanize-testapp.herokuapp.com:80"
31
- end
34
+ REMOTE_TEST_URL = "http://localhost"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-mechanize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.rc2
4
+ version: 0.3.0.rc3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,31 +9,31 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-13 00:00:00.000000000 +02:00
12
+ date: 2011-09-09 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mechanize
17
- requirement: &2152844820 !ruby/object:Gem::Requirement
17
+ requirement: &2168594720 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
21
21
  - !ruby/object:Gem::Version
22
- version: 1.0.0
22
+ version: 2.0.0
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2152844820
25
+ version_requirements: *2168594720
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: capybara
28
- requirement: &2152843680 !ruby/object:Gem::Requirement
28
+ requirement: &2168594240 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 1.0.0
33
+ version: 1.1.0
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2152843680
36
+ version_requirements: *2168594240
37
37
  description: RackTest driver for Capybara, but with remote request support thanks
38
38
  to mechanize
39
39
  email: jeroen@jeevidee.nl
@@ -44,12 +44,11 @@ files:
44
44
  - lib/capybara/mechanize/browser.rb
45
45
  - lib/capybara/mechanize/cucumber.rb
46
46
  - lib/capybara/mechanize/driver.rb
47
+ - lib/capybara/mechanize/version.rb
47
48
  - lib/capybara/mechanize.rb
48
49
  - lib/capybara/spec/extended_test_app.rb
49
- - lib/capybara/spec/tmp/restart.txt
50
50
  - spec/driver/mechanize_driver_spec.rb
51
51
  - spec/driver/remote_mechanize_driver_spec.rb
52
- - spec/README.mdown
53
52
  - spec/session/mechanize_spec.rb
54
53
  - spec/session/remote_mechanize_spec.rb
55
54
  - spec/spec_helper.rb
@@ -70,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
69
  version: '0'
71
70
  segments:
72
71
  - 0
73
- hash: -4133827917572927530
72
+ hash: -3177128084939450317
74
73
  required_rubygems_version: !ruby/object:Gem::Requirement
75
74
  none: false
76
75
  requirements:
File without changes
data/spec/README.mdown DELETED
@@ -1,76 +0,0 @@
1
- Capybara-mechanize
2
- ==================
3
-
4
- This gems makes it possible to use Capybara for (partially) remote testing. It inherits most functionality from the RackTest driver and only uses [Mechanize](http://github.com/tenderlove/mechanize) for remote requests.
5
-
6
- It is currently in use to test the integration between a Rails application and Twitter authorization and sharing.
7
-
8
- This gem is a [Capybara](http://github.com/jnicklas/capybara) extension. I have been inspired by the Capybara driver and some earlier efforts for a Mechanize driver.
9
-
10
- Thanks to [Pinkelstar](http://www.pinkelstar.com) for giving me the time and the need to develop this gem.
11
-
12
- ### Installation
13
-
14
- gem install capybara-mechanize
15
-
16
- ### Usage without Cucumber
17
-
18
- require 'capybara/mechanize'
19
-
20
- ### Usage with Cucumber and tags
21
-
22
- A @mechanize tag is added to your hooks when you add the following line to your env.rb
23
-
24
- require 'capybara/mechanize/cucumber'
25
-
26
- The following scenario will then be using the Mechanize driver
27
-
28
- @mechanize
29
- Scenario: do something remote
30
- When I click the remote link
31
-
32
- ### Remote testing
33
-
34
- When you want to use this driver to test a remote application. You have to set the app_host:
35
-
36
- Capybara.app_host = "http://www.yourapp.com"
37
-
38
- Note that I haven't tested this case for my self yet. The Capybara tests pass for this situation though so it should work! Please provide me with feedback if it doesn't.
39
-
40
- ## Running tests
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
- Run bundler
47
-
48
- bundle install
49
-
50
- Run the app with the following line:
51
-
52
- bundle exec ruby -rrubygems lib/capybara/spec/extended_test_app.rb
53
-
54
- Then you are ready to run the test like so
55
-
56
- rake spec
57
-
58
- Todo
59
- ----
60
- * Make the last 12 failing remote session spec pass, see remote_mechanize_spec and uncomment one line there to see them fail
61
- * Test this driver with non-rack/non-ruby projects
62
-
63
- Note on Patches/Pull Requests
64
- -----------------------------
65
-
66
- * Fork the project.
67
- * Make your feature addition or bug fix.
68
- * Add tests for it. This is important so I don't break it in a
69
- future version unintentionally.
70
- * Commit, do not mess with rakefile, version, or history.
71
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
72
- * Send me a pull request. Bonus points for topic branches.
73
-
74
- Copyright
75
- ---------
76
- Copyright (c) 2010 Jeroen van Dijk. See LICENSE for details.