Ruby-on-rails 如何在 rspec 请求规范中设置请求标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9654465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set request headers in rspec request spec?
提问by Sergey
In the controller spec, I can set http accept header like this:
在控制器规范中,我可以像这样设置 http 接受标头:
request.accept = "application/json"
but in the request spec, "request" object is nil. So how can I do it here?
但在请求规范中,“请求”对象为零。那么我怎么能在这里做到呢?
The reason I want to set http accept header to json is so I can do this:
我想将 http 接受标头设置为 json 的原因是我可以这样做:
get '/my/path'
instead of this
而不是这个
get '/my/path.json'
回答by awaage
You should be able to specify HTTP headers as the third argument to your get() method as described here:
您应该能够将 HTTP 标头指定为 get() 方法的第三个参数,如下所述:
http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-get
http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-get
and here
和这里
http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-process
http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-process
So, you can try something like this:
所以,你可以尝试这样的事情:
get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}
回答by Sytse Sijbrandij
I used this in Test::Unit:
我在 Test::Unit 中使用了它:
@request.env['HTTP_ACCEPT'] = "*/*, application/youtube-client"
get :index
回答by morgoth
This is working for controller specs, not request specs:
这适用于控制器规格,而不是请求规格:
request.headers["My Header"] = "something"
回答by Jules Copeland
I'm adding this here, as I got majorly stuck trying to do this in Rails 5.1.rc1
我在这里添加这个,因为我在 Rails 5.1.rc1 中尝试这样做时遇到了很大的困难
The get method signature is slightly different now.
get 方法签名现在略有不同。
You need to specify the options after the path as keyword arguments, i.e.
您需要将路径后面的选项指定为关键字参数,即
get /some/path, headers: {'ACCEPT' => 'application/json'}
get /some/path, headers: {'ACCEPT' => 'application/json'}
FYI, the full set of keywords arguments are:
仅供参考,完整的关键字参数集是:
params: {}, headers: {}, env: {}, xhr: false, as: :symbol
params: {}, headers: {}, env: {}, xhr: false, as: :symbol
回答by user4694178
I have to set up headers separately
我必须单独设置标题
request.headers["Accept"] = "application/json"
Trying sending it via get/delete/.... is complete garbage in rails4 and causing pain in my head because it is never send as header but as parameter.
尝试通过 get/delete/.... 发送它在 rails4 中是完全垃圾,并导致我的头疼,因为它永远不会作为标题发送,而是作为参数发送。
{"Accept" => "application/json"}
回答by marcusb
Using rspec with Rack::Test::Methods
使用 rspec Rack::Test::Methods
header 'X_YOUR_HEADER_VAR', 'val'
get '/path'
The header var will come through as X-Your-Header-Var
标头变量将通过 X-Your-Header-Var
回答by Cyril Duchon-Doris
With RSpec 3 you can use the following syntax
使用 RSpec 3,您可以使用以下语法
get my_resource_path, params: {}, headers: { 'HTTP_ACCEPT' => "application/json" }
As described in the official Rspec documentation(the link points to v3.7)
如官方 Rspec 文档中所述(链接指向 v3.7)
回答by Jim Stewart
To send both xhr: trueand headers, I had to do e.g.:
要同时发送xhr: true和标题,我必须执行以下操作,例如:
my_headers = { "HTTP_ACCEPT": "application/json" }
get my_path, xhr: true, headers: my_headers
回答by gayavat
Try something like:
尝试类似:
get :index, :format => 'json'
回答by Igor Escobar
Your question was already answered but in case you want to POST something to another action you have to do this:
您的问题已经得到解答,但如果您想将某些内容发布到另一个操作中,您必须执行以下操作:
post :save, {format: :json, application: {param1: "test", param2: "test"}}

