liquid_interpolatable_spec.rb 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. require 'spec_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 'to_xpath' do
  42. before do
  43. def @filter.to_xpath_roundtrip(string)
  44. Nokogiri::XML('').xpath(to_xpath(string))
  45. end
  46. end
  47. it 'should escape a string for use in XPath expression' do
  48. [
  49. %q{abc}.freeze,
  50. %q{'a"bc'dfa""fds''fa}.freeze,
  51. ].each { |string|
  52. expect(@filter.to_xpath_roundtrip(string)).to eq(string)
  53. }
  54. end
  55. it 'should stringify a non-string operand' do
  56. expect(@filter.to_xpath_roundtrip(nil)).to eq('')
  57. expect(@filter.to_xpath_roundtrip(1)).to eq('1')
  58. end
  59. end
  60. describe 'to_uri' do
  61. before do
  62. @agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{% assign u = s | to_uri %}{{ u.path }}' })
  63. @agent.interpolation_context['s'] = 'http://example.com/dir/1?q=test'
  64. end
  65. it 'should parse an abosule URI' do
  66. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  67. end
  68. it 'should parse an abosule URI with a base URI specified' do
  69. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  70. end
  71. it 'should parse a relative URI with a base URI specified' do
  72. expect(@filter.to_uri('foo/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/foo/index.html'))
  73. end
  74. it 'should parse an abosule URI with a base URI specified' 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 stringify a non-string operand' do
  78. expect(@filter.to_uri(123, 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/123'))
  79. end
  80. it 'should return a URI value in interpolation' do
  81. expect(@agent.interpolated['foo']).to eq('/dir/1')
  82. end
  83. it 'should return a URI value resolved against a base URI in interpolation' do
  84. @agent.options['foo'] = '{% assign u = s | to_uri:"http://example.com/dir/1" %}{{ u.path }}'
  85. @agent.interpolation_context['s'] = 'foo/index.html'
  86. expect(@agent.interpolated['foo']).to eq('/dir/foo/index.html')
  87. end
  88. end
  89. describe 'uri_expand' do
  90. before do
  91. stub_request(:head, 'https://t.co.x/aaaa').
  92. to_return(status: 301, headers: { Location: 'https://bit.ly.x/bbbb' })
  93. stub_request(:head, 'https://bit.ly.x/bbbb').
  94. to_return(status: 301, headers: { Location: 'http://tinyurl.com.x/cccc' })
  95. stub_request(:head, 'http://tinyurl.com.x/cccc').
  96. to_return(status: 301, headers: { Location: 'http://www.example.com/welcome' })
  97. stub_request(:head, 'http://www.example.com/welcome').
  98. to_return(status: 200)
  99. (1..5).each do |i|
  100. stub_request(:head, "http://2many.x/#{i}").
  101. to_return(status: 301, headers: { Location: "http://2many.x/#{i+1}" })
  102. end
  103. stub_request(:head, 'http://2many.x/6').
  104. to_return(status: 301, headers: { 'Content-Length' => '5' })
  105. end
  106. it 'should handle inaccessible URIs' do
  107. expect(@filter.uri_expand(nil)).to eq('')
  108. expect(@filter.uri_expand('')).to eq('')
  109. expect(@filter.uri_expand(5)).to eq('5')
  110. expect(@filter.uri_expand([])).to eq('[]')
  111. expect(@filter.uri_expand({})).to eq('{}')
  112. expect(@filter.uri_expand(URI('/'))).to eq('/')
  113. expect(@filter.uri_expand(URI('http:google.com'))).to eq('http:google.com')
  114. expect(@filter.uri_expand(URI('http:/google.com'))).to eq('http:/google.com')
  115. expect(@filter.uri_expand(URI('ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT'))).to eq('ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT')
  116. end
  117. it 'should follow redirects' do
  118. expect(@filter.uri_expand('https://t.co.x/aaaa')).to eq('http://www.example.com/welcome')
  119. end
  120. it 'should respect the limit for the number of redirects' do
  121. expect(@filter.uri_expand('http://2many.x/1')).to eq('http://2many.x/1')
  122. expect(@filter.uri_expand('http://2many.x/1', 6)).to eq('http://2many.x/6')
  123. end
  124. it 'should detect a redirect loop' do
  125. stub_request(:head, 'http://bad.x/aaaa').
  126. to_return(status: 301, headers: { Location: 'http://bad.x/bbbb' })
  127. stub_request(:head, 'http://bad.x/bbbb').
  128. to_return(status: 301, headers: { Location: 'http://bad.x/aaaa' })
  129. expect(@filter.uri_expand('http://bad.x/aaaa')).to eq('http://bad.x/aaaa')
  130. end
  131. it 'should be able to handle an FTP URL' do
  132. stub_request(:head, 'http://downloads.x/aaaa').
  133. to_return(status: 301, headers: { Location: 'http://downloads.x/download?file=aaaa.zip' })
  134. stub_request(:head, 'http://downloads.x/download').
  135. with(query: { file: 'aaaa.zip' }).
  136. to_return(status: 301, headers: { Location: 'ftp://downloads.x/pub/aaaa.zip' })
  137. expect(@filter.uri_expand('http://downloads.x/aaaa')).to eq('ftp://downloads.x/pub/aaaa.zip')
  138. end
  139. describe 'used in interpolation' do
  140. before do
  141. @agent = Agents::InterpolatableAgent.new(name: "test")
  142. end
  143. it 'should follow redirects' do
  144. @agent.interpolation_context['short_url'] = 'https://t.co.x/aaaa'
  145. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  146. expect(@agent.interpolated['long_url']).to eq('http://www.example.com/welcome')
  147. end
  148. it 'should respect the limit for the number of redirects' do
  149. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  150. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  151. expect(@agent.interpolated['long_url']).to eq('http://2many.x/1')
  152. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  153. @agent.options['long_url'] = '{{ short_url | uri_expand:6 }}'
  154. expect(@agent.interpolated['long_url']).to eq('http://2many.x/6')
  155. end
  156. end
  157. end
  158. describe 'regex_replace_first' do
  159. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  160. it 'should replace the first occurrence of a string using regex' do
  161. agent.interpolation_context['something'] = 'foobar foobar'
  162. agent.options['cleaned'] = '{{ something | regex_replace_first: "\S+bar", "foobaz" }}'
  163. expect(agent.interpolated['cleaned']).to eq('foobaz foobar')
  164. end
  165. it 'should support escaped characters' do
  166. agent.interpolation_context['something'] = "foo\\1\n\nfoo\\bar\n\nfoo\\baz"
  167. agent.options['test'] = "{{ something | regex_replace_first: '\\\\(\\w{2,})', '\\1\\\\' | regex_replace_first: '\\n+', '\\n' }}"
  168. expect(agent.interpolated['test']).to eq("foo\\1\nfoobar\\\n\nfoo\\baz")
  169. end
  170. end
  171. describe 'regex_replace' do
  172. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  173. it 'should replace the all occurrences of a string using regex' do
  174. agent.interpolation_context['something'] = 'foobar foobar'
  175. agent.options['cleaned'] = '{{ something | regex_replace: "\S+bar", "foobaz" }}'
  176. expect(agent.interpolated['cleaned']).to eq('foobaz foobaz')
  177. end
  178. it 'should support escaped characters' do
  179. agent.interpolation_context['something'] = "foo\\1\n\nfoo\\bar\n\nfoo\\baz"
  180. agent.options['test'] = "{{ something | regex_replace: '\\\\(\\w{2,})', '\\1\\\\' | regex_replace: '\\n+', '\\n' }}"
  181. expect(agent.interpolated['test']).to eq("foo\\1\nfoobar\\\nfoobaz\\")
  182. end
  183. end
  184. end