Search code examples
ruby-on-railscapybaraturbolinksrspec3turbolinks-ios

Set custom user agent on rails testing


I am developing iOS app using turbolinks-ios and Rails variant.

In my iOS app, I set custom user agent iPadApp and detect that on Rails application controller is using request.user_agent.try(:index, 'iPadApp') for setting variant to tablet (because by views are like foo.html+tablet.haml).

My app is working fine and now I am trying to write feature tests for my app but can't properly set user agent. I tried this post that actually is stackoverflow but I see that it does not set request.user_agent instead (page.driver.browser.header(key, value)) set query parameter with in request.params.

In my controller test I simply use request.user_agent = 'iPadApp' to set user agent which is working fine.

How can I configure testing request so that I can use request.user_agent.try(:index, 'iPadApp')?

Thank you for any kind of help.


Solution

  • Since you're not specifying a driver Capybara should be using rack_test. With the rack_test driver you can set the user agent header, in your test code before calling visit, with

    page.driver.header('User-Agent', 'the user agent string you want')
    

    That should then make request.user_agent accessible in your application code.

    A different solution would be register a specific driver for your ipad tests

    Capybara.register_driver(:ipad_rack_test) do |app|
      Capybara::RackTest::Driver.new(app, :headers => { 'HTTP_USER_AGENT' => 'User agent string' })
    end
    

    and then specify your driver as :ipad_rack_test