3

我在这方面找不到任何东西。如何在我的 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
4

1 回答 1

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

于 2013-12-10T16:02:13.073 回答