在Merb中测试基本的HTTP身份验证请求

时间:2009-01-21 22:44:24

标签: ruby merb merb-auth

The Merb Open Source Book有一个chapter on authentication。但是,testing an authenticated request section示例仅显示您可以对基于表单的身份验证执行的操作。我有一个Web服务,我想用HTTP基本身份验证进行测试。我该怎么做?

2 个答案:

答案 0 :(得分:0)

在发布我的问题之后,我尝试了更多的东西并找到了我自己的答案。您可以执行以下操作:

response = request('/widgets/2222', 
                   :method => "GET", 
                   "X_HTTP_AUTHORIZATION" => 'Basic ' + ["myusername:mypassword"].pack('m').delete("\r\n"))

我可能会更新这本书,但至少此信息可供Google查找并可能帮助其他人。

答案 1 :(得分:0)

以下是控制器内部的HTTP基本身份验证示例:

class MyMerbApp < Application
  before :authenticate, :only=>[:admin]

  def index
    render
  end

  def admin
    render
  end

  protected 

  def authenticate 
    basic_authentication("Protected Area") do |username, password| 
      username == "name" && password == "secret"  
    end 
  end

end

如果还没有为你完成,你需要在 config / router.rb 中定义merb_auth_slice:

Merb::Router.prepare do
  slice(:merb_auth_slice_password, :name_prefix => nil, :path_prefix => "")
end
相关问题