utils_spec.rb 4.5 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. Utils.unindent(<<-MD).should == "Hello World"
  6. Hello World
  7. MD
  8. Utils.unindent(<<-MD).should == "Hello World\nThis is\nnot indented"
  9. Hello World
  10. This is
  11. not indented
  12. MD
  13. Utils.unindent(<<-MD).should == "Hello World\n This is\n indented\nthough"
  14. Hello World
  15. This is
  16. indented
  17. though
  18. MD
  19. Utils.unindent("Hello\n I am indented").should == "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. Utils.unindent(a).should == "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. Utils.interpolate_jsonpaths("hello <$.there.world> this <escape works>", payload).should == "hello WORLD this should+work"
  28. end
  29. it "optionally supports treating values that start with '$' as raw JSONPath" do
  30. Utils.interpolate_jsonpaths("$.there.world", payload).should == "$.there.world"
  31. Utils.interpolate_jsonpaths("$.there.world", payload, :leading_dollarsign_is_jsonpath => true).should == "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. Utils.recursively_interpolate_jsonpaths(struct, data).should == {
  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. Utils.value_at({ :foo => { :bar => :baz }}.to_json, "foo.bar").should == "baz"
  60. Utils.value_at({ :foo => { :bar => { :bing => 2 } }}, "foo.bar.bing").should == 2
  61. end
  62. it "returns nil when the path cannot be followed" do
  63. Utils.value_at({ :foo => { :bar => :baz }}, "foo.bing").should be_nil
  64. end
  65. it "does not eval" do
  66. lambda {
  67. Utils.value_at({ :foo => 2 }, "foo[?(@ > 1)]")
  68. }.should raise_error(RuntimeError, /Cannot use .*? eval/)
  69. end
  70. end
  71. describe "#values_at" do
  72. it "returns arrays of matching values" do
  73. Utils.values_at({ :foo => { :bar => :baz }}, "foo.bar").should == %w[baz]
  74. Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar").should == %w[baz bing]
  75. Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar").should == %w[baz bing]
  76. end
  77. it "should allow escaping" do
  78. Utils.values_at({ :foo => { :bar => "escape this!?" }}, "escape $.foo.bar").should == ["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. cleaned_json.should_not include("</script>")
  85. cleaned_json.should include('\\u003c/script\\u003e')
  86. end
  87. it "html_safes the output unless :skip_safe is passed in" do
  88. Utils.jsonify({:foo => "bar"}).should be_html_safe
  89. Utils.jsonify({:foo => "bar"}, :skip_safe => false).should be_html_safe
  90. Utils.jsonify({:foo => "bar"}, :skip_safe => true).should_not 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. cleaned_json.should_not include("</script>")
  97. cleaned_json.should include("<\\/script>")
  98. end
  99. end
  100. end