Skip to content

Commit

Permalink
Merge pull request #446 from Umofomia/normalize-text
Browse files Browse the repository at this point in the history
OneLogin::RubySaml::Utils.element_text should normalize text before returning it
  • Loading branch information
pitbulk authored Feb 28, 2018
2 parents 0a7d012 + 22efc12 commit 3d31c13
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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

0 comments on commit 3d31c13

Please sign in to comment.