2011-11-04 15:51:22 +00:00
|
|
|
require 'spec_helper'
|
2012-12-03 19:00:45 +00:00
|
|
|
require 'nokogiri'
|
2011-11-04 15:51:22 +00:00
|
|
|
|
|
|
|
describe Rack::LiveReload do
|
2011-11-08 14:48:34 +00:00
|
|
|
let(:middleware) { described_class.new(app, options) }
|
2011-11-04 15:51:22 +00:00
|
|
|
let(:app) { stub }
|
|
|
|
|
|
|
|
subject { middleware }
|
|
|
|
|
|
|
|
its(:app) { should == app }
|
|
|
|
|
|
|
|
let(:env) { {} }
|
2011-11-08 14:48:34 +00:00
|
|
|
let(:options) { {} }
|
|
|
|
|
2013-02-03 11:13:48 +00:00
|
|
|
describe "livereload local uri" do
|
2011-11-08 14:48:34 +00:00
|
|
|
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
|
|
|
|
|
2013-02-03 11:13:48 +00:00
|
|
|
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
|
|
|
|
|
2011-11-08 14:48:34 +00:00
|
|
|
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
|
2011-11-04 15:51:22 +00:00
|
|
|
|
|
|
|
context 'not text/html' do
|
2013-04-21 13:48:32 +00:00
|
|
|
let(:ret) { [ 200, { 'Content-Type' => 'application/pdf' }, [ '<head></head>' ] ] }
|
2011-11-04 15:51:22 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
app.stubs(:call).with(env).returns(ret)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should pass through' do
|
|
|
|
middleware.call(env).should == ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-21 13:48:32 +00:00
|
|
|
context 'image/png' do
|
|
|
|
let(:body) { [ '<head></head>' ] }
|
|
|
|
let(:ret) { [ 200, { 'Content-Type' => 'image/png' }, 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
|
|
|
|
|
2013-01-24 15:22:54 +00:00
|
|
|
context 'text/event-stram' do
|
|
|
|
let(:body) { [ '<head></head>' ] }
|
|
|
|
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
|
|
|
|
|
2013-03-19 06:11:45 +00:00
|
|
|
context 'chunked response' do
|
|
|
|
let(:body) { [ '<head></head>' ] }
|
|
|
|
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
|
|
|
|
|
2013-03-19 06:32:46 +00:00
|
|
|
context 'inline disposition' do
|
|
|
|
let(:body) { [ '<head></head>' ] }
|
|
|
|
let(:ret) { [ 200, { 'Content-Disposition' => 'inline; filename=my_inlined_file' }, 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
|
|
|
|
|
2012-01-23 19:51:08 +00:00
|
|
|
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
|
|
|
|
|
2011-11-04 15:51:22 +00:00
|
|
|
context 'text/html' do
|
|
|
|
before do
|
2012-12-03 19:00:45 +00:00
|
|
|
app.stubs(:call).with(env).returns([ 200, { 'Content-Type' => 'text/html', 'Content-Length' => 0 }, [ page_html ] ])
|
2011-11-08 14:48:34 +00:00
|
|
|
middleware.stubs(:use_vendored?).returns(true)
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
|
2011-11-04 15:57:41 +00:00
|
|
|
let(:host) { 'host' }
|
|
|
|
let(:env) { { 'HTTP_HOST' => host } }
|
|
|
|
|
2012-03-04 18:27:31 +00:00
|
|
|
let(:ret) { middleware._call(env) }
|
2011-11-04 15:51:22 +00:00
|
|
|
let(:body) { ret.last.join }
|
|
|
|
let(:length) { ret[1]['Content-Length'] }
|
|
|
|
|
2012-12-03 19:00:45 +00:00
|
|
|
let(:page_html) { '<head></head>' }
|
|
|
|
|
2011-11-08 14:48:34 +00:00
|
|
|
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
|
2011-11-04 15:51:22 +00:00
|
|
|
|
2011-11-08 14:48:34 +00:00
|
|
|
described_class::LIVERELOAD_JS_PATH.should_not include(host)
|
2011-11-17 20:44:48 +00:00
|
|
|
|
|
|
|
body.should include('swfobject')
|
|
|
|
body.should include('web_socket')
|
2011-11-08 14:48:34 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-04 15:57:41 +00:00
|
|
|
|
2012-12-04 19:12:53 +00:00
|
|
|
context 'at the top of the head tag' do
|
|
|
|
let(:page_html) { '<head attribute="attribute"><script type="text/javascript" insert="first"></script><script type="text/javascript" insert="before"></script></head>' }
|
2012-12-03 19:00:45 +00:00
|
|
|
|
|
|
|
let(:body_dom) { Nokogiri::XML(body) }
|
|
|
|
|
|
|
|
it 'should add the livereload js script tag before all other script tags' do
|
2012-12-04 19:12:53 +00:00
|
|
|
body_dom.at_css("head")[:attribute].should == 'attribute'
|
2013-03-27 21:50:58 +00:00
|
|
|
body_dom.at_css("script:eq(5)")[:src].should include(described_class::LIVERELOAD_JS_PATH)
|
2012-12-03 19:00:45 +00:00
|
|
|
body_dom.at_css("script:last-child")[:insert].should == "before"
|
|
|
|
end
|
2013-02-03 11:13:48 +00:00
|
|
|
|
2013-03-08 19:59:39 +00:00
|
|
|
context 'when a relative URL root is specified' do
|
|
|
|
before do
|
|
|
|
ENV['RAILS_RELATIVE_URL_ROOT'] = '/a_relative_path'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should prepend the relative path to the script src' do
|
|
|
|
body_dom.at_css("script:eq(5)")[:src].should match(%r{^/a_relative_path/})
|
|
|
|
end
|
|
|
|
end
|
2012-12-03 19:00:45 +00:00
|
|
|
end
|
|
|
|
|
2013-02-03 11:13:48 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-13 22:41:30 +00:00
|
|
|
context 'in header tags' do
|
|
|
|
let(:page_html) { "<header class='hero'><h1>Just a normal header tag</h1></header>" }
|
|
|
|
|
|
|
|
let(:body_dom) { Nokogiri::XML(body) }
|
|
|
|
|
|
|
|
it 'should not add the livereload js' do
|
|
|
|
body_dom.at_css("header")[:class].should == 'hero'
|
|
|
|
body_dom.css('script').should be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-08 14:48:34 +00:00
|
|
|
context 'not vendored' do
|
|
|
|
before do
|
|
|
|
middleware.stubs(:use_vendored?).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should add the LR livereload js script tag' do
|
|
|
|
body.should include("script")
|
2013-02-03 11:13:48 +00:00
|
|
|
body.should include(middleware.livereload_local_uri.gsub('localhost', 'host'))
|
2011-11-08 14:48:34 +00:00
|
|
|
end
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
2011-11-07 14:06:40 +00:00
|
|
|
|
|
|
|
context 'set options' do
|
2011-11-07 14:11:27 +00:00
|
|
|
let(:middleware) { described_class.new(app, :host => new_host, :port => port, :min_delay => min_delay, :max_delay => max_delay) }
|
2011-11-07 14:06:40 +00:00
|
|
|
let(:min_delay) { 5 }
|
|
|
|
let(:max_delay) { 10 }
|
|
|
|
let(:port) { 23 }
|
2011-11-07 14:11:27 +00:00
|
|
|
let(:new_host) { 'myhost' }
|
2011-11-07 14:06:40 +00:00
|
|
|
|
|
|
|
it 'should add the livereload.js script tag' do
|
|
|
|
body.should include("mindelay=#{min_delay}")
|
|
|
|
body.should include("maxdelay=#{max_delay}")
|
|
|
|
body.should include("port=#{port}")
|
2011-11-07 14:11:27 +00:00
|
|
|
body.should include("host=#{new_host}")
|
2011-11-07 14:06:40 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-17 20:44:48 +00:00
|
|
|
|
|
|
|
context 'force flash' do
|
|
|
|
let(:middleware) { described_class.new(app, :force_swf => true) }
|
|
|
|
|
|
|
|
it 'should not add the flash shim' do
|
|
|
|
body.should include('WEB_SOCKET_FORCE_FLASH')
|
|
|
|
body.should include('swfobject')
|
|
|
|
body.should include('web_socket')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'no flash' do
|
|
|
|
let(:middleware) { described_class.new(app, :no_swf => true) }
|
|
|
|
|
|
|
|
it 'should not add the flash shim' do
|
|
|
|
body.should_not include('swfobject')
|
|
|
|
body.should_not include('web_socket')
|
|
|
|
end
|
|
|
|
end
|
2011-11-28 15:42:39 +00:00
|
|
|
|
|
|
|
context 'no host at all' do
|
|
|
|
let(:env) { {} }
|
|
|
|
|
|
|
|
it 'should use localhost' do
|
|
|
|
body.should include('localhost')
|
|
|
|
end
|
|
|
|
end
|
2011-12-01 12:25:02 +00:00
|
|
|
|
|
|
|
context 'ignored' do
|
|
|
|
let(:options) { { :ignore => [ %r{file} ] } }
|
|
|
|
|
2011-12-02 11:58:06 +00:00
|
|
|
context 'not root' do
|
|
|
|
let(:env) { { 'PATH_INFO' => '/this/file' } }
|
|
|
|
|
|
|
|
it 'should have no change' do
|
|
|
|
body.should_not include('script')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'root' do
|
|
|
|
let(:env) { { 'PATH_INFO' => '/' } }
|
|
|
|
|
|
|
|
it 'should have script' do
|
|
|
|
body.should include('script')
|
|
|
|
end
|
2011-12-01 12:25:02 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context '/__rack/livereload.js' do
|
|
|
|
let(:env) { { 'PATH_INFO' => described_class::LIVERELOAD_JS_PATH } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
middleware.expects(:deliver_file).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the js file' do
|
2012-03-04 18:27:31 +00:00
|
|
|
middleware._call(env).should be_true
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
|
|
|
end
|
2012-01-03 13:58:51 +00:00
|
|
|
|
|
|
|
describe '#ignored?' do
|
|
|
|
let(:path_info) { 'path info' }
|
|
|
|
|
|
|
|
context 'no ignore set' do
|
|
|
|
it { should_not be_ignored(path_info) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'ignore set' do
|
|
|
|
let(:options) { { :ignore => [ %r{#{path_info}} ] } }
|
|
|
|
|
|
|
|
it { should be_ignored(path_info) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#bad_browser?' do
|
|
|
|
let(:user_agent) { described_class::BAD_USER_AGENTS.first.source }
|
|
|
|
|
|
|
|
it { should be_bad_browser(user_agent) }
|
|
|
|
end
|
2012-12-14 00:27:16 +00:00
|
|
|
|
|
|
|
describe 'head tag regex' do
|
|
|
|
let(:regex) { described_class::HEAD_TAG_REGEX }
|
|
|
|
subject { regex }
|
|
|
|
|
|
|
|
it { should be_kind_of(Regexp) }
|
|
|
|
|
|
|
|
it 'only picks a valid <head> tag' do
|
|
|
|
regex.match("<head></head>").to_s.should eq('<head>')
|
|
|
|
regex.match("<head><title></title></head>").to_s.should eq('<head>')
|
|
|
|
regex.match("<head attribute='something'><title></title></head>").to_s.should eq("<head attribute='something'>")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'responds false when no head tag' do
|
|
|
|
regex.match("<header></header>").should be_false
|
|
|
|
end
|
|
|
|
end
|
2011-11-04 15:51:22 +00:00
|
|
|
end
|
2011-11-07 14:06:40 +00:00
|
|
|
|