2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2016-08-06 12:03:25 -04:00
|
|
|
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
|
|
|
|
|
2018-09-27 20:50:21 -04:00
|
|
|
{
|
|
|
|
capitalize: nil,
|
|
|
|
chomp: nil,
|
|
|
|
chop: nil,
|
|
|
|
delete: "foo",
|
|
|
|
delete_prefix: "foo",
|
|
|
|
delete_suffix: "foo",
|
|
|
|
downcase: nil,
|
|
|
|
gsub: ["foo", "bar"],
|
|
|
|
lstrip: nil,
|
|
|
|
next: nil,
|
|
|
|
reverse: nil,
|
|
|
|
rstrip: nil,
|
2020-11-20 21:03:04 -05:00
|
|
|
scrub: nil,
|
2018-09-27 20:50:21 -04:00
|
|
|
slice: "foo",
|
|
|
|
squeeze: nil,
|
|
|
|
strip: nil,
|
|
|
|
sub: ["foo", "bar"],
|
|
|
|
succ: nil,
|
|
|
|
swapcase: nil,
|
|
|
|
tr: ["foo", "bar"],
|
|
|
|
tr_s: ["foo", "bar"],
|
|
|
|
unicode_normalize: nil,
|
|
|
|
upcase: nil,
|
|
|
|
}.each do |unsafe_method, dummy_args|
|
|
|
|
test "Should not return safe buffer from #{unsafe_method}" do
|
|
|
|
skip unless String.method_defined?(unsafe_method)
|
2020-10-02 02:15:31 -04:00
|
|
|
altered_buffer = @buffer.public_send(unsafe_method, *dummy_args)
|
2018-09-27 20:50:21 -04:00
|
|
|
assert_not_predicate altered_buffer, :html_safe?
|
|
|
|
end
|
2011-06-07 17:31:03 -04:00
|
|
|
|
2018-09-27 20:50:21 -04:00
|
|
|
test "Should not return safe buffer from #{unsafe_method}!" do
|
|
|
|
skip unless String.method_defined?("#{unsafe_method}!")
|
2020-10-02 02:15:31 -04:00
|
|
|
@buffer.public_send("#{unsafe_method}!", *dummy_args)
|
2018-09-27 20:50:21 -04:00
|
|
|
assert_not_predicate @buffer, :html_safe?
|
|
|
|
end
|
2011-06-16 07:38:56 -04:00
|
|
|
end
|
|
|
|
|
2019-03-13 18:28:43 -04:00
|
|
|
test "can assign value into zero-index" do
|
|
|
|
buffer = ActiveSupport::SafeBuffer.new("012345")
|
|
|
|
|
|
|
|
buffer[0] = "<"
|
|
|
|
|
|
|
|
assert_equal "<12345", buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "can assign value into non zero-index" do
|
|
|
|
buffer = ActiveSupport::SafeBuffer.new("012345")
|
|
|
|
|
|
|
|
buffer[2] = "<"
|
|
|
|
|
|
|
|
assert_equal "01<345", buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "can assign value into slice" do
|
|
|
|
buffer = ActiveSupport::SafeBuffer.new("012345")
|
|
|
|
|
|
|
|
buffer[0, 3] = "<"
|
|
|
|
|
|
|
|
assert_equal "<345", buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "can assign value into offset slice" do
|
|
|
|
buffer = ActiveSupport::SafeBuffer.new("012345")
|
|
|
|
|
|
|
|
buffer[1, 3] = "<"
|
|
|
|
|
|
|
|
assert_equal "0<45", buffer
|
|
|
|
end
|
|
|
|
|
2011-06-16 07:38:56 -04:00
|
|
|
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
|
|
|
|
|
2019-04-18 04:44:22 -04:00
|
|
|
test "Should preserve html_safe? status on multiplication" do
|
|
|
|
multiplied_safe_buffer = "<br />".html_safe * 2
|
|
|
|
assert_predicate multiplied_safe_buffer, :html_safe?
|
|
|
|
|
|
|
|
multiplied_unsafe_buffer = @buffer.gsub("", "<>") * 2
|
|
|
|
assert_not_predicate multiplied_unsafe_buffer, :html_safe?
|
|
|
|
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
|
|
|
|
|
2019-02-01 11:47:10 -05:00
|
|
|
test "clone_empty keeps the original dirtiness" 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
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not x.html_safe?, "should not be safe"
|
2012-02-29 16:30:51 -05:00
|
|
|
|
|
|
|
# getting a slice of it
|
|
|
|
y = x[0..-1]
|
|
|
|
|
|
|
|
# should still be unsafe
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not y.html_safe?, "should not be safe"
|
2012-02-29 16:30:51 -05:00
|
|
|
end
|
2013-12-13 11:53:18 -05:00
|
|
|
|
2018-08-31 12:46:09 -04:00
|
|
|
test "Should continue safe on slice" do
|
|
|
|
x = "<div>foo</div>".html_safe
|
|
|
|
|
2018-09-06 13:48:50 -04:00
|
|
|
assert_predicate x, :html_safe?
|
2018-08-31 12:46:09 -04:00
|
|
|
|
|
|
|
# getting a slice of it
|
|
|
|
y = x[0..-1]
|
|
|
|
|
|
|
|
# should still be safe
|
2018-09-06 13:48:50 -04:00
|
|
|
assert_predicate y, :html_safe?
|
2018-08-31 12:46:09 -04:00
|
|
|
end
|
|
|
|
|
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
|
2018-11-08 07:02:45 -05:00
|
|
|
|
|
|
|
test "Should set back references" do
|
|
|
|
a = "foo123".html_safe
|
|
|
|
a2 = a.sub(/([a-z]+)([0-9]+)/) { $2 + $1 }
|
|
|
|
assert_equal "123foo", a2
|
|
|
|
assert_not_predicate a2, :html_safe?
|
|
|
|
a.sub!(/([a-z]+)([0-9]+)/) { $2 + $1 }
|
|
|
|
assert_equal "123foo", a
|
|
|
|
assert_not_predicate a, :html_safe?
|
|
|
|
|
|
|
|
b = "foo123 bar456".html_safe
|
|
|
|
b2 = b.gsub(/([a-z]+)([0-9]+)/) { $2 + $1 }
|
|
|
|
assert_equal "123foo 456bar", b2
|
|
|
|
assert_not_predicate b2, :html_safe?
|
|
|
|
b.gsub!(/([a-z]+)([0-9]+)/) { $2 + $1 }
|
|
|
|
assert_equal "123foo 456bar", b
|
|
|
|
assert_not_predicate b, :html_safe?
|
|
|
|
end
|
2019-10-17 09:51:09 -04:00
|
|
|
|
|
|
|
test "Should support Enumerator" do
|
|
|
|
a = "aaa".html_safe.gsub!(/a/).with_index { |m, i| i }
|
|
|
|
assert_equal "012", a
|
|
|
|
end
|
2009-10-07 16:31:20 -04:00
|
|
|
end
|