Skip to content

Commit

Permalink
Uses the same content type as request on http authenticatable 401 res…
Browse files Browse the repository at this point in the history
…ponses
  • Loading branch information
josevalim committed Feb 17, 2010
1 parent 1282fc0 commit 968ebe1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rdoc
Expand Up @@ -9,6 +9,14 @@
* Rails 3 compatible only.
* Scoped views are no longer "sessions/users/new". Now use "users/sessions/new".

== 1.0.2

* enhancements
* Allows you set mailer content type (by github.com/glennr)

* bug fix
* Uses the same content type as request on http authenticatable 401 responses

== 1.0.1

* enhancements
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/strategies/http_authenticatable.rb
Expand Up @@ -30,7 +30,7 @@ def decode_credentials(request)

def custom_headers
{
"Content-Type" => "text/plain",
"Content-Type" => request.format.to_s,
"WWW-Authenticate" => %(Basic realm="#{Devise.http_authentication_realm.gsub(/"/, "")}")
}
end
Expand Down
10 changes: 8 additions & 2 deletions test/integration/http_authenticatable_test.rb
Expand Up @@ -16,6 +16,12 @@ class HttpAuthenticationTest < ActionController::IntegrationTest
assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
end

test 'uses the request format as response content type' do
sign_in_as_new_user_with_http("unknown", "123456", :xml)
assert_equal 401, status
assert_equal "application/xml", headers["Content-Type"]
end

test 'returns a custom response with www-authenticate and chosen realm' do
swap Devise, :http_authentication_realm => "MyApp" do
sign_in_as_new_user_with_http("unknown")
Expand All @@ -36,9 +42,9 @@ class HttpAuthenticationTest < ActionController::IntegrationTest

private

def sign_in_as_new_user_with_http(username="user@test.com", password="123456")
def sign_in_as_new_user_with_http(username="user@test.com", password="123456", format=:html)
user = create_user
get users_path, {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
get users_path(:format => format), {}, "HTTP_AUTHORIZATION" => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
user
end
end

0 comments on commit 968ebe1

Please sign in to comment.