Nenhuma Descrição http://j1x-huginn.herokuapp.com

utils_spec.rb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 \"attr\": \"src\"\n },\n \"title\": {\n \"css\": \"#comic img\",\n \"attr\": \"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\"attr\": \"src\"\n },\n \"title\": {\n\"css\": \"#comic img\",\n\"attr\": \"title\"\n }\n }\""
  22. end
  23. end
  24. describe "#interpolate_jsonpaths" do
  25. it "interpolates jsonpath expressions between matching <>'s" do
  26. Utils.interpolate_jsonpaths("hello <$.there.world> this <escape works>", { :there => { :world => "WORLD" }, :works => "should work" }).should == "hello WORLD this should+work"
  27. end
  28. end
  29. describe "#recursively_interpolate_jsonpaths" do
  30. it "interpolates all string values in a structure" do
  31. struct = {
  32. :int => 5,
  33. :string => "this <escape $.works>",
  34. :array => ["<works>", "now", "<$.there.world>"],
  35. :deep => {
  36. :string => "hello <there.world>",
  37. :hello => :world
  38. }
  39. }
  40. data = { :there => { :world => "WORLD" }, :works => "should work" }
  41. Utils.recursively_interpolate_jsonpaths(struct, data).should == {
  42. :int => 5,
  43. :string => "this should+work",
  44. :array => ["should work", "now", "WORLD"],
  45. :deep => {
  46. :string => "hello WORLD",
  47. :hello => :world
  48. }
  49. }
  50. end
  51. end
  52. describe "#value_at" do
  53. it "returns the value at a JSON path" do
  54. Utils.value_at({ :foo => { :bar => :baz }}.to_json, "foo.bar").should == "baz"
  55. Utils.value_at({ :foo => { :bar => { :bing => 2 } }}, "foo.bar.bing").should == 2
  56. end
  57. it "returns nil when the path cannot be followed" do
  58. Utils.value_at({ :foo => { :bar => :baz }}, "foo.bing").should be_nil
  59. end
  60. it "does not eval" do
  61. lambda {
  62. Utils.value_at({ :foo => 2 }, "foo[?(@ > 1)]")
  63. }.should raise_error(RuntimeError, /Cannot use .*? eval/)
  64. end
  65. end
  66. describe "#values_at" do
  67. it "returns arrays of matching values" do
  68. Utils.values_at({ :foo => { :bar => :baz }}, "foo.bar").should == %w[baz]
  69. Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar").should == %w[baz bing]
  70. Utils.values_at({ :foo => [ { :bar => :baz }, { :bar => :bing } ]}, "foo[*].bar").should == %w[baz bing]
  71. end
  72. it "should allow escaping" do
  73. Utils.values_at({ :foo => { :bar => "escape this!?" }}, "escape $.foo.bar").should == ["escape+this%21%3F"]
  74. end
  75. end
  76. describe "#jsonify" do
  77. it "escapes </script> tags in the output JSON" do
  78. cleaned_json = Utils.jsonify(:foo => "bar", :xss => "</script><script>alert('oh no!')</script>")
  79. cleaned_json.should_not include("</script>")
  80. cleaned_json.should include("<\\/script>")
  81. end
  82. it "html_safes the output unless :skip_safe is passed in" do
  83. Utils.jsonify({:foo => "bar"}).should be_html_safe
  84. Utils.jsonify({:foo => "bar"}, :skip_safe => false).should be_html_safe
  85. Utils.jsonify({:foo => "bar"}, :skip_safe => true).should_not be_html_safe
  86. end
  87. end
  88. describe "#pretty_jsonify" do
  89. it "escapes </script> tags in the output JSON" do
  90. cleaned_json = Utils.pretty_jsonify(:foo => "bar", :xss => "</script><script>alert('oh no!')</script>")
  91. cleaned_json.should_not include("</script>")
  92. cleaned_json.should include("<\\/script>")
  93. end
  94. end
  95. end