liquid_interpolatable_spec.rb 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. (1..5).each do |i|
  90. stub_request(:head, "http://2many.x/#{i}").
  91. to_return(status: 301, headers: { Location: "http://2many.x/#{i+1}" })
  92. end
  93. stub_request(:head, 'http://2many.x/6').
  94. to_return(status: 301, headers: { 'Content-Length' => '5' })
  95. end
  96. it 'should follow redirects' do
  97. expect(@filter.uri_expand('https://t.co.x/aaaa')).to eq('http://www.example.com/welcome')
  98. end
  99. it 'should respect the limit for the number of redirects' do
  100. expect(@filter.uri_expand('http://2many.x/1')).to eq('http://2many.x/1')
  101. expect(@filter.uri_expand('http://2many.x/1', 6)).to eq('http://2many.x/6')
  102. end
  103. it 'should detect a redirect loop' do
  104. stub_request(:head, 'http://bad.x/aaaa').
  105. to_return(status: 301, headers: { Location: 'http://bad.x/bbbb' })
  106. stub_request(:head, 'http://bad.x/bbbb').
  107. to_return(status: 301, headers: { Location: 'http://bad.x/aaaa' })
  108. expect(@filter.uri_expand('http://bad.x/aaaa')).to eq('http://bad.x/aaaa')
  109. end
  110. it 'should be able to handle an FTP URL' do
  111. stub_request(:head, 'http://downloads.x/aaaa').
  112. to_return(status: 301, headers: { Location: 'http://downloads.x/download?file=aaaa.zip' })
  113. stub_request(:head, 'http://downloads.x/download').
  114. with(query: { file: 'aaaa.zip' }).
  115. to_return(status: 301, headers: { Location: 'ftp://downloads.x/pub/aaaa.zip' })
  116. expect(@filter.uri_expand('http://downloads.x/aaaa')).to eq('ftp://downloads.x/pub/aaaa.zip')
  117. end
  118. describe 'used in interpolation' do
  119. before do
  120. @agent = Agents::InterpolatableAgent.new(name: "test")
  121. end
  122. it 'should follow redirects' do
  123. @agent.interpolation_context['short_url'] = 'https://t.co.x/aaaa'
  124. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  125. expect(@agent.interpolated['long_url']).to eq('http://www.example.com/welcome')
  126. end
  127. it 'should respect the limit for the number of redirects' do
  128. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  129. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  130. expect(@agent.interpolated['long_url']).to eq('http://2many.x/1')
  131. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  132. @agent.options['long_url'] = '{{ short_url | uri_expand:6 }}'
  133. expect(@agent.interpolated['long_url']).to eq('http://2many.x/6')
  134. end
  135. end
  136. end
  137. end