我找不到任何相关内容。如何在RSpec请求测试中传递API密钥?
我的API密钥是在标头中发送的,所以我在网上传递它:
Header: Authorization
Value: Token token="c32a29a71ca5953180c0a60c7d68ed9e"
如何在RSpec请求规范中传递它?
谢谢!
编辑:
这是我的规格:
require 'spec_helper'
describe "sessions" do
before do
@program =FactoryGirl.create(:program)
@user = FactoryGirl.create(:user)
FactoryGirl.create(:api_key)
end
it "is authenticated with a token" do
put "/api/v1/users/#{@user.id}?user_email=#{@user.email}&auth_token=#{@user.authentication_token}", {user: {name: "New Name"}}, { 'Authorization' => "Token token='MyString'" }
response.status.should be(201)
end
it "fails without an API Token" do
put "/api/v1/users/#{@user.id}?user_email=#{@user.email}&auth_token=#{@user.authentication_token}", user: {name: "New Name"}
response.status.should be(401)
end
end
答案 0 :(得分:5)
所以我非常接近。我需要记录输出以进行实际的API调用,以查看服务器对HTTP标头所期望的确切格式。所以,问题是格式有点偏差。
describe "sessions" do
before do
@user = FactoryGirl.create(:user)
@api_key = FactoryGirl.create(:api_key)
end
it "is authenticated with a token" do
put "/api/v1/users/#{@user.id}?user_email=#{@user.email}&auth_token=#{@user.authentication_token}", {user: {name: "New Name"}}, { "HTTP_AUTHORIZATION"=>"Token token=\"#{@api_key.access_token}\"" }
response.status.should be(201)
end
end
正如您所看到的,我必须将格式从{ 'Authorization' => "Token token='MyString'" }
更改为{ "HTTP_AUTHORIZATION"=>"Token token=\"#{@api_key.access_token}\"" }
我还用'MyString'
替换了对api令牌的实际实例的更健壮的引用。 @api_key.access_token