liquid_interpolatable_spec.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. '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",
  71. options: { 'foo' => '{% assign u = s | to_uri %}{{ u.path }}' })
  72. @agent.interpolation_context['s'] = 'http://example.com/dir/1?q=test'
  73. end
  74. it 'should parse an absolute URI' do
  75. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  76. end
  77. it 'should parse an absolute URI with a base URI specified' do
  78. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  79. end
  80. it 'should parse a relative URI with a base URI specified' do
  81. expect(@filter.to_uri('foo/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/foo/index.html'))
  82. end
  83. it 'should parse an absolute URI with a base URI specified' do
  84. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  85. end
  86. it 'should stringify a non-string operand' do
  87. expect(@filter.to_uri(123, 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/123'))
  88. end
  89. it 'should normalize a URL' do
  90. expect(@filter.to_uri('a[]', 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/a%5B%5D'))
  91. end
  92. it 'should return a URI value in interpolation' do
  93. expect(@agent.interpolated['foo']).to eq('/dir/1')
  94. end
  95. it 'should return a URI value resolved against a base URI in interpolation' do
  96. @agent.options['foo'] = '{% assign u = s | to_uri:"http://example.com/dir/1" %}{{ u.path }}'
  97. @agent.interpolation_context['s'] = 'foo/index.html'
  98. expect(@agent.interpolated['foo']).to eq('/dir/foo/index.html')
  99. end
  100. it 'should normalize a URI value if an empty base URI is given' do
  101. @agent.options['foo'] = '{{ u | to_uri: b }}'
  102. @agent.interpolation_context['u'] = "\u{3042}"
  103. @agent.interpolation_context['b'] = ""
  104. expect(@agent.interpolated['foo']).to eq('%E3%81%82')
  105. @agent.interpolation_context['b'] = nil
  106. expect(@agent.interpolated['foo']).to eq('%E3%81%82')
  107. end
  108. end
  109. describe 'uri_expand' do
  110. before do
  111. stub_request(:head, 'https://t.co.x/aaaa')
  112. .to_return(status: 301, headers: { Location: 'https://bit.ly.x/bbbb' })
  113. stub_request(:head, 'https://bit.ly.x/bbbb')
  114. .to_return(status: 301, headers: { Location: 'http://tinyurl.com.x/cccc' })
  115. stub_request(:head, 'http://tinyurl.com.x/cccc')
  116. .to_return(status: 301, headers: { Location: 'http://www.example.com/welcome' })
  117. stub_request(:head, 'http://www.example.com/welcome')
  118. .to_return(status: 200)
  119. (1..5).each do |i|
  120. stub_request(:head, "http://2many.x/#{i}")
  121. .to_return(status: 301, headers: { Location: "http://2many.x/#{i + 1}" })
  122. end
  123. stub_request(:head, 'http://2many.x/6')
  124. .to_return(status: 301, headers: { 'Content-Length' => '5' })
  125. end
  126. it 'should handle inaccessible URIs' do
  127. expect(@filter.uri_expand(nil)).to eq('')
  128. expect(@filter.uri_expand('')).to eq('')
  129. expect(@filter.uri_expand(5)).to eq('5')
  130. expect(@filter.uri_expand([])).to eq('%5B%5D')
  131. expect(@filter.uri_expand({})).to eq('%7B%7D')
  132. expect(@filter.uri_expand(URI('/'))).to eq('/')
  133. expect(@filter.uri_expand(URI('http:google.com'))).to eq('http:google.com')
  134. expect(@filter.uri_expand(URI('http:/google.com'))).to eq('http:/google.com')
  135. expect(@filter.uri_expand(URI('ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT'))).to eq('ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT')
  136. end
  137. it 'should follow redirects' do
  138. expect(@filter.uri_expand('https://t.co.x/aaaa')).to eq('http://www.example.com/welcome')
  139. end
  140. it 'should respect the limit for the number of redirects' do
  141. expect(@filter.uri_expand('http://2many.x/1')).to eq('http://2many.x/1')
  142. expect(@filter.uri_expand('http://2many.x/1', 6)).to eq('http://2many.x/6')
  143. end
  144. it 'should detect a redirect loop' do
  145. stub_request(:head, 'http://bad.x/aaaa')
  146. .to_return(status: 301, headers: { Location: 'http://bad.x/bbbb' })
  147. stub_request(:head, 'http://bad.x/bbbb')
  148. .to_return(status: 301, headers: { Location: 'http://bad.x/aaaa' })
  149. expect(@filter.uri_expand('http://bad.x/aaaa')).to eq('http://bad.x/aaaa')
  150. end
  151. it 'should be able to handle an FTP URL' do
  152. stub_request(:head, 'http://downloads.x/aaaa')
  153. .to_return(status: 301, headers: { Location: 'http://downloads.x/download?file=aaaa.zip' })
  154. stub_request(:head, 'http://downloads.x/download')
  155. .with(query: { file: 'aaaa.zip' })
  156. .to_return(status: 301, headers: { Location: 'ftp://downloads.x/pub/aaaa.zip' })
  157. expect(@filter.uri_expand('http://downloads.x/aaaa')).to eq('ftp://downloads.x/pub/aaaa.zip')
  158. end
  159. describe 'used in interpolation' do
  160. before do
  161. @agent = Agents::InterpolatableAgent.new(name: "test")
  162. end
  163. it 'should follow redirects' do
  164. @agent.interpolation_context['short_url'] = 'https://t.co.x/aaaa'
  165. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  166. expect(@agent.interpolated['long_url']).to eq('http://www.example.com/welcome')
  167. end
  168. it 'should respect the limit for the number of redirects' do
  169. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  170. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  171. expect(@agent.interpolated['long_url']).to eq('http://2many.x/1')
  172. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  173. @agent.options['long_url'] = '{{ short_url | uri_expand:6 }}'
  174. expect(@agent.interpolated['long_url']).to eq('http://2many.x/6')
  175. end
  176. end
  177. end
  178. describe 'regex_replace_first' do
  179. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  180. it 'should replace the first occurrence of a string using regex' do
  181. agent.interpolation_context['something'] = 'foobar foobar'
  182. agent.options['cleaned'] = '{{ something | regex_replace_first: "\S+bar", "foobaz" }}'
  183. expect(agent.interpolated['cleaned']).to eq('foobaz foobar')
  184. end
  185. it 'should support escaped characters' do
  186. agent.interpolation_context['something'] = "foo\\1\n\nfoo\\bar\n\nfoo\\baz"
  187. agent.options['test'] =
  188. "{{ something | regex_replace_first: '\\\\(\\w{2,})', '\\1\\\\' | regex_replace_first: '\\n+', '\\n' }}"
  189. expect(agent.interpolated['test']).to eq("foo\\1\nfoobar\\\n\nfoo\\baz")
  190. end
  191. end
  192. describe 'regex_extract' do
  193. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  194. it 'should extract the matched part' do
  195. agent.interpolation_context['something'] = "foo BAR BAZ"
  196. agent.options['test'] =
  197. "{{ something | regex_extract: '[A-Z]+' }} / {{ something | regex_extract: '[A-Z]([A-Z]+)', 1 }} / {{ something | regex_extract: '(?<x>.)AZ', 'x' }}"
  198. expect(agent.interpolated['test']).to eq("BAR / AR / B")
  199. end
  200. it 'should return nil if not matched' do
  201. agent.interpolation_context['something'] = "foo BAR BAZ"
  202. agent.options['test'] =
  203. "{% assign var = something | regex_extract: '[A-Z][a-z]+' %}{% if var == nil %}nil{% else %}non-nil{% endif %}"
  204. expect(agent.interpolated['test']).to eq("nil")
  205. end
  206. end
  207. describe 'regex_replace' do
  208. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  209. it 'should replace the all occurrences of a string using regex' do
  210. agent.interpolation_context['something'] = 'foobar foobar'
  211. agent.options['cleaned'] = '{{ something | regex_replace: "\S+bar", "foobaz" }}'
  212. expect(agent.interpolated['cleaned']).to eq('foobaz foobaz')
  213. end
  214. it 'should support escaped characters' do
  215. agent.interpolation_context['something'] = "foo\\1\n\nfoo\\bar\n\nfoo\\baz"
  216. agent.options['test'] =
  217. "{{ something | regex_replace: '\\\\(\\w{2,})', '\\1\\\\' | regex_replace: '\\n+', '\\n' }}"
  218. expect(agent.interpolated['test']).to eq("foo\\1\nfoobar\\\nfoobaz\\")
  219. end
  220. end
  221. describe 'regex_replace_first block' do
  222. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  223. it 'should replace the first occurrence of a string using regex' do
  224. agent.interpolation_context['something'] = 'foobar zoobar'
  225. agent.options['cleaned'] =
  226. '{% regex_replace_first "(?<word>\S+)(?<suffix>bar)" in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace_first %}'
  227. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  228. end
  229. it 'should be able to take a pattern in a variable' do
  230. agent.interpolation_context['something'] = 'foobar zoobar'
  231. agent.interpolation_context['pattern'] = "(?<word>\\S+)(?<suffix>bar)"
  232. agent.options['cleaned'] =
  233. '{% regex_replace_first pattern in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace_first %}'
  234. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  235. end
  236. it 'should define a variable named "match" in a "with" block' do
  237. agent.interpolation_context['something'] = 'foobar zoobar'
  238. agent.interpolation_context['pattern'] = "(?<word>\\S+)(?<suffix>bar)"
  239. agent.options['cleaned'] =
  240. '{% regex_replace_first pattern in %}{{ something }}{% with %}{{ match.word | upcase }}{{ match["suffix"] }}{% endregex_replace_first %}'
  241. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  242. end
  243. end
  244. describe 'regex_replace block' do
  245. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  246. it 'should replace the all occurrences of a string using regex' do
  247. agent.interpolation_context['something'] = 'foobar zoobar'
  248. agent.options['cleaned'] =
  249. '{% regex_replace "(?<word>\S+)(?<suffix>bar)" in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace %}'
  250. expect(agent.interpolated['cleaned']).to eq('FOObar ZOObar')
  251. end
  252. end
  253. describe 'fromjson' do
  254. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  255. it 'should parse a JSON string' do
  256. agent.interpolation_context['json'] = '{"array": ["a", "b", "c"], "number": 42}'
  257. agent.options['key'] = '{% assign obj = json | fromjson %}{{ obj["array"][1] }} and {{ obj.number }}'
  258. expect(agent.interpolated['key']).to eq('b and 42')
  259. end
  260. end
  261. context 'as_object' do
  262. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  263. it 'returns an array that was splitted in liquid tags' do
  264. agent.interpolation_context['something'] = 'test,string,abc'
  265. agent.options['array'] = "{{something | split: ',' | as_object}}"
  266. expect(agent.interpolated['array']).to eq(['test', 'string', 'abc'])
  267. end
  268. it 'returns an object that was not modified in liquid' do
  269. agent.interpolation_context['something'] = { 'nested' => { 'abc' => 'test' } }
  270. agent.options['object'] = "{{something.nested | as_object}}"
  271. expect(agent.interpolated['object']).to eq({ "abc" => 'test' })
  272. end
  273. context 'as_json' do
  274. def ensure_safety(obj)
  275. JSON.parse(JSON.dump(obj))
  276. end
  277. it 'it converts "complex" objects' do
  278. agent.interpolation_context['something'] = { 'nested' => Service.new }
  279. agent.options['object'] = "{{something | as_object}}"
  280. expect(agent.interpolated['object']).to eq({ 'nested' => ensure_safety(Service.new.as_json) })
  281. end
  282. it 'works with Agent::Drops' do
  283. agent.interpolation_context['something'] = agent
  284. agent.options['object'] = "{{something | as_object}}"
  285. expect(agent.interpolated['object']).to eq(ensure_safety(agent.to_liquid.as_json.stringify_keys))
  286. end
  287. it 'works with Event::Drops' do
  288. event = Event.new(payload: { some: 'payload' }, agent:, created_at: Time.now)
  289. agent.interpolation_context['something'] = event
  290. agent.options['object'] = "{{something | as_object}}"
  291. expect(agent.interpolated['object']).to eq(ensure_safety(event.to_liquid.as_json.stringify_keys))
  292. end
  293. it 'works with MatchDataDrops' do
  294. match = "test string".match(/\A(?<word>\w+)\s(.+?)\z/)
  295. agent.interpolation_context['something'] = match
  296. agent.options['object'] = "{{something | as_object}}"
  297. expect(agent.interpolated['object']).to eq(ensure_safety(match.to_liquid.as_json.stringify_keys))
  298. end
  299. it 'works with URIDrops' do
  300. uri = URI.parse("https://google.com?q=test")
  301. agent.interpolation_context['something'] = uri
  302. agent.options['object'] = "{{something | as_object}}"
  303. expect(agent.interpolated['object']).to eq(ensure_safety(uri.to_liquid.as_json.stringify_keys))
  304. end
  305. end
  306. end
  307. describe 'rebase_hrefs' do
  308. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  309. let(:fragment) { <<~HTML }
  310. <ul>
  311. <li>
  312. <a href="downloads/file1"><img src="/images/iconA.png" srcset="/images/iconA.png 1x, /images/iconA@2x.png 2x">file1</a>
  313. </li>
  314. <li>
  315. <a href="downloads/file2"><img src="/images/iconA.png" srcset="/images/iconA.png 1x, /images/iconA@2x.png 2x">file2</a>
  316. </li>
  317. <li>
  318. <a href="downloads/file3"><img src="/images/iconB.png" srcset="/images/iconB.png 1x, /images/iconB@2x.png 2x">file3</a>
  319. </li>
  320. </ul>
  321. HTML
  322. let(:replaced_fragment) { <<~HTML }
  323. <ul>
  324. <li>
  325. <a href="http://example.com/support/downloads/file1"><img src="http://example.com/images/iconA.png" srcset="http://example.com/images/iconA.png 1x, http://example.com/images/iconA@2x.png 2x">file1</a>
  326. </li>
  327. <li>
  328. <a href="http://example.com/support/downloads/file2"><img src="http://example.com/images/iconA.png" srcset="http://example.com/images/iconA.png 1x, http://example.com/images/iconA@2x.png 2x">file2</a>
  329. </li>
  330. <li>
  331. <a href="http://example.com/support/downloads/file3"><img src="http://example.com/images/iconB.png" srcset="http://example.com/images/iconB.png 1x, http://example.com/images/iconB@2x.png 2x">file3</a>
  332. </li>
  333. </ul>
  334. HTML
  335. it 'rebases relative URLs in a fragment' do
  336. agent.interpolation_context['content'] = fragment
  337. agent.options['template'] = "{{ content | rebase_hrefs: 'http://example.com/support/files.html' }}"
  338. expect(agent.interpolated['template']).to eq(replaced_fragment)
  339. end
  340. end
  341. describe 'digest filters' do
  342. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  343. it 'computes digest values from string input' do
  344. agent.interpolation_context['value'] = 'Huginn'
  345. agent.interpolation_context['key'] = 'Muninn'
  346. agent.options['template'] = "{{ value | md5 }}"
  347. expect(agent.interpolated['template']).to eq('5fca9fe120027bc87fa9923cc926f8fe')
  348. agent.options['template'] = "{{ value | sha1 }}"
  349. expect(agent.interpolated['template']).to eq('647d81f6dae6ff474cdcef3e9b74f038206af680')
  350. agent.options['template'] = "{{ value | sha256 }}"
  351. expect(agent.interpolated['template']).to eq('62c6099ec14502176974aadf0991525f50332ba552500556fea583ffdf0ba076')
  352. agent.options['template'] = "{{ value | hmac_sha1: key }}"
  353. expect(agent.interpolated['template']).to eq('9bd7cdebac134e06ba87258c28d2deea431407ac')
  354. agent.options['template'] = "{{ value | hmac_sha256: key }}"
  355. expect(agent.interpolated['template']).to eq('38b98bc2625a8cac33369f6204e784482be5e172b242699406270856a841d1ec')
  356. end
  357. end
  358. describe 'group_by' do
  359. let(:events) do
  360. [
  361. { "date" => "2019-07-30", "type" => "Snap" },
  362. { "date" => "2019-07-30", "type" => "Crackle" },
  363. { "date" => "2019-07-29", "type" => "Pop" },
  364. { "date" => "2019-07-29", "type" => "Bam" },
  365. { "date" => "2019-07-29", "type" => "Pow" },
  366. ]
  367. end
  368. it "should group an enumerable by the given attribute" do
  369. expect(@filter.group_by(events, "date")).to eq(
  370. [
  371. {
  372. "name" => "2019-07-30", "items" => [
  373. { "date" => "2019-07-30", "type" => "Snap" },
  374. { "date" => "2019-07-30", "type" => "Crackle" }
  375. ]
  376. },
  377. {
  378. "name" => "2019-07-29", "items" => [
  379. { "date" => "2019-07-29", "type" => "Pop" },
  380. { "date" => "2019-07-29", "type" => "Bam" },
  381. { "date" => "2019-07-29", "type" => "Pow" }
  382. ]
  383. }
  384. ]
  385. )
  386. end
  387. it "should leave non-groupables alone" do
  388. expect(@filter.group_by("some string", "anything")).to eq("some string")
  389. end
  390. end
  391. describe '_agent_' do
  392. let(:agent) { Agents::InterpolatableAgent.new(name: 'test', options: { 'foo' => '{{bar}}' }) }
  393. it 'computes digest values from string input' do
  394. agent.options['template'] = 'name={{ _agent_.name }} foo={{ _agent_.options.foo }}'
  395. expect(agent.interpolated['template']).to eq 'name=test foo={{bar}}'
  396. end
  397. end
  398. end