簡體   English   中英

如何在黃瓜+水豚步驟定義中設置HTTP_REFERER?

[英]How to set HTTP_REFERER in cucumber+capybara step definitions?

如何使用capybara為黃瓜步驟定義的請求設置HTTP_REFERER請求標頭?

谷歌搜索,我嘗試了以下,沒有一個工作:

header 'Referer', 'http://example.com/'
ENV['HTTP_REFERER'] = 'http://example.com/'
get '/', {}, { 'HTTP_REFERER' => 'http://example.com/' }
get '/', nil, { 'HTTP_REFERER' => 'http://example.com/' }
request.env['HTTP_REFERER'] = 'http://example.com/' # raises ArgumentError

visit '/'

我在控制器中打印request.env ['HTTP_REFERER'],以證明它沒有通過。 如果我使用Web瀏覽器測試控制器,則會正確打印引用程序。

請注意,使用get參數的3參數實際上確實將HTTP_REFERER放入控制器觀察到的請求環境中,但它似乎以我無法弄清楚的方式擾亂了請求; 所有渲染的都是doctype元素。

我正在使用默認的黃瓜驅動程序,Cucumber :: RackTest :: Driver,黃瓜-1.1.3,黃瓜軌道1.2.0和capybara-1.1.2。

由於我使用的是Cucumber :: RackTest :: Driver,在撰寫本文時我的解決方案是:

Capybara.current_session.driver.header 'Referer', 'http://example.com'

這個解決方案的靈感來自以下對水豚的未合並拉動請求:

https://github.com/thoughtbot/capybara-webkit/issues/198

我不太了解Cucumber,但是這個[即調用帖子而不是訪問'some_path']在Rspec中適用於我,所以也許類似的解決方案可能適合你(鑒於它也是一個集成測試):

it 'correctly updates the last_login_ip attribute' do
  post login_path, { :email => user.email, :password => user.password }, { 'REMOTE_ADDR' => 'some_address' }
  user.reload
  user.last_login_ip.should == 'some_address'
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM