Skip to content

Commit

Permalink
Add tests to json_safe_and_pretty method
Browse files Browse the repository at this point in the history
  • Loading branch information
cheremukhin23 committed Mar 31, 2017
1 parent 703e1d7 commit 5867246
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions spec/dummy/spec/helpers/react_on_rails_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,36 @@
}
end

describe "#sanitized_props_string(props)" do
let(:hash) do
{
hello: "world",
free: "of charge",
x: "</script><script>alert('foo')</script>"
}
end
let(:hash) do
{
hello: "world",
free: "of charge",
x: "</script><script>alert('foo')</script>"
}
end

let(:hash_sanitized) do
'{"hello":"world","free":"of charge","x":"\\u003c/script\\u003e\\u003cscrip'\
let(:hash_sanitized) do
'{"hello":"world","free":"of charge","x":"\\u003c/script\\u003e\\u003cscrip'\
"t\\u003ealert('foo')\\u003c/script\\u003e\"}"
end

let(:hash_unsanitized) do
"{\"hello\":\"world\",\"free\":\"of charge\",\"x\":\"</script><script>alert('foo')</script>\"}"
end

describe "#json_safe_and_pretty(hash_or_string)" do
it "converts a hash to escaped JSON" do
escaped_json = helper.json_safe_and_pretty(hash)
expect(escaped_json).to eq(hash_sanitized)
end

let(:hash_unsanitized) do
"{\"hello\":\"world\",\"free\":\"of charge\",\"x\":\"</script><script>alert('foo')</script>\"}"
it "converts a string to escaped JSON" do
escaped_json = helper.json_safe_and_pretty(hash_unsanitized)
expect(escaped_json).to eq(hash_sanitized)
end
end

describe "#sanitized_props_string(props)" do
it "converts a hash to JSON and escapes </script>" do
sanitized = helper.sanitized_props_string(hash)
expect(sanitized).to eq(hash_sanitized)
Expand Down

0 comments on commit 5867246

Please sign in to comment.