2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/core_ext/string/inflections"
|
|
|
|
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
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "Foo", "foo".html_safe.titleize
|
2012-01-26 12:24:14 -05:00
|
|
|
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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @buffer, :html_safe?
|
2009-10-07 16:31:20 -04:00
|
|
|
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
|
2016-08-06 12:03:25 -04:00
|
|
|
str = "hello!"
|
2011-01-28 18:00:52 -05:00
|
|
|
buf = ActiveSupport::SafeBuffer.new str
|
|
|
|
yaml = buf.to_yaml
|
|
|
|
|
|
|
|
assert_match(/^--- #{str}/, yaml)
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "hello!", YAML.load(yaml)
|
2011-01-28 18:00:52 -05:00
|
|
|
end
|
|
|
|
|
2011-06-16 07:38:56 -04:00
|
|
|
test "Should work in nested to_yaml conversion" do
|
2016-08-06 12:03:25 -04:00
|
|
|
str = "hello!"
|
|
|
|
data = { "str" => ActiveSupport::SafeBuffer.new(str) }
|
2011-01-28 18:00:52 -05:00
|
|
|
yaml = YAML.dump data
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ "str" => str }, YAML.load(yaml))
|
2011-01-28 18:00:52 -05:00
|
|
|
end
|
2011-06-07 17:31:03 -04:00
|
|
|
|
2015-02-11 20:04:23 -05:00
|
|
|
test "Should work with primitive-like-strings in to_yaml conversion" do
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "true", YAML.load(ActiveSupport::SafeBuffer.new("true").to_yaml)
|
|
|
|
assert_equal "false", YAML.load(ActiveSupport::SafeBuffer.new("false").to_yaml)
|
|
|
|
assert_equal "1", YAML.load(ActiveSupport::SafeBuffer.new("1").to_yaml)
|
|
|
|
assert_equal "1.1", YAML.load(ActiveSupport::SafeBuffer.new("1.1").to_yaml)
|
2015-02-11 20:04:23 -05:00
|
|
|
end
|
|
|
|
|
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
|
2016-08-06 12:03:25 -04:00
|
|
|
altered_buffer = @buffer.gsub("", "asdf")
|
|
|
|
assert_equal "asdf", altered_buffer
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate altered_buffer, :html_safe?
|
2011-06-07 17:31:03 -04:00
|
|
|
end
|
|
|
|
|
2011-06-16 07:38:56 -04:00
|
|
|
test "Should not return safe buffer from gsub!" do
|
2016-08-06 12:03:25 -04:00
|
|
|
@buffer.gsub!("", "asdf")
|
|
|
|
assert_equal "asdf", @buffer
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @buffer, :html_safe?
|
2011-06-16 07:38:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Should escape dirty buffers on add" do
|
|
|
|
clean = "hello".html_safe
|
2016-08-06 12:03:25 -04:00
|
|
|
@buffer.gsub!("", "<>")
|
2011-09-08 14:49:08 -04:00
|
|
|
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
|
2016-08-06 12:03:25 -04:00
|
|
|
@buffer.gsub!("", "<>")
|
2011-09-08 14:49:08 -04:00
|
|
|
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
|
2016-08-06 12:03:25 -04:00
|
|
|
@buffer.gsub!("", "<>")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @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
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate result_buffer, :html_safe?
|
2012-01-04 10:37:20 -05:00
|
|
|
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
|
2016-08-06 12:03:25 -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
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "", ActiveSupport::SafeBuffer.new("foo").clone_empty
|
2012-02-13 03:54:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "clone_empty keeps the original dirtyness" do
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @buffer.clone_empty, :html_safe?
|
|
|
|
assert_not_predicate @buffer.gsub!("", "").clone_empty, :html_safe?
|
2012-02-13 03:54:58 -05:00
|
|
|
end
|
2012-02-29 16:30:51 -05:00
|
|
|
|
|
|
|
test "Should be safe when sliced if original value was safe" do
|
2016-10-28 23:05:58 -04:00
|
|
|
new_buffer = @buffer[0, 0]
|
2012-02-29 16:30:51 -05:00
|
|
|
assert_not_nil new_buffer
|
|
|
|
assert new_buffer.html_safe?, "should be safe"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should continue unsafe on slice" do
|
2016-08-06 12:03:25 -04:00
|
|
|
x = "foo".html_safe.gsub!("f", '<script>alert("lolpwnd");</script>')
|
2012-02-29 16:30:51 -05:00
|
|
|
|
|
|
|
# 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
|
2013-12-13 11:53:18 -05:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
test "Should work with interpolation (array argument)" do
|
|
|
|
x = "foo %s bar".html_safe % ["qux"]
|
|
|
|
assert_equal "foo qux bar", x
|
2013-12-13 11:53:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
test "Should work with interpolation (hash argument)" do
|
|
|
|
x = "foo %{x} bar".html_safe % { x: "qux" }
|
|
|
|
assert_equal "foo qux bar", x
|
2013-12-13 11:53:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
test "Should escape unsafe interpolated args" do
|
|
|
|
x = "foo %{x} bar".html_safe % { x: "<br/>" }
|
|
|
|
assert_equal "foo <br/> bar", x
|
2013-12-13 11:53:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
test "Should not escape safe interpolated args" do
|
|
|
|
x = "foo %{x} bar".html_safe % { x: "<br/>".html_safe }
|
|
|
|
assert_equal "foo <br/> bar", x
|
2013-12-13 11:53:18 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
test "Should interpolate to a safe string" do
|
|
|
|
x = "foo %{x} bar".html_safe % { x: "qux" }
|
|
|
|
assert x.html_safe?, "should be safe"
|
2013-12-13 11:53:18 -05:00
|
|
|
end
|
2014-12-29 08:01:34 -05:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
test "Should not affect frozen objects when accessing characters" do
|
|
|
|
x = "Hello".html_safe
|
2016-12-24 20:29:45 -05:00
|
|
|
assert_nil x[/a/, 1]
|
2014-12-29 08:01:34 -05:00
|
|
|
end
|
2009-10-07 16:31:20 -04:00
|
|
|
end
|