Added :headers option to rack_test driver, closes #328

This commit is contained in:
Jonas Nicklas 2011-04-25 11:35:57 +02:00
parent 0befc0ee41
commit a00435d630
5 changed files with 59 additions and 6 deletions

View File

@ -94,7 +94,7 @@ You can now use it in your examples:
before :each do
User.make(:email => 'user@example.com', :password => 'caplin')
end
it "signs me in" do
within("#session") do
fill_in 'Login', :with => 'user@example.com'
@ -208,6 +208,27 @@ After/teardown blocks):
Note that switching the driver creates a new session, so you may not be able to
switch in the middle of a test.
=== RackTest
RackTest is Capybara's default driver. It is written in pure Ruby and does not
have any support for executing JavaScript. Since the RackTest driver works
directly agains the Rack interface, it does not need any server to be started,
it can work directly work against any Rack app. This means that if your
application is not a Rack application (Rails, Sinatra and most other Ruby
frameworks are Rack applications) then you cannot use this driver. You cannot
use the RackTest driver to test a remote application.
{capybara-mechanize}[https://github.com/jeroenvandijk/capybara-mechanize]
intends to provide a similar driver which works against remote servers, it is a
separate project.
RackTest can be configured with a set of headers like this:
Capybara.register_driver :rack_test do |app|
Capybara::RackTest::Driver.new(app, :browser => :chrome)
end
See the section on adding and configuring drivers.
=== Selenium
At the moment, Capybara supports {Selenium 2.0

View File

@ -1,11 +1,12 @@
class Capybara::RackTest::Browser
include ::Rack::Test::Methods
attr_reader :app
attr_reader :app, :options
attr_accessor :current_host
def initialize(app)
def initialize(app, options={})
@app = app
@options = options
end
def visit(path, attributes = {})
@ -107,6 +108,7 @@ protected
rescue Rack::Test::Error
# no request yet
end
env.merge!(options[:headers]) if options[:headers]
env
end

View File

@ -5,15 +5,16 @@ require 'nokogiri'
require 'cgi'
class Capybara::RackTest::Driver < Capybara::Driver::Base
attr_reader :app
attr_reader :app, :options
def initialize(app)
def initialize(app, options={})
raise ArgumentError, "rack-test requires a rack application, but none was given" unless app
@app = app
@options = options
end
def browser
@browser ||= Capybara::RackTest::Browser.new(app)
@browser ||= Capybara::RackTest::Browser.new(app, options)
end
def response

View File

@ -0,0 +1,7 @@
<p>
<a href="/get_header">Link</a>
</p>
<form action="/get_header" method="get">
<p><input type="submit" value="Post"/></p>
</form>

View File

@ -81,4 +81,26 @@ describe Capybara::RackTest::Driver do
lambda { @driver.request }.should raise_error
end
end
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.body.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.body.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.body.should include('foobar')
end
end
end