require 'spec_helper' require 'nokogiri' describe Rack::LiveReload do let(:middleware) { described_class.new(app, options) } let(:app) { stub } subject { middleware } its(:app) { should == app } let(:env) { {} } let(:options) { {} } describe "livereload local uri" do context 'does not exist' do before do stub_request(:any, 'localhost:35729/livereload.js').to_timeout end it { should use_vendored } end context 'exists' do before do stub_request(:any, 'localhost:35729/livereload.js') end it { should_not use_vendored } end context 'with custom port' do let(:options) { {:live_reload_port => '12348'}} context 'exists' do before do stub_request(:any, 'localhost:12348/livereload.js') end it { should_not use_vendored } end end context 'specify vendored' do let(:options) { { :source => :vendored } } it { should use_vendored } end context 'specify LR' do let(:options) { { :source => :livereload } } it { should_not use_vendored } end end context 'not text/html' do let(:ret) { [ 200, { 'Content-Type' => 'image/png' }, [ '
' ] ] } before do app.stubs(:call).with(env).returns(ret) end it 'should pass through' do middleware.call(env).should == ret end end context 'text/event-stram' do let(:body) { [ '' ] } let(:ret) { [ 200, { 'Content-Type' => 'text/event-stream' }, body ] } before do app.stubs(:call).with(env).returns(ret) body.expects(:close).never body.stubs(:respond_to?).with(:close).returns(true) end it 'should pass through' do middleware.call(env).should == ret end end context 'chunked response' do let(:body) { [ '' ] } let(:ret) { [ 200, { 'Transfer-Encoding' => 'chunked' }, body ] } before do app.stubs(:call).with(env).returns(ret) body.expects(:close).never body.stubs(:respond_to?).with(:close).returns(true) end it 'should pass through' do middleware.call(env).should == ret end end context 'unknown Content-Type' do let(:ret) { [ 200, {}, [ 'hey ho' ] ] } before do app.stubs(:call).with(env).returns(ret) end it 'should not break' do middleware.call(env).should_not raise_error(NoMethodError, /You have a nil object/) end end context 'text/html' do before do app.stubs(:call).with(env).returns([ 200, { 'Content-Type' => 'text/html', 'Content-Length' => 0 }, [ page_html ] ]) middleware.stubs(:use_vendored?).returns(true) end let(:host) { 'host' } let(:env) { { 'HTTP_HOST' => host } } let(:ret) { middleware._call(env) } let(:body) { ret.last.join } let(:length) { ret[1]['Content-Length'] } let(:page_html) { '' } context 'vendored' do it 'should add the vendored livereload js script tag' do body.should include("script") body.should include(described_class::LIVERELOAD_JS_PATH) length.should == body.length.to_s described_class::LIVERELOAD_JS_PATH.should_not include(host) body.should include('swfobject') body.should include('web_socket') end end context 'at the top of the head tag' do let(:page_html) { '' } let(:body_dom) { Nokogiri::XML(body) } it 'should add the livereload js script tag before all other script tags' do body_dom.at_css("head")[:attribute].should == 'attribute' body_dom.at_css("script:eq(4)")[:src].should include(described_class::LIVERELOAD_JS_PATH) body_dom.at_css("script:last-child")[:insert].should == "before" end end describe "LIVERELOAD_PORT value" do let(:options) { {:live_reload_port => 12345 }} it "sets the variable at the top of the file" do body.should include 'RACK_LIVERELOAD_PORT = 12345' end end context 'in header tags' do let(:page_html) { "