2009-10-07 16:31:20 -04:00
|
|
|
require 'abstract_unit'
|
2011-06-16 07:38:56 -04:00
|
|
|
require 'active_support/core_ext/string/inflections'
|
2011-01-28 18:00:52 -05:00
|
|
|
require 'yaml'
|
2009-10-07 16:31:20 -04:00
|
|
|
|
2010-02-01 01:12:01 -05:00
|
|
|
class SafeBufferTest < ActiveSupport::TestCase
|
2009-10-07 16:31:20 -04:00
|
|
|
def setup
|
2010-02-01 01:12:01 -05:00
|
|
|
@buffer = ActiveSupport::SafeBuffer.new
|
2009-10-07 16:31:20 -04:00
|
|
|
end
|
|
|
|
|
2012-01-26 12:24:14 -05:00
|
|
|
def test_titleize
|
|
|
|
assert_equal 'Foo', "foo".html_safe.titleize
|
|
|
|
end
|
|
|
|
|
2009-10-07 16:31:20 -04:00
|
|
|
test "Should look like a string" do
|
|
|
|
assert @buffer.is_a?(String)
|
|
|
|
assert_equal "", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should escape a raw string which is passed to them" do
|
|
|
|
@buffer << "<script>"
|
|
|
|
assert_equal "<script>", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should NOT escape a safe value passed to it" do
|
For performance reasons, you can no longer call html_safe! on Strings. Instead, all Strings are always not html_safe?. Instead, you can get a SafeBuffer from a String by calling #html_safe, which will SafeBuffer.new(self).
* Additionally, instead of doing concat("</form>".html_safe), you can do
safe_concat("</form>"), which will skip both the flag set, and the flag
check.
* For the first pass, I converted virtually all #html_safe!s to #html_safe,
and the tests pass. A further optimization would be to try to use
#safe_concat as much as possible, reducing the performance impact if
we know up front that a String is safe.
2010-01-31 22:17:42 -05:00
|
|
|
@buffer << "<script>".html_safe
|
2009-10-07 16:31:20 -04:00
|
|
|
assert_equal "<script>", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should not mess with an innocuous string" do
|
|
|
|
@buffer << "Hello"
|
|
|
|
assert_equal "Hello", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should not mess with a previously escape test" do
|
2009-10-10 21:29:31 -04:00
|
|
|
@buffer << ERB::Util.html_escape("<script>")
|
2009-10-07 16:31:20 -04:00
|
|
|
assert_equal "<script>", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should be considered safe" do
|
|
|
|
assert @buffer.html_safe?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should return a safe buffer when calling to_s" do
|
|
|
|
new_buffer = @buffer.to_s
|
2010-02-01 01:12:01 -05:00
|
|
|
assert_equal ActiveSupport::SafeBuffer, new_buffer.class
|
2009-10-07 16:31:20 -04:00
|
|
|
end
|
2011-01-28 18:00:52 -05:00
|
|
|
|
2011-06-16 07:38:56 -04:00
|
|
|
test "Should be converted to_yaml" do
|
2011-01-28 18:00:52 -05:00
|
|
|
str = 'hello!'
|
|
|
|
buf = ActiveSupport::SafeBuffer.new str
|
|
|
|
yaml = buf.to_yaml
|
|
|
|
|
|
|
|
assert_match(/^--- #{str}/, yaml)
|
|
|
|
assert_equal 'hello!', YAML.load(yaml)
|
|
|
|
end
|
|
|
|
|
2011-06-16 07:38:56 -04:00
|
|
|
test "Should work in nested to_yaml conversion" do
|
2011-01-28 18:00:52 -05:00
|
|
|
str = 'hello!'
|
|
|
|
data = { 'str' => ActiveSupport::SafeBuffer.new(str) }
|
|
|
|
yaml = YAML.dump data
|
|
|
|
assert_equal({'str' => str}, YAML.load(yaml))
|
|
|
|
end
|
2011-06-07 17:31:03 -04:00
|
|
|
|
2011-06-16 07:38:56 -04:00
|
|
|
test "Should work with underscore" do
|
|
|
|
str = "MyTest".html_safe.underscore
|
|
|
|
assert_equal "my_test", str
|
|
|
|
end
|
|
|
|
|
2011-09-08 14:49:08 -04:00
|
|
|
test "Should not return safe buffer from gsub" do
|
|
|
|
altered_buffer = @buffer.gsub('', 'asdf')
|
|
|
|
assert_equal 'asdf', altered_buffer
|
2011-06-07 17:31:03 -04:00
|
|
|
assert !altered_buffer.html_safe?
|
|
|
|
end
|
|
|
|
|
2011-06-16 07:38:56 -04:00
|
|
|
test "Should not return safe buffer from gsub!" do
|
2011-09-08 14:49:08 -04:00
|
|
|
@buffer.gsub!('', 'asdf')
|
|
|
|
assert_equal 'asdf', @buffer
|
|
|
|
assert !@buffer.html_safe?
|
2011-06-16 07:38:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Should escape dirty buffers on add" do
|
|
|
|
clean = "hello".html_safe
|
2011-09-08 14:49:08 -04:00
|
|
|
@buffer.gsub!('', '<>')
|
|
|
|
assert_equal "hello<>", clean + @buffer
|
2011-06-16 07:38:56 -04:00
|
|
|
end
|
|
|
|
|
2012-02-29 16:30:51 -05:00
|
|
|
test "Should concat as a normal string when safe" do
|
2011-06-16 07:38:56 -04:00
|
|
|
clean = "hello".html_safe
|
2011-09-08 14:49:08 -04:00
|
|
|
@buffer.gsub!('', '<>')
|
|
|
|
assert_equal "<>hello", @buffer + clean
|
2011-06-16 07:38:56 -04:00
|
|
|
end
|
|
|
|
|
2012-02-29 16:30:51 -05:00
|
|
|
test "Should preserve html_safe? status on copy" do
|
2011-09-08 14:49:08 -04:00
|
|
|
@buffer.gsub!('', '<>')
|
|
|
|
assert !@buffer.dup.html_safe?
|
2011-06-07 17:31:03 -04:00
|
|
|
end
|
2011-06-16 16:04:31 -04:00
|
|
|
|
2012-01-04 10:37:20 -05:00
|
|
|
test "Should return safe buffer when added with another safe buffer" do
|
|
|
|
clean = "<script>".html_safe
|
|
|
|
result_buffer = @buffer + clean
|
|
|
|
assert result_buffer.html_safe?
|
|
|
|
assert_equal "<script>", result_buffer
|
|
|
|
end
|
|
|
|
|
2012-02-29 16:30:51 -05:00
|
|
|
test "Should raise an error when safe_concat is called on unsafe buffers" do
|
2011-09-08 14:49:08 -04:00
|
|
|
@buffer.gsub!('', '<>')
|
2011-06-16 16:04:31 -04:00
|
|
|
assert_raise ActiveSupport::SafeBuffer::SafeConcatError do
|
|
|
|
@buffer.safe_concat "BUSTED"
|
|
|
|
end
|
|
|
|
end
|
2012-01-04 10:39:28 -05:00
|
|
|
|
2012-02-29 16:30:51 -05:00
|
|
|
test "Should not fail if the returned object is not a string" do
|
2011-06-23 03:29:56 -04:00
|
|
|
assert_kind_of NilClass, @buffer.slice("chipchop")
|
2011-06-17 12:17:28 -04:00
|
|
|
end
|
2011-07-29 13:06:45 -04:00
|
|
|
|
2012-02-13 03:54:58 -05:00
|
|
|
test "clone_empty returns an empty buffer" do
|
|
|
|
assert_equal '', ActiveSupport::SafeBuffer.new('foo').clone_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
test "clone_empty keeps the original dirtyness" do
|
|
|
|
assert @buffer.clone_empty.html_safe?
|
|
|
|
assert !@buffer.gsub!('', '').clone_empty.html_safe?
|
|
|
|
end
|
2012-02-29 16:30:51 -05:00
|
|
|
|
|
|
|
test "Should be safe when sliced if original value was safe" do
|
|
|
|
new_buffer = @buffer[0,0]
|
|
|
|
assert_not_nil new_buffer
|
|
|
|
assert new_buffer.html_safe?, "should be safe"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should continue unsafe on slice" do
|
|
|
|
x = 'foo'.html_safe.gsub!('f', '<script>alert("lolpwnd");</script>')
|
|
|
|
|
|
|
|
# calling gsub! makes the dirty flag true
|
|
|
|
assert !x.html_safe?, "should not be safe"
|
|
|
|
|
|
|
|
# getting a slice of it
|
|
|
|
y = x[0..-1]
|
|
|
|
|
|
|
|
# should still be unsafe
|
|
|
|
assert !y.html_safe?, "should not be safe"
|
|
|
|
end
|
2009-10-07 16:31:20 -04:00
|
|
|
end
|