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

发布于 2024-07-12 10:08:24 字数 392 浏览 7 评论 0原文

Merb 开源书籍 有一个有关身份验证的章节。 但是,测试经过身份验证的请求部分示例仅显示了您可以执行的操作基于表单的身份验证。 我有一个 Web 服务,想要使用 HTTP 基本身份验证进行测试。 我该怎么做呢?

The Merb Open Source Book has a chapter on authentication. However, the testing an authenticated request section example only shows what you can do for forms based authentication. I have a web service that I want to test with HTTP basic authentication. How would I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-07-19 10:08:24

发布我的问题后,我又尝试了一些事情并找到了自己的答案。 您可以执行如下操作:

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

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

After posting my question, I tried a few more things and found my own answer. You can do something like the following:

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

I may get around to updating the book, but at least this info is here for Google to find and possibly help someone else.

流星番茄 2024-07-19 10:08:24

以下是控制器内部 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

Here is an example for HTTP basic auth from inside a controller:

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

you'll need to define the merb_auth_slice in config/router.rb if it's not already done for you:

Merb::Router.prepare do
  slice(:merb_auth_slice_password, :name_prefix => nil, :path_prefix => "")
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文