Tag: basic authentication

如何在Rails 5测试控制器中测试或绕过Basic Auth

我想在Rails 5中测试激活基本身份validation的控制器。 当前官方指令 (截至2018-10-14)中解释的方式由于某种原因不起作用。 问题与解答 “ 在Rails 2.2+中测试HTTP基本身份validation ”对于Rails 5来说似乎太旧了(至少对于默认值而言)。 这是重现案例的简化示例。 我通过全新安装的Rails(最新的稳定版本5.2.1)通过脚手架制作了一个文章模型和相关资源: bin/rails g scaffold Article title:string content:text 并根据官方指南将基本的authfunction添加到控制器; 那么ArticlesController是这样的,当然有效: # /app/controllers/articles_controller.rb class ArticlesController < ApplicationController http_basic_authenticate_with name: "user1", password: "pass" before_action :set_article, only: [:show, :edit, :update, :destroy] def index @articles = Article.all end end 官方说明解释了测试基本认证的方法; 你在控制器的测试文件中的setup块中添加了request.headers[‘Authorization’] ,我做了: # /test/controllers/articles_controller_test.rb require ‘test_helper’ class ArticlesControllerTest < […]

Rails – authenticate_or_request_with_http_basic自定义“访问被拒绝”消息

在使用authenticate_or_request_with_http_basic在rails上插入无效凭据作为username:password时,我正在努力尝试更改默认消息。 例如,如果我对需要此身份validation的方法发出curl请求,则在插入错误的用户名和密码后,它将返回HTTP Basic: Access denied. 因此,在这种情况下,我希望能够使用特定的XML格式字符串自定义此消息(就像twitter API一样)。 那可能吗? 提前致谢

Ruby rest-client文件作为具有基本身份validation的多部分表单数据上载

我理解如何使用Ruby的rest-client使用基本身份validation来创建http请求 response = RestClient::Request.new(:method => :get, :url => @base_url + path, :user => @sid, :password => @token).execute 以及如何将文件作为多部分表单数据发布 RestClient.post ‘/data’, :myfile => File.new(“/path/to/image.jpg”, ‘rb’) 但我似乎无法弄清楚如何将两者结合起来将文件发布到需要基本身份validation的服务器。 有谁知道创建此请求的最佳方法是什么?