我目前正在尝试使用Rspec为我的应用程序测试登录和注销JSON端点。我正在使用devise和devise_token_auth宝石,以便为我的身份验证构建JSON端点。在Ruby on Rails中为Rspec和Rack :: Test设置请求标头
我可以成功登录一个用户,但是当注销时需要有多个请求头部出现在注销功能中才能找到正确的用户并完成。
我试着将标题添加到我当前的机架会话中,但似乎在请求创建时删除它们。这里是我迄今为止代码:
Helper方法(spec/support/api_helper.rb
):
def login_user
user = create(:user)
post '/api/v1/auth/sign_in', email: user.email, password: user.password, format: :json
return {
'token-type' => 'Bearer',
'uid' => last_response.headers['uid'],
'access-token' => last_response.headers['access-token'],
'client' => last_response.headers['client'],
'expiry' => last_response.headers['expiry']
}
end
我Rspec的例子(spec/api/v1/authentication_spec.rb
):
describe 'DELETE /api/v1/auth/sign_out' do
it 'should destroy your current session and log you out' do
login_user
delete '/api/v1/auth/sign_out', {}, login_user
expect(last_response.status).to eq 200
expect(parse_json(last_response.body['success'])).to eq true
end
end
试图请求用户注销时,输出用DELETE HTTP动词:
=> #<Rack::MockResponse:0x007fc0f66fa748 @original_headers={"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"}, @errors="", @body_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", "Content-Length"=>"37"}, @chunked=false, @writer=#<Proc:[email protected]/Users/tomdallimore/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rack-1.6.0/lib/rack/response.rb:30 (lambda)>, @block=nil, @length=37, @body=["{\"errors\":[\"Authorized users only.\"]}"]>
我也尝试添加标题到当前的机架会象下面这样:
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']
有谁知道为什么头正在从机架会话中放弃当一个新的请求时?我还能如何将标题添加到Rack会话?