Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在RubyonRails中为Rspec和Rack::Test设置请求头_Ruby On Rails_Json_Rspec_Devise_Rack - Fatal编程技术网

Ruby on rails 在RubyonRails中为Rspec和Rack::Test设置请求头

Ruby on rails 在RubyonRails中为Rspec和Rack::Test设置请求头,ruby-on-rails,json,rspec,devise,rack,Ruby On Rails,Json,Rspec,Devise,Rack,我目前正在尝试使用Rspec测试我的应用程序的登录和注销JSON端点。我使用和gems来构建用于身份验证的JSON端点 我可以成功登录一个用户,但是当注销时,需要有几个请求头,以便注销功能找到正确的用户并完成 我尝试将标题添加到当前机架会话中,但在创建请求时,它似乎会删除标题。以下是我到目前为止的代码: 助手方法(spec/support/api_Helper.rb): 我的Rspec示例(spec/api/v1/authentication\u spec.rb): 尝试使用DELETE HTT

我目前正在尝试使用Rspec测试我的应用程序的登录和注销JSON端点。我使用和gems来构建用于身份验证的JSON端点

我可以成功登录一个用户,但是当注销时,需要有几个请求头,以便注销功能找到正确的用户并完成

我尝试将标题添加到当前机架会话中,但在创建请求时,它似乎会删除标题。以下是我到目前为止的代码:

助手方法(
spec/support/api_Helper.rb
):

我的Rspec示例(
spec/api/v1/authentication\u spec.rb
):

尝试使用DELETE HTTP谓词请求用户注销时的输出:


=>#“SAMEORIGIN”、“X-XSS-Protection”=>“1;mode=block”、“X-Content-Type-Options”=>“nosniff”、“Content-Type”=>“application/json;charset=utf-8”、“Cache-Control”=>“no-Cache”、“X-Request-Id”=>“754c89bb-7a8f-4c83-b32b-dc9ed3404863”、“X-Runtime”=>“0.010023”}、@errors=“”、@body\u string=nil、@status=401、@header”X-Frame-Options=>“SAMEORIGIN”,“X-XSS-Protection”=>“1;mode=block”、“X-Content-Type-Options”=>“nosniff”、“Content-Type”=>“application/json;charset=utf-8”、“Cache-Control”=>“no-Cache”、“X-Request-Id”=>“754c89bb-7a8f-4c83-b32b-dc9ed3404863”、“X-Runtime”=>“0.010023”、“内容长度”=>“37”}、@chunked=false、@writer=#、@block=nil、@Length=37、@body==”:[\“仅限授权用户。\“]}”]>

我还尝试向当前机架会话添加标题,如下所示:

header 'uid', login_user['uid']
header 'token-type', 'Bearer'
header 'access-token', login_user['access-token']
header 'client', login_user['client']
header 'expiry', login_user['expiry']

有人知道为什么在发出新请求时会从机架会话中删除标头吗?我还可以如何向机架会话添加标题?

我认为您所做的应该可以。此外,还可以使用设置标题
标题('name','value')

无论如何,你应该检查一下

post '/api/v1/auth/sign_in', email: user.email, password: user.password, format: :json

user.password
应该是散列的,所以如果你这样发送,你就不会登录。

我已经找了几个小时的
标题('name','value')
header 'uid', login_user['uid']
header 'token-type', 'Bearer'
header 'access-token', login_user['access-token']
header 'client', login_user['client']
header 'expiry', login_user['expiry']
post '/api/v1/auth/sign_in', email: user.email, password: user.password, format: :json