Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set http header for all capybara features

I use rspec, capybara. I set locale from http header as in bellow

  before_filter :set_locale

  def extract_locale_from_accept_language_header
    request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
  end

  def set_locale
    return I18n.locale = current_user.locale if user_signed_in?
    I18n.locale = extract_locale_from_accept_language_header || I18n.default_locale
  end

When I run my feature test I get error 'undefined method scan for NilClass'. Apparently capybara don't set http headers.

How I can set http header for all my features or avoid this by another way?

like image 853
Aminov Vali Avatar asked Jul 06 '13 14:07

Aminov Vali


2 Answers

depending on your browser driver, you can set headers globally like this:

  Capybara.current_session.driver.headers = { 'Accept-Language' => 'de' }
  Capybara.current_session.driver.header('Accept-Language', 'de')
like image 116
phoet Avatar answered Nov 12 '22 04:11

phoet


You can set your headers like this:

RSpec.configure do |config|
  config.before(:each) do
    page.driver.header 'Accept-Language', 'de'
  end
end

Source: https://github.com/thoughtbot/capybara-webkit#non-standard-driver-methods

header: set the given HTTP header for subsequent requests

page.driver.header 'Referer', 'https://www.thoughtbot.com'

like image 39
abc123 Avatar answered Nov 12 '22 03:11

abc123