setting request headers in selenium

后端 未结 9 1183
青春惊慌失措
青春惊慌失措 2020-11-29 21:53

I\'m attempting to set the request header \'Referer\' to spoof a request coming from another site. We need the ability test that a specific referrer is used, which returns a

9条回答
  •  离开以前
    2020-11-29 22:37

    I wanted something a bit slimmer for RSpec/Ruby so that the custom code only had to live in one place. Here's my solution:

    /spec/support/selenium.rb
    ...
    RSpec.configure do |config|
      config.after(:suite) do
        $custom_headers = nil
      end
    end
    
    module RequestWithExtraHeaders
      def headers
        $custom_headers.each do |key, value|
          self.set_header "HTTP_#{key}", value
        end if $custom_headers
    
        super
      end
    end
    class ActionDispatch::Request
      prepend RequestWithExtraHeaders
    end
    

    Then in my specs:

    /specs/features/something_spec.rb
    ...
    $custom_headers = {"Referer" => referer_string}
    

提交回复
热议问题