mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Improve token_and_options regex and test
add a test case to test the regex for the helper method raw_params
This commit is contained in:
parent
ad778bc422
commit
b39a344426
2 changed files with 23 additions and 3 deletions
|
@ -471,7 +471,7 @@ module ActionController
|
|||
# pairs by the standardized `:`, `;`, or `\t` delimiters defined in
|
||||
# `AUTHN_PAIR_DELIMITERS`.
|
||||
def raw_params(auth)
|
||||
auth.sub(TOKEN_REGEX, '').split(/"\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
|
||||
auth.sub(TOKEN_REGEX, '').split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
|
||||
end
|
||||
|
||||
# Encodes the given token and options into an Authorization header value.
|
||||
|
|
|
@ -139,16 +139,36 @@ class HttpTokenAuthenticationTest < ActionController::TestCase
|
|||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
test "token_and_options returns correct token with nounce option" do
|
||||
token = "rcHu+HzSFw89Ypyhn/896A="
|
||||
nonce_hash = {nonce: "123abc"}
|
||||
actual = ActionController::HttpAuthentication::Token.token_and_options(sample_request(token, nonce_hash))
|
||||
expected_token = token
|
||||
expected_nonce = {"nonce" => nonce_hash[:nonce]}
|
||||
assert_equal(expected_token, actual.first)
|
||||
assert_equal(expected_nonce, actual.last)
|
||||
end
|
||||
|
||||
test "token_and_options returns nil with no value after the equal sign" do
|
||||
actual = ActionController::HttpAuthentication::Token.token_and_options(malformed_request).first
|
||||
expected = nil
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
test "raw_params returns a tuple of two key value pair strings" do
|
||||
auth = sample_request("rcHu+HzSFw89Ypyhn/896A=").authorization.to_s
|
||||
actual = ActionController::HttpAuthentication::Token.raw_params(auth)
|
||||
expected = ["token=\"rcHu+HzSFw89Ypyhn/896A=\"", "nonce=\"def\""]
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sample_request(token)
|
||||
@sample_request ||= OpenStruct.new authorization: %{Token token="#{token}", nonce="def"}
|
||||
def sample_request(token, options = {nonce: "def"})
|
||||
authorization = options.inject([%{Token token="#{token}"}]) do |arr, (k, v)|
|
||||
arr << "#{k}=\"#{v}\""
|
||||
end.join(", ")
|
||||
@sample_request ||= OpenStruct.new authorization: authorization
|
||||
end
|
||||
|
||||
def malformed_request
|
||||
|
|
Loading…
Reference in a new issue