Forked from denisahearn/test_http_digest_rails3_patch.rb
Created
May 21, 2012 19:34
-
-
Save tvandervossen/2764167 to your computer and use it in GitHub Desktop.
Testing HTTP Digest Authentication in Rails 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add this to test/test_helper.rb | |
# Allow http digest authentication in functional tests, based on https://gist.github.com/1282275 | |
# Call authenticate_with_http_digest(user, realm, digest) or authenticate_with_http_digest(user, realm, password, false) before you perform a request | |
class ActionController::TestCase | |
require 'digest/md5' | |
def authenticate_with_http_digest(user, realm, password, password_is_ha1 = true) | |
ActionController::Base.class_eval { include ActionController::Testing } | |
@controller.instance_eval %Q( | |
alias real_process_with_new_base_test process_with_new_base_test | |
def process_with_new_base_test(request, response) | |
credentials = { | |
:uri => request.url, | |
:realm => "#{realm}", | |
:username => "#{user}", | |
:nonce => ActionController::HttpAuthentication::Digest.nonce(request.env['action_dispatch.secret_token']), | |
:opaque => ActionController::HttpAuthentication::Digest.opaque(request.env['action_dispatch.secret_token']) | |
} | |
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Digest.encode_credentials(request.request_method, credentials, "#{password}", #{password_is_ha1}) | |
real_process_with_new_base_test(request, response) | |
end | |
) | |
end | |
end |
I just started getting an error message using this code snippet in my tests. Any ideas?
Here is the error:
NameError: undefined method `process_with_new_base_test' for class `AuthController`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Change arguments to accept the digest instead of the plain-text password by default