How to set HTTP header for all capybara functions

I am using rspec, capybara. I set the locale from the http header as below.

before_filter :set_locale def extract_locale_from_accept_language_header request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[az]{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 test test, I get the "undefined scan for NilClass" method. Capybara does not seem to set http headers.

How to set an HTTP header for all my functions or avoid it in another way?

+6
source share
2 answers

depending on your browser driver, you can set headers globally as follows:

  Capybara.current_session.driver.headers = { 'Accept-Language' => 'de' } Capybara.current_session.driver.header('Accept-Language', 'de') 
+11
source

You can customize the headers as follows:

 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 this HTTP header for subsequent requests

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

+4
source

Source: https://habr.com/ru/post/948847/


All Articles