Overriding user_agent in ActionDispatch::TestRequest on initialization #11590

Closed
htanata opened this Issue Jul 25, 2013 · 2 comments

Comments

Projects
None yet
2 participants
Contributor

htanata commented Jul 25, 2013

I have a test where I need to set user_agent on ActionDispatch::TestRequest.

Currently, I have to do something like this:

test_request = ActionController::TestRequest.new
test_request.user_agent = 'Googlebot'

instead of this:

ActionController::TestRequest.new('HTTP_USER_AGENT' => 'Googlebot')

because 'HTTP_USER_AGENT' is overridden here:

module ActionDispatch
  class TestRequest < Request
    # ...

    def initialize(env = {})
      env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application
      super(default_env.merge(env))

      self.host        = 'test.host'
      self.remote_addr = '0.0.0.0'
      self.user_agent  = 'Rails Testing'
    end

    # ...

    def user_agent=(user_agent)
      @env['HTTP_USER_AGENT'] = user_agent
    end

    # ...
  end
end

Is there a reason why it's done that way? I'm thinking that the values in the parameter env should take precedence over the default values of user_agent, host, and remote_address.

I'm happy to submit a pull request if that's acceptable.

pixeltrix added a commit that referenced this issue Jul 25, 2013

Allow overriding of all headers from passed environment hash
Allow REMOTE_ADDR, HTTP_HOST and HTTP_USER_AGENT to be overridden from
the environment passed into `ActionDispatch::TestRequest.new`.

Fixes #11590

(cherry picked from commit 4db0637)

Conflicts:
	actionpack/CHANGELOG.md

@pixeltrix pixeltrix closed this in 4db0637 Jul 25, 2013

Owner

pixeltrix commented Jul 25, 2013

@htanata thanks for reporting this issue.

Contributor

htanata commented Jul 25, 2013

Thanks @pixeltrix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment