liquid_interpolatable_spec.rb 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. require 'rails_helper'
  2. require 'nokogiri'
  3. describe LiquidInterpolatable::Filters do
  4. before do
  5. @filter = Class.new do
  6. include LiquidInterpolatable::Filters
  7. end.new
  8. end
  9. describe 'uri_escape' do
  10. it 'should escape a string for use in URI' do
  11. expect(@filter.uri_escape('abc:/?=')).to eq('abc%3A%2F%3F%3D')
  12. end
  13. it 'should not raise an error when an operand is nil' do
  14. expect(@filter.uri_escape(nil)).to be_nil
  15. end
  16. end
  17. describe 'validations' do
  18. class Agents::InterpolatableAgent < Agent
  19. include LiquidInterpolatable
  20. def check
  21. create_event :payload => {}
  22. end
  23. def validate_options
  24. interpolated['foo']
  25. end
  26. end
  27. it "should finish without raising an exception" do
  28. agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{{bar}' })
  29. expect(agent.valid?).to eq(false)
  30. expect(agent.errors[:options].first).to match(/not properly terminated/)
  31. end
  32. end
  33. describe 'unescape' do
  34. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  35. it 'should unescape basic HTML entities' do
  36. agent.interpolation_context['something'] = '&#39;&lt;foo&gt; &amp; bar&#x27;'
  37. agent.options['cleaned'] = '{{ something | unescape }}'
  38. expect(agent.interpolated['cleaned']).to eq("'<foo> & bar'")
  39. end
  40. end
  41. describe "json" do
  42. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  43. it 'serializes data to json' do
  44. agent.interpolation_context['something'] = {foo: 'bar'}
  45. agent.options['cleaned'] = '{{ something | json }}'
  46. expect(agent.interpolated['cleaned']).to eq('{"foo":"bar"}')
  47. end
  48. end
  49. describe 'to_xpath' do
  50. before do
  51. def @filter.to_xpath_roundtrip(string)
  52. Nokogiri::XML('').xpath(to_xpath(string))
  53. end
  54. end
  55. it 'should escape a string for use in XPath expression' do
  56. [
  57. %q{abc}.freeze,
  58. %q{'a"bc'dfa""fds''fa}.freeze,
  59. ].each { |string|
  60. expect(@filter.to_xpath_roundtrip(string)).to eq(string)
  61. }
  62. end
  63. it 'should stringify a non-string operand' do
  64. expect(@filter.to_xpath_roundtrip(nil)).to eq('')
  65. expect(@filter.to_xpath_roundtrip(1)).to eq('1')
  66. end
  67. end
  68. describe 'to_uri' do
  69. before do
  70. @agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{% assign u = s | to_uri %}{{ u.path }}' })
  71. @agent.interpolation_context['s'] = 'http://example.com/dir/1?q=test'
  72. end
  73. it 'should parse an abosule URI' do
  74. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  75. end
  76. it 'should parse an abosule URI with a base URI specified' do
  77. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  78. end
  79. it 'should parse a relative URI with a base URI specified' do
  80. expect(@filter.to_uri('foo/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/foo/index.html'))
  81. end
  82. it 'should parse an abosule URI with a base URI specified' do
  83. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  84. end
  85. it 'should stringify a non-string operand' do
  86. expect(@filter.to_uri(123, 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/123'))
  87. end
  88. it 'should return a URI value in interpolation' do
  89. expect(@agent.interpolated['foo']).to eq('/dir/1')
  90. end
  91. it 'should return a URI value resolved against a base URI in interpolation' do
  92. @agent.options['foo'] = '{% assign u = s | to_uri:"http://example.com/dir/1" %}{{ u.path }}'
  93. @agent.interpolation_context['s'] = 'foo/index.html'
  94. expect(@agent.interpolated['foo']).to eq('/dir/foo/index.html')
  95. end
  96. end
  97. describe 'uri_expand' do
  98. before do
  99. stub_request(:head, 'https://t.co.x/aaaa').
  100. to_return(status: 301, headers: { Location: 'https://bit.ly.x/bbbb' })
  101. stub_request(:head, 'https://bit.ly.x/bbbb').
  102. to_return(status: 301, headers: { Location: 'http://tinyurl.com.x/cccc' })
  103. stub_request(:head, 'http://tinyurl.com.x/cccc').
  104. to_return(status: 301, headers: { Location: 'http://www.example.com/welcome' })
  105. stub_request(:head, 'http://www.example.com/welcome').
  106. to_return(status: 200)
  107. (1..5).each do |i|
  108. stub_request(:head, "http://2many.x/#{i}").
  109. to_return(status: 301, headers: { Location: "http://2many.x/#{i+1}" })
  110. end
  111. stub_request(:head, 'http://2many.x/6').
  112. to_return(status: 301, headers: { 'Content-Length' => '5' })
  113. end
  114. it 'should handle inaccessible URIs' do
  115. expect(@filter.uri_expand(nil)).to eq('')
  116. expect(@filter.uri_expand('')).to eq('')
  117. expect(@filter.uri_expand(5)).to eq('5')
  118. expect(@filter.uri_expand([])).to eq('[]')
  119. expect(@filter.uri_expand({})).to eq('{}')
  120. expect(@filter.uri_expand(URI('/'))).to eq('/')
  121. expect(@filter.uri_expand(URI('http:google.com'))).to eq('http:google.com')
  122. expect(@filter.uri_expand(URI('http:/google.com'))).to eq('http:/google.com')
  123. expect(@filter.uri_expand(URI('ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT'))).to eq('ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT')
  124. end
  125. it 'should follow redirects' do
  126. expect(@filter.uri_expand('https://t.co.x/aaaa')).to eq('http://www.example.com/welcome')
  127. end
  128. it 'should respect the limit for the number of redirects' do
  129. expect(@filter.uri_expand('http://2many.x/1')).to eq('http://2many.x/1')
  130. expect(@filter.uri_expand('http://2many.x/1', 6)).to eq('http://2many.x/6')
  131. end
  132. it 'should detect a redirect loop' do
  133. stub_request(:head, 'http://bad.x/aaaa').
  134. to_return(status: 301, headers: { Location: 'http://bad.x/bbbb' })
  135. stub_request(:head, 'http://bad.x/bbbb').
  136. to_return(status: 301, headers: { Location: 'http://bad.x/aaaa' })
  137. expect(@filter.uri_expand('http://bad.x/aaaa')).to eq('http://bad.x/aaaa')
  138. end
  139. it 'should be able to handle an FTP URL' do
  140. stub_request(:head, 'http://downloads.x/aaaa').
  141. to_return(status: 301, headers: { Location: 'http://downloads.x/download?file=aaaa.zip' })
  142. stub_request(:head, 'http://downloads.x/download').
  143. with(query: { file: 'aaaa.zip' }).
  144. to_return(status: 301, headers: { Location: 'ftp://downloads.x/pub/aaaa.zip' })
  145. expect(@filter.uri_expand('http://downloads.x/aaaa')).to eq('ftp://downloads.x/pub/aaaa.zip')
  146. end
  147. describe 'used in interpolation' do
  148. before do
  149. @agent = Agents::InterpolatableAgent.new(name: "test")
  150. end
  151. it 'should follow redirects' do
  152. @agent.interpolation_context['short_url'] = 'https://t.co.x/aaaa'
  153. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  154. expect(@agent.interpolated['long_url']).to eq('http://www.example.com/welcome')
  155. end
  156. it 'should respect the limit for the number of redirects' do
  157. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  158. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  159. expect(@agent.interpolated['long_url']).to eq('http://2many.x/1')
  160. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  161. @agent.options['long_url'] = '{{ short_url | uri_expand:6 }}'
  162. expect(@agent.interpolated['long_url']).to eq('http://2many.x/6')
  163. end
  164. end
  165. end
  166. describe 'regex_replace_first' do
  167. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  168. it 'should replace the first occurrence of a string using regex' do
  169. agent.interpolation_context['something'] = 'foobar foobar'
  170. agent.options['cleaned'] = '{{ something | regex_replace_first: "\S+bar", "foobaz" }}'
  171. expect(agent.interpolated['cleaned']).to eq('foobaz foobar')
  172. end
  173. it 'should support escaped characters' do
  174. agent.interpolation_context['something'] = "foo\\1\n\nfoo\\bar\n\nfoo\\baz"
  175. agent.options['test'] = "{{ something | regex_replace_first: '\\\\(\\w{2,})', '\\1\\\\' | regex_replace_first: '\\n+', '\\n' }}"
  176. expect(agent.interpolated['test']).to eq("foo\\1\nfoobar\\\n\nfoo\\baz")
  177. end
  178. end
  179. describe 'regex_replace' do
  180. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  181. it 'should replace the all occurrences of a string using regex' do
  182. agent.interpolation_context['something'] = 'foobar foobar'
  183. agent.options['cleaned'] = '{{ something | regex_replace: "\S+bar", "foobaz" }}'
  184. expect(agent.interpolated['cleaned']).to eq('foobaz foobaz')
  185. end
  186. it 'should support escaped characters' do
  187. agent.interpolation_context['something'] = "foo\\1\n\nfoo\\bar\n\nfoo\\baz"
  188. agent.options['test'] = "{{ something | regex_replace: '\\\\(\\w{2,})', '\\1\\\\' | regex_replace: '\\n+', '\\n' }}"
  189. expect(agent.interpolated['test']).to eq("foo\\1\nfoobar\\\nfoobaz\\")
  190. end
  191. end
  192. describe 'regex_replace_first block' do
  193. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  194. it 'should replace the first occurrence of a string using regex' do
  195. agent.interpolation_context['something'] = 'foobar zoobar'
  196. agent.options['cleaned'] = '{% regex_replace_first "(?<word>\S+)(?<suffix>bar)" in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace_first %}'
  197. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  198. end
  199. it 'should be able to take a pattern in a variable' do
  200. agent.interpolation_context['something'] = 'foobar zoobar'
  201. agent.interpolation_context['pattern'] = "(?<word>\\S+)(?<suffix>bar)"
  202. agent.options['cleaned'] = '{% regex_replace_first pattern in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace_first %}'
  203. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  204. end
  205. it 'should define a variable named "match" in a "with" block' do
  206. agent.interpolation_context['something'] = 'foobar zoobar'
  207. agent.interpolation_context['pattern'] = "(?<word>\\S+)(?<suffix>bar)"
  208. agent.options['cleaned'] = '{% regex_replace_first pattern in %}{{ something }}{% with %}{{ match.word | upcase }}{{ match["suffix"] }}{% endregex_replace_first %}'
  209. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  210. end
  211. end
  212. describe 'regex_replace block' do
  213. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  214. it 'should replace the all occurrences of a string using regex' do
  215. agent.interpolation_context['something'] = 'foobar zoobar'
  216. agent.options['cleaned'] = '{% regex_replace "(?<word>\S+)(?<suffix>bar)" in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace %}'
  217. expect(agent.interpolated['cleaned']).to eq('FOObar ZOObar')
  218. end
  219. end
  220. context 'as_object' do
  221. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  222. it 'returns an array that was splitted in liquid tags' do
  223. agent.interpolation_context['something'] = 'test,string,abc'
  224. agent.options['array'] = "{{something | split: ',' | as_object}}"
  225. expect(agent.interpolated['array']).to eq(['test', 'string', 'abc'])
  226. end
  227. it 'returns an object that was not modified in liquid' do
  228. agent.interpolation_context['something'] = {'nested' => {'abc' => 'test'}}
  229. agent.options['object'] = "{{something.nested | as_object}}"
  230. expect(agent.interpolated['object']).to eq({"abc" => 'test'})
  231. end
  232. context 'as_json' do
  233. def ensure_safety(obj)
  234. JSON.parse(JSON.dump(obj))
  235. end
  236. it 'it converts "complex" objects' do
  237. agent.interpolation_context['something'] = {'nested' => Service.new}
  238. agent.options['object'] = "{{something | as_object}}"
  239. expect(agent.interpolated['object']).to eq({'nested'=> ensure_safety(Service.new.as_json)})
  240. end
  241. it 'works with AgentDrops' do
  242. agent.interpolation_context['something'] = agent
  243. agent.options['object'] = "{{something | as_object}}"
  244. expect(agent.interpolated['object']).to eq(ensure_safety(agent.to_liquid.as_json.stringify_keys))
  245. end
  246. it 'works with EventDrops' do
  247. event = Event.new(payload: {some: 'payload'}, agent: agent, created_at: Time.now)
  248. agent.interpolation_context['something'] = event
  249. agent.options['object'] = "{{something | as_object}}"
  250. expect(agent.interpolated['object']).to eq(ensure_safety(event.to_liquid.as_json.stringify_keys))
  251. end
  252. it 'works with MatchDataDrops' do
  253. match = "test string".match(/\A(?<word>\w+)\s(.+?)\z/)
  254. agent.interpolation_context['something'] = match
  255. agent.options['object'] = "{{something | as_object}}"
  256. expect(agent.interpolated['object']).to eq(ensure_safety(match.to_liquid.as_json.stringify_keys))
  257. end
  258. it 'works with URIDrops' do
  259. uri = URI.parse("https://google.com?q=test")
  260. agent.interpolation_context['something'] = uri
  261. agent.options['object'] = "{{something | as_object}}"
  262. expect(agent.interpolated['object']).to eq(ensure_safety(uri.to_liquid.as_json.stringify_keys))
  263. end
  264. end
  265. end
  266. end