Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OneLogin::RubySaml::Utils.element_text should normalize text before returning it #446

Merged
merged 3 commits into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/onelogin/ruby-saml/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def self.original_uri_match?(destination_url, settings_url)
# that there all children other than text nodes can be ignored (e.g. comments). If nil is
# passed, nil will be returned.
def self.element_text(element)
element.texts.join if element
element.texts.map(&:value).join if element
end
end
end
Expand Down
37 changes: 37 additions & 0 deletions test/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,42 @@ class UtilsTest < Minitest::Test
assert !OneLogin::RubySaml::Utils.uri_match?(destination, settings)
end
end

describe 'element_text' do
it 'returns the element text' do
element = REXML::Document.new('<element>element text</element>').elements.first
assert_equal 'element text', OneLogin::RubySaml::Utils.element_text(element)
end

it 'returns all segments of the element text' do
element = REXML::Document.new('<element>element <!-- comment -->text</element>').elements.first
assert_equal 'element text', OneLogin::RubySaml::Utils.element_text(element)
end

it 'returns normalized element text' do
element = REXML::Document.new('<element>element &amp; text</element>').elements.first
assert_equal 'element & text', OneLogin::RubySaml::Utils.element_text(element)
end

it 'returns the CDATA element text' do
element = REXML::Document.new('<element><![CDATA[element & text]]></element>').elements.first
assert_equal 'element & text', OneLogin::RubySaml::Utils.element_text(element)
end

it 'returns the element text with newlines and additional whitespace' do
element = REXML::Document.new("<element> element \n text </element>").elements.first
assert_equal " element \n text ", OneLogin::RubySaml::Utils.element_text(element)
end

it 'returns nil when element is nil' do
assert_nil OneLogin::RubySaml::Utils.element_text(nil)
end

it 'returns empty string when element has no text' do
element = REXML::Document.new('<element></element>').elements.first
assert_equal '', OneLogin::RubySaml::Utils.element_text(element)
end

end
end
end