setting request headers in selenium

后端 未结 9 1190
青春惊慌失措
青春惊慌失措 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条回答
  •  Happy的楠姐
    2020-11-29 22:51

    Had the same issue today, except that I needed to set different referer per test. I ended up using a middleware and a class to pass headers to it. Thought I'd share (or maybe there's a cleaner solution?):

    lib/request_headers.rb:
    
    class CustomHeadersHelper
      cattr_accessor :headers
    end
    
    class RequestHeaders
      def initialize(app, helper = nil)
        @app, @helper = app, helper
      end
    
      def call(env)
        if @helper
          headers = @helper.headers
    
          if headers.is_a?(Hash)
            headers.each do |k,v|
              env["HTTP_#{k.upcase.gsub("-", "_")}"] = v
            end
          end
        end
    
        @app.call(env)
      end
    end
    

    config/initializers/middleware.rb
    
    require 'request_headers'
    
    if %w(test cucumber).include?(Rails.env)
      Rails.application.config.middleware.insert_before Rack::Lock, "RequestHeaders", CustomHeadersHelper
    end
    

    spec/support/capybara_headers.rb
    
    require 'request_headers'
    
    module CapybaraHeaderHelpers
      shared_context "navigating within the site" do
        before(:each) { add_headers("Referer" => Capybara.app_host + "/") }
      end
    
      def add_headers(custom_headers)
        if Capybara.current_driver == :rack_test
          custom_headers.each do |name, value|
            page.driver.browser.header(name, value)
          end
        else
          CustomHeadersHelper.headers = custom_headers
        end
      end
    end
    

    spec/spec_helper.rb
    
    ...
    config.include CapybaraHeaderHelpers
    

    Then I can include the shared context wherever I need, or pass different headers in another before block. I haven't tested it with anything other than Selenium and RackTest, but it should be transparent, as header injection is done before the request actually hits the application.

提交回复
热议问题