5

我正在编写一个也是 OAuth 提供者的 API。有什么推荐的方法来编写你的 rspec 测试吗?

启用 oauth 以保护所有端点后,如何编写将通过身份验证步骤的 rspec 测试?

4

1 回答 1

6

如果您使用的是 oauth 和 oauth-plugin gems,这篇文章可能会对您有所帮助:http: //peachshake.com/2010/11/11/oauth-capybara-rspec-headers-and-a-lot-of-pain/


然而,oauth-plugin gem 会生成一些规范,其中包括帮助您模拟身份验证过程的帮助程序。

看看这个文件: https ://github.com/pelle/oauth-plugin/blob/v0.4.0.pre4/lib/generators/rspec/templates/controller_spec_helper.rb

您可以使用以下方法签署您的请求:

def sign_request_with_oauth(token=nil,options={})
  ActionController::TestRequest.use_oauth=true
  @request.configure_oauth(current_consumer,token,options)
end

def two_legged_sign_request_with_oauth(consumer=nil,options={})
  ActionController::TestRequest.use_oauth=true
  @request.configure_oauth(consumer,nil,options)
end

def add_oauth2_token_header(token,options={})
  request.env['HTTP_AUTHORIZATION'] = "OAuth #{token.token}"
end

我建议您复制此文件作为您的规范的助手,并根据您的需要进行相应修改。

于 2011-03-22T14:01:03.493 回答