require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe "Rack::Gist" do before(:all) do @gist_id = 348301 end def pbody(body) ''.tap do |b| body.each { |part| b << part.to_s } # For giggles end end def app(headers) lambda do |env| body = File.read(File.join(File.dirname(__FILE__), "body-#{env['PATH_INFO'].gsub(/[^\w]/, '')}.html")) rescue '' status = 404 if body.empty? [status || 200, headers, [body]] end end def mock_env(path = '/full') Rack::MockRequest.env_for(path) end def middleware(options = {}, headers = { 'Content-Type' => 'text/html' }) Rack::Gist.new(app(headers), 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' pbody(body).should have_html_tag('p').with('id' => "rack-gist-#{@gist_id}", 'gist-id' => @gist_id, 'class' => 'rack-gist') end end it 'should rewrite gist embed tags for full gists when content_type includes other things' do middleware({}, { 'Content-Type' => 'text/html; charset=utf-8' }).tap do |a| status, headers, body = a.call(mock_env) status.should == 200 headers['Content-Type'].should == 'text/html; charset=utf-8' pbody(body).should have_html_tag('p').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' pbody(body).should have_html_tag('p').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' pbody(body).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' pbody(body).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' pbody(body).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' pbody(body).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' pbody(body).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' pbody(body).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 == '1066' 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' pbody(body).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