Is it possible to specify a user agent in a rails integration test or spec?

I was doing this before in a rails 2 app in a ActionController::IntegrationTest with

get '/', {}, {:user_agent => "Googlebot"}

but this seems to not work anymore in Rails 3.

What should I do?

If you use request.user_agent in your application, you can write the following code:

get '/', {}, { "HTTP_USER_AGENT" => "Googlebot" }

How to set the user agent in tests, In Rack::Test , you can set your user agent like this on Capybara: In controller specs, you can just modify the Rack environment before making your request: Learn to structure large Ruby on Rails codebases with the tools you already know Since you want your integration tests to behave consistently, you want to set a  11 Rails - execution sequence of after create callback & nested attributes Jan 2 '13 10 Rails generate wrong pluralize form Dec 31 '12 7 Is it possible to specify a user agent in a rails integration test or spec?

None of the above answers worked for me, the following is what finally worked in an rspec controller test:

@request.user_agent = "a MobileDevice/User-Agent"
post :endpoint, param: 2354

Rails 5 / Rspec 3.5.0 -- setting request headers? · Issue #1655 , Hi, I had a spec working under Rails 4 / Rspec 3.4.4 that looked like this: it 'does The specs for this action try to set the referrer in several ways; only the last leads us to ActionDispatch::IntegrationTest::Behavior as seen here: 'client', client # header 'uid', user.email # request.headers['Access-Token']  Some classes are well-isolated from Rails and you should be able to test them without the overhead added by the Rails environment and Bundler’s :default group’s gem loading. In these cases, you can require 'fast_spec_helper' instead of require 'spec_helper' in your test file, and your test should run really fast since: Gems loading is skipped

I fixed this behavior and with Rails 4.0 you will be able to specify actual HTTP Headers like "User-Agent" and "Content-Type" in integration and functional tests. There no longer a need to specify them as CGI variables.

If you are interested you can have a look at the change: https://github.com/rails/rails/pull/9700

Testing - Today I Learned, If you are using Selenium with Chrome in a Ruby integration test, you can pass the option considers the test user more and more risky each time. cover allowed me to test this attribute, and the underlying For rspec tests add the following into spec/rails_helper.rb : Set custom user agent on #Capybara. The User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.

If you have a collection of specs which all require a specific user agent, you may find the following helps to DRY up your specs:

Define this somewhere (e.g. spec_helper.rb):

module DefaultUserAgent

  def post(uri, params = {}, session = {})
    super uri, params, {'HTTP_USER_AGENT' => MY_USER_AGENT}.merge(session)
  end

  def get(uri, params = {}, session = {})
    super uri, params, {'HTTP_USER_AGENT' => MY_USER_AGENT}.merge(session)
  end

end

Then just include DefaultUserAgent when you need it.

ActionDispatch::IntegrationTest, An integration test spans multiple controllers and actions, tying them all together to require "test_helper" class ExampleTest < ActionDispatch::IntegrationTest ActionDispatch::IntegrationTest test "login and browse site" do # User david logs​  However, it does require a User-Agent header. Headers can be created on the test configuration page right under Variables (the Request headers view). In the example above, the test is in a failure state because I haven't created the User-Agent header yet. After creating the User-Agent header my test will be passing. 3.

For myself, in a controller test in rspec3, I used

request.env["HTTP_USER_AGENT"] = "Hello"

Before making the request

Testing Rails Applications, How to write unit, functional, integration, and system tests for your application. Rails tests can also simulate browser requests and thus you can test your It generates a normal Minitest::Unit test with method names prefixed with test_ . The message parameter allows you to specify a custom error message for assertion  In the db selector test we don't always have a writing handler so I updated test fixtures to return if that's nil. Lastly one test needed to be updated to use a different handler name due to it needing to write to successfully test what it needs to test. Fixes rails#37765

How to Test Rails Models with RSpec, By the end of this tutorial you will know how to test your Rails models using RSpec and How to use Continuous Integration to automate testing? to the model, our model object will be valid without specifying any attributes. Instead of going in-depth with the User model and its specs, we'll go over them  TestRail is a test case and test management software tool that helps teams manage and track their software testing efforts. Its intuitive web-based user interface makes it easy to create test cases, manage test runs and coordinate your entire testing process.

File: README, I fixed this behavior and with Rails 4.0 you will be able to specify actual HTTP Headers like "User-Agent" and "Content-Type" in integration and functional tests. Use rspec-rails 3.x for Rails earlier than 5.0. Use rspec-rails 1.x for Rails 2.x. Installation. IMPORTANT This README / branch refers to the current development build. See the 4-0-maintenance branch on Github if you want or require the latest stable release. Add rspec-rails to both the :development and :test groups of your app’s Gemfile:

Configuring Authentication and User Agent, To use Rails 2.x, you'll need to use an earlier version of the Honeybadger gem. You will need to manually set up your own middleware and hooks for error monitoring exceptions. ignored_user_agents, Array, A list of user agents to ignore. It is possible to test Honeybadger's integration with your application using the  Action Mailer BasicsThis guide provides you with all you need to get started in sending emails from and to your application, and many internals of Action Mailer. It also covers how to test your mailers.After reading this guide, you will know: How to send email within a Rails application. How to generate and edit an Action Mailer class and mailer view. How to configure Action Mailer for your

Comments
  • Awesome - do you know of any way to set this for all requests made during integration tests?
  • Can you elaborate why passing symbols doesn't work anymore? This seems to be a step back from a syntactic point of view, and an unnecessary migration hassle when porting from 2.x.
  • Also, passing the header as HTTP_* didn't work for me either. I had to pass it as CONTENT_TYPE instead of HTTP_CONTENT_TYPE. If I passed XML as the request body, only CONTENT_TYPE => application/xml would make Rails parse it into the params hash.
  • This doesn't work with Rails 3.1. The third argument to get specifies a session hash, not HTTP headers.
  • You won't be able to set remote_ip that way
  • undefined method `user_agent=' for nil:NilClass
  • That's what I'm doing. request.user_agent in my controller returns nil. If I do the same experiment against the running app, it returns the user agent.
  • did you try request.headers['User-Agent']