Skip to content

Commit

Permalink
Remove whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Van Dyk committed May 21, 2011
1 parent 7ff1b61 commit 681b316
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -6,7 +6,7 @@ group :development do
gem 'rspec', '~> 2.0.1'
gem "sinatra", "1.0.0"
gem "mongrel", '~> 1.2.pre'

gem "ruby-debug", :platforms => :ruby_18
gem "ruby-debug19", :platforms => :ruby_19
end
12 changes: 6 additions & 6 deletions README.mdown
@@ -1,7 +1,7 @@
Capybara-mechanize
==================

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.
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.

It is currently in use to test the integration between a Rails application and Twitter authorization and sharing.

Expand All @@ -28,26 +28,26 @@ The following scenario will then be using the Mechanize driver
@mechanize
Scenario: do something remote
When I click the remote link

### Remote testing

When you want to use this driver to test a remote application. You have to set the app_host:

Capybara.app_host = "http://www.yourapp.com"

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.

## Running tests

Until this library is merged with capybara there needs to be local app and you need to add the following to your host file:

127.0.0.1 capybara-testapp.heroku.com
127.0.0.1 capybara-testapp.heroku.com

Run bundler

bundle install

Run the app with the following line:
Run the app with the following line:

bundle exec ruby -rrubygems lib/capybara/spec/extended_test_app.rb

Expand All @@ -62,7 +62,7 @@ Todo

Note on Patches/Pull Requests
-----------------------------

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
Expand Down
56 changes: 28 additions & 28 deletions lib/capybara/driver/mechanize_driver.rb
Expand Up @@ -2,52 +2,52 @@

class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
extend Forwardable

def_delegator :agent, :scheme_handlers
def_delegator :agent, :scheme_handlers=

def initialize(app = nil)
@agent = ::Mechanize.new
@agent.redirect_ok = false

if app
# Delegate the RackApp to the RackTest driver
super(app)
elsif !Capybara.app_host
raise ArgumentError, "You have to set at least Capybara.app_host or Capybara.app"
end
end

def reset!
@agent.cookie_jar.clear!
@last_remote_host = nil
@last_request_remote = nil
super
end

def current_url
last_request_remote? ? remote_response.current_url : super
end

def response
last_request_remote? ? remote_response : super
end

# TODO see how this can be cleaned up
def follow_redirect!
unless response.redirect?
raise "Last response was not a redirect. Cannot follow_redirect!"
end

location = if last_request_remote?
remote_response.page.response['Location']
remote_response.page.response['Location']
else
response['Location']
end

get(location)
end

def get(url, params = {}, headers = {})
if remote?(url)
process_remote_request(:get, url, params)
Expand Down Expand Up @@ -83,7 +83,7 @@ def post_data(params)
end
end
end

def put(url, params = {}, headers = {})
if remote?(url)
process_remote_request(:put, url)
Expand All @@ -103,13 +103,13 @@ def delete(url, params = {}, headers = {})
end

def remote?(url)
if !Capybara.app_host.nil?
if !Capybara.app_host.nil?
true
elsif Capybara.default_host.nil?
false
else
host = URI.parse(url).host

if host.nil? && last_request_remote?
true
else
Expand All @@ -121,11 +121,11 @@ def remote?(url)
attr_reader :agent

private

def last_request_remote?
!!@last_request_remote
end

def register_local_request
@last_remote_host = nil
@last_request_remote = false
Expand All @@ -142,48 +142,48 @@ def process_remote_request(method, url, *options)
else
@last_remote_host = "#{remote_uri.host}:#{remote_uri.port}"
end

reset_cache
@agent.send *( [method, url] + options)

@last_request_remote = true
end
end

def remote_response
ResponseProxy.new(@agent.current_page) if @agent.current_page
end

class ResponseProxy
extend Forwardable

def_delegator :page, :body

attr_reader :page

def initialize(page)
@page = page
end

def current_url
page.uri.to_s
end

def headers
# Hax the content-type contains utf8, so Capybara specs are failing, need to ask mailinglist
# Hax the content-type contains utf8, so Capybara specs are failing, need to ask mailinglist
headers = page.response
headers["content-type"].gsub!(';charset=utf-8', '') if headers["content-type"]
headers
end

def status
page.code.to_i
end
end

def redirect?
[301, 302].include?(status)
end
end

end

