123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- require 'spec_helper'
- require 'nokogiri'
- describe LiquidInterpolatable::Filters do
- before do
- @filter = Class.new do
- include LiquidInterpolatable::Filters
- end.new
- end
- describe 'uri_escape' do
- it 'should escape a string for use in URI' do
- expect(@filter.uri_escape('abc:/?=')).to eq('abc%3A%2F%3F%3D')
- end
- it 'should not raise an error when an operand is nil' do
- expect(@filter.uri_escape(nil)).to be_nil
- end
- end
- describe 'validations' do
- class Agents::InterpolatableAgent < Agent
- include LiquidInterpolatable
- def check
- create_event :payload => {}
- end
- def validate_options
- interpolated['foo']
- end
- end
- it "should finish without raising an exception" do
- agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{{bar}' })
- expect(agent.valid?).to eq(false)
- expect(agent.errors[:options].first).to match(/not properly terminated/)
- end
- end
- describe 'to_xpath' do
- before do
- def @filter.to_xpath_roundtrip(string)
- Nokogiri::XML('').xpath(to_xpath(string))
- end
- end
- it 'should escape a string for use in XPath expression' do
- [
- %q{abc}.freeze,
- %q{'a"bc'dfa""fds''fa}.freeze,
- ].each { |string|
- expect(@filter.to_xpath_roundtrip(string)).to eq(string)
- }
- end
- it 'should stringify a non-string operand' do
- expect(@filter.to_xpath_roundtrip(nil)).to eq('')
- expect(@filter.to_xpath_roundtrip(1)).to eq('1')
- end
- end
- describe 'to_uri' do
- before do
- @agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{% assign u = s | to_uri %}{{ u.path }}' })
- @agent.interpolation_context['s'] = 'http://example.com/dir/1?q=test'
- end
- it 'should parse an abosule URI' do
- expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
- end
- it 'should parse an abosule URI with a base URI specified' do
- expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
- end
- it 'should parse a relative URI with a base URI specified' do
- expect(@filter.to_uri('foo/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/foo/index.html'))
- end
- it 'should parse an abosule URI with a base URI specified' do
- expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
- end
- it 'should stringify a non-string operand' do
- expect(@filter.to_uri(123, 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/123'))
- end
- it 'should return a URI value in interpolation' do
- expect(@agent.interpolated['foo']).to eq('/dir/1')
- end
- it 'should return a URI value resolved against a base URI in interpolation' do
- @agent.options['foo'] = '{% assign u = s | to_uri:"http://example.com/dir/1" %}{{ u.path }}'
- @agent.interpolation_context['s'] = 'foo/index.html'
- expect(@agent.interpolated['foo']).to eq('/dir/foo/index.html')
- end
- end
- describe 'uri_expand' do
- before do
- stub_request(:head, 'https://t.co.x/aaaa').
- to_return(status: 301, headers: { Location: 'https://bit.ly.x/bbbb' })
- stub_request(:head, 'https://bit.ly.x/bbbb').
- to_return(status: 301, headers: { Location: 'http://tinyurl.com.x/cccc' })
- stub_request(:head, 'http://tinyurl.com.x/cccc').
- to_return(status: 301, headers: { Location: 'http://www.example.com/welcome' })
- stub_request(:head, 'http://www.example.com/welcome').
- to_return(status: 200)
- (1..5).each do |i|
- stub_request(:head, "http://2many.x/#{i}").
- to_return(status: 301, headers: { Location: "http://2many.x/#{i+1}" })
- end
- stub_request(:head, 'http://2many.x/6').
- to_return(status: 301, headers: { 'Content-Length' => '5' })
- end
- it 'should follow redirects' do
- expect(@filter.uri_expand('https://t.co.x/aaaa')).to eq('http://www.example.com/welcome')
- end
- it 'should respect the limit for the number of redirects' do
- expect(@filter.uri_expand('http://2many.x/1')).to eq('http://2many.x/1')
- expect(@filter.uri_expand('http://2many.x/1', 6)).to eq('http://2many.x/6')
- end
- it 'should detect a redirect loop' do
- stub_request(:head, 'http://bad.x/aaaa').
- to_return(status: 301, headers: { Location: 'http://bad.x/bbbb' })
- stub_request(:head, 'http://bad.x/bbbb').
- to_return(status: 301, headers: { Location: 'http://bad.x/aaaa' })
- expect(@filter.uri_expand('http://bad.x/aaaa')).to eq('http://bad.x/aaaa')
- end
- it 'should be able to handle an FTP URL' do
- stub_request(:head, 'http://downloads.x/aaaa').
- to_return(status: 301, headers: { Location: 'http://downloads.x/download?file=aaaa.zip' })
- stub_request(:head, 'http://downloads.x/download').
- with(query: { file: 'aaaa.zip' }).
- to_return(status: 301, headers: { Location: 'ftp://downloads.x/pub/aaaa.zip' })
- expect(@filter.uri_expand('http://downloads.x/aaaa')).to eq('ftp://downloads.x/pub/aaaa.zip')
- end
- describe 'used in interpolation' do
- before do
- @agent = Agents::InterpolatableAgent.new(name: "test")
- end
- it 'should follow redirects' do
- @agent.interpolation_context['short_url'] = 'https://t.co.x/aaaa'
- @agent.options['long_url'] = '{{ short_url | uri_expand }}'
- expect(@agent.interpolated['long_url']).to eq('http://www.example.com/welcome')
- end
- it 'should respect the limit for the number of redirects' do
- @agent.interpolation_context['short_url'] = 'http://2many.x/1'
- @agent.options['long_url'] = '{{ short_url | uri_expand }}'
- expect(@agent.interpolated['long_url']).to eq('http://2many.x/1')
- @agent.interpolation_context['short_url'] = 'http://2many.x/1'
- @agent.options['long_url'] = '{{ short_url | uri_expand:6 }}'
- expect(@agent.interpolated['long_url']).to eq('http://2many.x/6')
- end
- end
-
- describe 'regex replace' do
- let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
- it 'should replace the first occurrence of a string using regex' do
- agent.interpolation_context['something'] = 'foobar foobar'
- agent.options['cleaned'] = '{{ something | regex_replace_first: "\S+bar", "foobaz" }}'
- expect(agent.interpolated['cleaned']).to eq('foobaz foobar')
- end
- it 'should replace the all occurrences of a string using regex' do
- agent.interpolation_context['something'] = 'foobar foobar'
- agent.options['cleaned'] = '{{ something | regex_replace: "\S+bar", "foobaz" }}'
- expect(agent.interpolated['cleaned']).to eq('foobaz foobaz')
- end
-
- end
-
- end
- end
|