We use custom headers to authenticate our web apps. An http proxy service intercepts requests, confirms the authenticity of the user and then injects custom headers into the request.
In order to test the app, I need to write those headers into the request before it hits my ApplicationController methods. Right now, the current hack works for all my non-javascript tests:
# in hooks.rb
Before do
require 'capybara/driver/rack_test_driver'
module Capybara::Driver
class Authentic < RackTest
def env
super.merge('My-Custom-Header' => "foo")
end
end
end
Capybara.register_driver(:authentic) do |app|
Capybara::Driver::Authentic.new(app)
end
Capybara.default_driver = :authentic
end
#in the controller
class ApplicationController < ActionController::Base
before_filter :authenticate
def authenticate
render :file => File.join(Rails.root, "public", "403.html") unless request.env.keys.include?('foo')
end
end
any request I don't want to be authenticated, I can just use a tag to use the regular rack_test driver:
Feature: Authentication
Valid users should be authenticated.
Invalid users should be redirected to the login page.
Scenario: Authorized
Given I am on the homepage
Then I should see "Create a New Research Project"
@rack_test
Scenario: Unauthorized
Given I go to the homepage
Then I should see "You are not authorized to view this page."
when I use the selenium browser, a similar method of redefining env on the driver does not seem to be effective. I assume this is because the app is spooled up in the java thread and all the Rack stuff is already setup.
Where should I inject the custom headers for selenium to pick them up?
Thanks!