liquid_interpolatable_spec.rb 6.8KB

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