utils_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. require 'spec_helper'
  2. describe Utils do
  3. describe "#unindent" do
  4. it "unindents to the level of the greatest consistant indention" do
  5. expect(Utils.unindent(<<-MD)).to eq("Hello World")
  6. Hello World
  7. MD
  8. expect(Utils.unindent(<<-MD)).to eq("Hello World\nThis is\nnot indented")
  9. Hello World
  10. This is
  11. not indented
  12. MD
  13. expect(Utils.unindent(<<-MD)).to eq("Hello World\n This is\n indented\nthough")
  14. Hello World
  15. This is
  16. indented
  17. though
  18. MD
  19. expect(Utils.unindent("Hello\n I am indented")).to eq("Hello\n I am indented")
  20. a = " Events will have the fields you specified. Your options look like:\n\n {\n \"url\": {\n \"css\": \"#comic img\",\n \"value\": \"@src\"\n },\n \"title\": {\n \"css\": \"#comic img\",\n \"value\": \"@title\"\n }\n }\"\n"
  21. expect(Utils.unindent(a)).to eq("Events will have the fields you specified. Your options look like:\n\n {\n \"url\": {\n\"css\": \"#comic img\",\n\"value\": \"@src\"\n },\n \"title\": {\n\"css\": \"#comic img\",\n\"value\": \"@title\"\n }\n }\"")
  22. end
  23. end
  24. describe "#interpolate_jsonpaths" do
  25. let(:payload) { { :there => { :world => "WORLD" }, :works => "should work" } }
  26. it "interpolates jsonpath expressions between matching <>'s" do
  27. expect(Utils.interpolate_jsonpaths("hello <$.there.world> this <escape works>", payload)).to eq("hello WORLD this should+work")
  28. end
  29. it "optionally supports treating values that start with '$' as raw JSONPath" do
  30. expect(Utils.interpolate_jsonpaths("$.there.world", payload)).to eq("$.there.world")
  31. expect(Utils.interpolate_jsonpaths("$.there.world", payload, :leading_dollarsign_is_jsonpath => true)).to eq("WORLD")
  32. end
  33. end
  34. describe "#recursively_interpolate_jsonpaths" do
  35. it "interpolates all string values in a structure" do
  36. struct = {
  37. :int => 5,
  38. :string => "this <escape $.works>",
  39. :array => ["<works>", "now", "<$.there.world>"],
  40. :deep => {
  41. :string => "hello <there.world>",
  42. :hello => :world
  43. }
  44. }
  45. data = { :there => { :world => "WORLD" }, :works => "should work" }
  46. expect(Utils.recursively_interpolate_jsonpaths(struct, data)).to eq({
  47. :int => 5,
  48. :string => "this should+work",
  49. :array => ["should work", "now", "WORLD"],
  50. :deep => {
  51. :string => "hello WORLD",
  52. :hello => :world
  53. }
  54. })
  55. end
  56. end
  57. describe "#value_at" do
  58. it "returns the value at a JSON path" do
  59. expect(Utils.value_at({ :foo => { :bar => :baz }}.to_json, "foo.bar")).to eq("baz")
  60. expect(Utils.value_at({ :foo => { :bar => { :bing => 2 } }}, "foo.bar.bing")).to eq(2)
  61. end
  62. it "returns nil when the path cannot be followed" do
  63. expect(Utils.value_at({ :foo => { :bar => :baz }}, "foo.bing")).to be_nil
  64. end
  65. it "does not eval" do
  66. expect {
  67. Utils.value_at({ :foo => 2 }, "foo[?(@ > 1)]")
  68. }.to raise_error(RuntimeError, /Cannot use .*? eval/)
  69. end
  70. end
  71. describe "#values_at" do
  72. it "returns arrays of matching values" do
  73. expect(Utils.values_at({ :foo => { :bar => :baz }}, "foo.bar")).to eq(%w[baz])
  74. expect(Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar")).to eq(%w[baz bing])
  75. expect(Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar")).to eq(%w[baz bing])
  76. end
  77. it "should allow escaping" do
  78. expect(Utils.values_at({ :foo => { :bar => "escape this!?" }}, "escape $.foo.bar")).to eq(["escape+this%21%3F"])
  79. end
  80. end
  81. describe "#jsonify" do
  82. it "escapes </script> tags in the output JSON" do
  83. cleaned_json = Utils.jsonify(:foo => "bar", :xss => "</script><script>alert('oh no!')</script>")
  84. expect(cleaned_json).not_to include("</script>")
  85. expect(cleaned_json).to include('\\u003c/script\\u003e')
  86. end
  87. it "html_safes the output unless :skip_safe is passed in" do
  88. expect(Utils.jsonify({:foo => "bar"})).to be_html_safe
  89. expect(Utils.jsonify({:foo => "bar"}, :skip_safe => false)).to be_html_safe
  90. expect(Utils.jsonify({:foo => "bar"}, :skip_safe => true)).not_to be_html_safe
  91. end
  92. end
  93. describe "#pretty_jsonify" do
  94. it "escapes </script> tags in the output JSON" do
  95. cleaned_json = Utils.pretty_jsonify(:foo => "bar", :xss => "</script><script>alert('oh no!')</script>")
  96. expect(cleaned_json).not_to include("</script>")
  97. expect(cleaned_json).to include("<\\/script>")
  98. end
  99. end
  100. end