require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe "Rack::Gist" do before(:all) do @gist_id = 348301 @app = lambda do |env| headers = { 'Content-Type' => 'text/html' } body = File.read(File.join(File.dirname(__FILE__), "body-#{env['PATH_INFO'].gsub(/[^\w]/, '')}.html")) [200, headers, body] end end def mock_env(path = '/full') Rack::MockRequest.env_for(path) end def middleware(options = {}) Rack::Gist.new(@app, options) end it 'should pass Rack::Lint' do status, headers, body = Rack::Lint.new(middleware).call(mock_env) status, headers, body = Rack::Lint.new(middleware).call(mock_env('/multiple')) status, headers, body = Rack::Lint.new(middleware).call(mock_env("/gist.github.com/#{@gist_id}/example.pig.js")) end it 'should rewrite gist embed tags for full gists' do middleware.tap do |a| status, headers, body = a.call(mock_env) status.should == 200 headers['Content-Type'].should == 'text/html' body.to_s.should have_html_tag('div').with('id' => "rack-gist-#{@gist_id}", 'gist-id' => @gist_id, 'class' => 'rack-gist') end end it 'should rewrite gist embed tags for partial gists' do middleware.tap do |a| status, headers, body = a.call(mock_env('/partial')) status.should == 200 headers['Content-Type'].should == 'text/html' body.to_s.should have_html_tag('div').with('id' => "rack-gist-#{@gist_id}", 'gist-id' => @gist_id, 'class' => 'rack-gist', 'rack-gist-file' => 'example.pig') end end it 'should not include the github css file if no gists are present' do middleware.tap do |a| status, headers, body = a.call(mock_env('/none')) status.should == 200 headers['Content-Type'].should == 'text/html' body.to_s.should_not have_html_tag('link').with('rel' => 'stylesheet', 'href' => 'http://gist.github.com/stylesheets/gist/embed.css') end end it 'should include the github css file once' do middleware.tap do |a| status, headers, body = a.call(mock_env('/multiple')) status.should == 200 headers['Content-Type'].should == 'text/html' body.to_s.should have_html_tag('link').with('rel' => 'stylesheet', 'href' => 'http://gist.github.com/stylesheets/gist/embed.css') end end it 'should include jquery by default' do middleware.tap do |a| status, headers, body = a.call(mock_env) status.should == 200 headers['Content-Type'].should == 'text/html' body.to_s.should have_html_tag('script[@src*="jquery"]') end end it 'should not include jquery if the option is passed to disable it' do middleware(:jquery => false).tap do |a| status, headers, body = a.call(mock_env) status.should == 200 headers['Content-Type'].should == 'text/html' body.to_s.should_not have_html_tag('script[@src*="jquery"]') end end it 'should include required jquery helper' do middleware(:jquery => false).tap do |a| status, headers, body = a.call(mock_env) status.should == 200 headers['Content-Type'].should == 'text/html' body.to_s.should have_html_tag('script').containing('CDATA') end end it "shouldn't include the jquery helper if no gist is present" do middleware.tap do |a| status, headers, body = a.call(mock_env) status.should == 200 headers['Content-Type'].should == 'text/html' body.to_s.should_not have_html_tag('script') end end it 'should update Content-Length' do middleware.tap do |a| status, headers, body = a.call(mock_env) status.should == 200 headers['Content-Type'].should == 'text/html' headers['Content-Length'].should == '913' end end it 'should proxy/serve single gists' do middleware.tap do |a| status, headers, body = a.call(mock_env("/gist.github.com/#{@gist_id}/example.pig.js")) status.should == 200 headers['Content-Type'].should == 'application/javascript' body.to_s.should == %q{$('#rack-gist-%s[rack-gist-file="example.pig"]').replaceWith('
REGISTER com.darkhax.blog.pig.jar;<\/div>DEFINE Parser com.darkhax.blog.pig.LogParser();<\/div>
<\/div>logs = LOAD \'apache.log.bz2\' USING TextLoader AS (line: chararray);<\/div>log_events = FOREACH logs GENERATE FLATTEN(Parser(line));<\/div>
<\/div>by_action = GROUP log_events BY action;<\/div>counts = FOREACH by_action GENERATE group, COUNT(log_events);<\/div>STORE counts INTO \'count_summary\';<\/div><\/pre><\/div>\n \n <\/div>\n\n