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' })

      (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
  end
end