require 'sprockets_relative_url' describe SprocketsRelativeUrl::Processor do let(:env) do Sprockets::Environment.new.tap do |env| env.append_path('tmp') end end let(:base_path) { Pathname.new(File.expand_path('./tmp')) } let(:context) do double( pathname: base_path + 'css' + 'application.css', environment: env ) end let(:asset_prefix) { '/assets' } let(:files) { %w(images/background.gif css/extra/test.gif images/\.gif) } let(:hash) { Time.now.to_i.to_s } subject { SprocketsRelativeUrl::Processor.new { data } } before do files.each do |file| pathname = base_path.join(file) pathname.parent.mkpath pathname.open('w').close unless pathname.exist? end allow(context).to receive(:asset_path) do |s| base, _, suffix = s.rpartition('.') File.join(asset_prefix, "#{base}-#{hash}.#{suffix}") end end shared_examples 'rewrite the url' do it 'rewrites the url' do expect(subject.evaluate(context)).to match expected end end shared_examples 'leave the url alone' do it 'leaves the url alone' do expect(subject.evaluate(context)).to be == data end end context 'with a simple url' do let(:data) { 'background: url(../images/background.gif)' } let(:expected) do %r{\Abackground: url\(/assets/images/background-\h+\.gif\)\z} end include_examples 'rewrite the url' context 'with single quotes' do let(:data) { "background: url('../images/background.gif')" } include_examples 'rewrite the url' end context 'with double quotes' do let(:data) { 'background: url("../images/background.gif")' } include_examples 'rewrite the url' end context 'with surrounding whitespace' do let(:data) { 'background: url( ../images/background.gif )' } include_examples 'rewrite the url' context 'with single quotes' do let(:data) { "background: url( '../images/background.gif' )" } include_examples 'rewrite the url' end context 'with double quotes' do let(:data) { 'background: url( "../images/background.gif" )' } include_examples 'rewrite the url' end end end context 'with an expected hash' do let(:hash) { '1234abcd' } let(:data) { 'background: url(../images/background.gif)' } let(:expected) { 'background: url(/assets/images/background-1234abcd.gif)' } include_examples 'rewrite the url' end context 'with a different asset prefix' do let(:asset_prefix) { '/differentpath' } let(:data) { 'background: url(../images/background.gif)' } let(:expected) do %r{\Abackground: url\(/differentpath/images/background-\h+\.gif\)\z} end include_examples 'rewrite the url' end context 'with trailing text' do let(:data) { 'background: url(../images/background.gif) repeat-x' } let(:expected) do %r{\Abackground: url\(/assets/images/background-\h+\.gif\) repeat-x\z} end include_examples 'rewrite the url' end context 'with a lower relative url' do let(:data) { 'background: url(extra/test.gif)' } let(:expected) do %r{\Abackground: url\(/assets/css/extra/test-\h+\.gif\)\z} end include_examples 'rewrite the url' end context 'with url encoding' do let(:data) { 'background: url(../images/%5c.gif)' } let(:expected) { %r{background: url\(/assets/images/%5[cC]-\h+\.gif\)} } include_examples 'rewrite the url' end context 'with query parameters' do let(:data) { 'background: url(../images/background.gif?1)' } let(:expected) do %r{background: url\(/assets/images/background-\h+\.gif\?1\)} end include_examples 'rewrite the url' end context 'with a url fragment' do let(:data) { 'background: url(../images/background.gif#1)' } let(:expected) do %r{background: url\(/assets/images/background-\h+\.gif#1\)} end include_examples 'rewrite the url' end context 'with a root-relative url' do let(:data) { 'background: url(/images/background.gif)' } include_examples 'leave the url alone' end context 'with an absolute url' do let(:data) { 'background: url(http://example.com/images/background.gif)' } include_examples 'leave the url alone' end context 'with a non-existent file' do let(:data) { 'background: url(../images/nonexistent.gif)' } include_examples 'leave the url alone' end context 'with preceding characters' do let(:data) { 'background: nurl(../images/background.gif)' } include_examples 'leave the url alone' end context 'with escaped characters' do let(:data) { 'background: url(../\ images/background.gif)' } include_examples 'leave the url alone' end context 'with escaped parentheses' do let(:data) { 'background: url(../\)images/background.gif)' } include_examples 'leave the url alone' end end