end
10 changes: 5 additions & 5 deletions lib/capybara/spec/extended_test_app.rb
Expand Up @@ -2,25 +2,25 @@

class ExtendedTestApp < TestApp#< Sinatra::Base
set :environment, :production # so we don't get debug info that makes our test pass!

get %r{/redirect_to/(.*)} do
redirect params[:captures]
end

get '/host' do
"current host is #{request.host}:#{request.port}, method get"
end

get '/form_with_relative_action_to_host' do
%{<form action="/host" method="post">
<input type="submit" value="submit" />
</form>}
end

get '/relative_link_to_host' do
%{<a href="/host">host</a>}
end

post '/host' do
"current host is #{request.host}:#{request.port}, method post"
end
Expand Down
22 changes: 11 additions & 11 deletions spec/driver/mechanize_driver_spec.rb
Expand Up @@ -4,13 +4,13 @@
before do
@driver = Capybara::Driver::Mechanize.new(ExtendedTestApp)
end

it "should throw an error when no rack app is given without an app host" do
running do
Capybara::Driver::Mechanize.new
end.should raise_error(ArgumentError, "You have to set at least Capybara.app_host or Capybara.app")
end

it_should_behave_like "driver"
it_should_behave_like "driver with header support"
it_should_behave_like "driver with status code support"
Expand All @@ -20,13 +20,13 @@
it "should default to local mode" do
@driver.remote?('http://www.local.com').should be false
end

context "with an app_host" do

before do
Capybara.app_host = 'remote.com'
end

after do
Capybara.app_host = nil
end
Expand All @@ -35,20 +35,20 @@
@driver.should be_remote('http://www.remote.com')
end
end

context "with a default url, no app host" do
before :each do
Capybara.default_host = 'www.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')
Expand Down Expand Up @@ -89,7 +89,7 @@
@driver.visit("http://www.local.com/redirect_to/#{REMOTE_TEST_URL}/host")
should_be_a_remote_get
end

after :each do
Capybara.default_host = nil
end
Expand Down Expand Up @@ -122,7 +122,7 @@
def should_be_a_remote_get
@driver.body.should == "current host is #{REMOTE_TEST_HOST}, method get"
end

def should_be_a_local_get
@driver.body.should == "current host is www.local.com:80, method get"
end
Expand Down
8 changes: 4 additions & 4 deletions spec/driver/remote_mechanize_driver_spec.rb
Expand Up @@ -4,22 +4,22 @@
before(:each) do
Capybara.app_host = REMOTE_TEST_URL
end

after(:each) do
Capybara.app_host = nil
end

before do
@driver = Capybara::Driver::Mechanize.new
end

context "in remote mode" do
it "should not throw an error when no rack app is given" do
running do
Capybara::Driver::Mechanize.new
end.should_not raise_error(ArgumentError)
end

it "should pass arguments through to a get request" do
@driver.visit("#{REMOTE_TEST_URL}/form/get", {:form => "success"})
@driver.body.should == %{<pre id="results">--- success\n</pre>}
Expand All @@ -45,5 +45,5 @@
it_should_behave_like "driver with cookies support"
it_should_behave_like "driver with infinite redirect detection"
end

end
6 changes: 3 additions & 3 deletions spec/session/mechanize_spec.rb
Expand Up @@ -26,17 +26,17 @@
@session.body.should == 'The requested object was deleted'
end
end

it "should use the last remote url when following relative links" do
@session.visit("#{REMOTE_TEST_URL}/relative_link_to_host")
@session.click_link "host"
@session.body.should == "current host is #{REMOTE_TEST_HOST}, method get"
end

it "should use the last remote url when submitting a form with a relative action" do
@session.visit("#{REMOTE_TEST_URL}/form_with_relative_action_to_host")
@session.click_button "submit"
@session.body.should == "current host is #{REMOTE_TEST_HOST}, method post"
@session.body.should == "current host is #{REMOTE_TEST_HOST}, method post"
end

it_should_behave_like "session"
Expand Down
6 changes: 3 additions & 3 deletions spec/session/remote_mechanize_spec.rb
Expand Up @@ -9,9 +9,9 @@
after(:each) do
Capybara.app_host = nil
end
before do


before do
@session = Capybara::Session.new(:mechanize)
end

Expand Down

0 comments on commit 681b316

Please sign in to comment.