我目前正在嘗試使用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會話?