Von dem, was ich von dem docs verstehen kann, http_basic_authenticate_with
wirkt als vor Filter, der einen Namen und ein Passwort akzeptiert wie
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index
Während authenticate_or_request_with_http_basic einen Block ermöglicht nimmt für Sie einen Code einzufügen, um zu bestimmen, ob sie sollte authentifiziert werden (documentation). Z.B.
before_filter :authenticate
def authenticate
authenticate_or_request_with_http_basic('Administration') do |username, password|
ActiveSupport::SecurityUtils.secure_compare(username, "admin") &&
ActiveSupport::SecurityUtils.secure_compare(password, "password")
end
end
(Vorsicht, dieses Beispiel nicht sicher sein kann, z. B. zur Zeit ist es unsicher, weil es secure_compare
statt variable_size_secure_compare
verwendet. Siehe source code von http_basic_authenticate_with
in ActionController::HttpAuthentication
der aktuellen Version von Rails für eine sicherere Beispiel.)
Um dies mit Capybara zu testen, siehe http://stackoverflow.com/a/7938935/664833 – user664833
Und ** um auf der Controller-Ebene ** zu testen, verwenden Sie '@ request.env ['HTTP_AUTHORIZATION'] = 'Basic' + Base64 :: encode64 ('Benutzername: Passwort') 'dann' get: your_action'. Ref: http://apidock.com/rails/ActionController/HttpAuthentication/Basic/ControllerMethods/authenticate_or_request_with_http_basic#197-Testing-protected-controllers – user664833
'http_basic_authenticate_with' ruft intern' authenticate_or_request_with_http_basic' auf. Siehe [Quelle] (https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/http_authentication.rb#L69). – mlovic