Skip to content

mocking request.env['omniauth.origin'] when request. #934

Closed

Description

@gnextAungNaing

Hi I would like to rspec testing with request.env['omniauth.origin']. I successfully mocked OmniAuth.config.mock_auth[:google_oauth2]. but I can't set the request.env['omniauth.origin'] value. The callback controller doesn't know it. How can i do it?

Configuration

  • Provider Gem: omniauth-google-oauth
  • Ruby Version: 2.4.2
  • Framework: rails

RSpec

it 'should redirect to another page' do
      valid_google_oauth2_login_setup
      get '/auth/google_oauth2/callback'
      request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:google_oauth2]
      request.env['omniauth.origin'] = 'http://www.xxxx.com'
end

Activity

  1. jeremyk3 commented on May 1, 2019

    @jeremyk3

    There's literally zero documentation on this, but this worked for me:

        OmniAuth.config.before_callback_phase do |env|
          env['omniauth.origin'] = 'https://example.com/'
        end
    
  2. qnm commented on May 8, 2019

    @qnm

    @jeremyk3 Thanks - however what you suggest affects Omniauth across all specs. Any ideas how to isolate the change to only a particular test?

  3. speckins commented on Nov 13, 2019

    @speckins

    You're setting the request.env values after you've performed the GET request. You need to set them before you invoke the request.

  4. njcameron commented on Dec 13, 2020

    @njcameron

    @jeremyk3 I had the same problem, the work around I came up with was adding in a blank block in the setup e.g.

    class OauthTest
      def setup
        # set stuff up
        # ...
        OmniAuth.config.before_callback_phase do |env|
          # intentionally left blank
        end
      end
    
      test "when origin is blank" do
        OmniAuth.config.before_callback_phase do |env|
          env['omniauth.origin'] = nil
        end
     
        # test stuff
      end
    
      test "Another thing" do
        # test other stuff
      end
    end

    Pro-tip: if you want to be 100% sure your config is not leaking you can use

      def self.test_order
        :alpha
      end

    at the top and make sure your test with the config override starts with an earlier letter in the alphabet.

  5. yshmarov commented on Sep 2, 2023

    @yshmarov

    OmniAuth.config.before_callback_phase do |env|
    env['omniauth.origin'] = 'https://example.com/'
    end

    I can confirm that this works for me. Thanks a lot! 🙇‍♂️

  6. added theissue type on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions