简体   繁体   English

如何在 Rails 5 中设置 request.referer (ActionDispatch::IntegrationTest)

[英]How set request.referer in Rails 5 (ActionDispatch::IntegrationTest)

I'm using Rails 5, default testing engine (ActionDispatch::IntegrationTest)我正在使用 Rails 5,默认测试引擎 (ActionDispatch::IntegrationTest)

How can I set the content of request.referer in test environment.如何在测试环境中设置request.referer的内容。

It work perfectly on dev environment but it is nil when I run tests.它在开发环境中完美运行,但在我运行测试时为零

I found a reply in the docs ( http://edgeguides.rubyonrails.org/testing.html#setting-headers-and-cgi-variables )我在文档中找到了回复( http://edgeguides.rubyonrails.org/testing.html#setting-headers-and-cgi-variables

You can set request.referer using the following syntax :您可以使用以下语法设置request.referer

get articles_url, headers: { "HTTP_REFERER": "http://example.com/home" }

In this case request.referer will be equal to http://example.com/home在这种情况下request.referer将等于http://example.com/home

For RSPEC 3, RAILS 5对于 RSPEC 3、导轨 5

let(:headers) { some: "header" }
#...
request.headers.merge! headers
post :endpoint, params: params

https://github.com/rspec/rspec-rails/issues/1655#issuecomment-250418438 https://github.com/rspec/rspec-rails/issues/1655#issuecomment-250418438

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM