diff --git a/Guardfile b/Guardfile index 1b0946a..5279189 100644 --- a/Guardfile +++ b/Guardfile @@ -1,7 +1,7 @@ -# A sample Guardfile -# More info at https://github.com/guard/guard#readme +# A sample Guardfile +# More info at https://github.com/guard/guard#readme -guard 'rspec', :version => 2 do +guard 'rspec', :version => 2, :cli => '-c' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } @@ -10,3 +10,8 @@ end guard 'livereload' do watch('index.html') end + +guard 'cucumber' do + watch(%r{^features/.+\.feature$}) + watch(%r{^features/support/.+$}) { 'features' } +end diff --git a/Rakefile b/Rakefile index c7b9ecd..791b9fd 100644 --- a/Rakefile +++ b/Rakefile @@ -24,5 +24,9 @@ require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) -task :default => :spec +require 'cucumber/rake/task' + +Cucumber::Rake::Task.new(:cucumber) + +task :default => [ :spec, :cucumber ] diff --git a/features/skip_certain_browsers.feature b/features/skip_certain_browsers.feature new file mode 100644 index 0000000..51b5816 --- /dev/null +++ b/features/skip_certain_browsers.feature @@ -0,0 +1,11 @@ +Feature: Skip Certain Browsers + Scenario Outline: + Given I have a Rack app with Rack::LiveReload + When I make a request to "/" with the following headers: + | HTTP_USER_AGENT | | + Then I should not have any Rack::LiveReload code + + Scenarios: Browsers to check for + | user agent | + | MSIE | + diff --git a/features/step_definitions/given/i_have_a_rack_app_with_live_reload.rb b/features/step_definitions/given/i_have_a_rack_app_with_live_reload.rb new file mode 100644 index 0000000..8bb5ee5 --- /dev/null +++ b/features/step_definitions/given/i_have_a_rack_app_with_live_reload.rb @@ -0,0 +1,7 @@ +Given /^I have a Rack app with Rack::LiveReload$/ do + @app = Rack::Builder.new do + use Rack::LiveReload + + run lambda { |env| [ 200, { 'Content-Type' => 'text/html' }, [ "" ] ] } + end +end diff --git a/features/step_definitions/then/i_should_not_have_livereload_code.rb b/features/step_definitions/then/i_should_not_have_livereload_code.rb new file mode 100644 index 0000000..76a9b1a --- /dev/null +++ b/features/step_definitions/then/i_should_not_have_livereload_code.rb @@ -0,0 +1,4 @@ +Then /^I should not have any Rack::LiveReload code$/ do + @response.body.should_not include("rack/livereload.js") +end + diff --git a/features/step_definitions/when/i_make_a_request_with_headers.rb b/features/step_definitions/when/i_make_a_request_with_headers.rb new file mode 100644 index 0000000..eaba7d5 --- /dev/null +++ b/features/step_definitions/when/i_make_a_request_with_headers.rb @@ -0,0 +1,6 @@ +When /^I make a request to "([^"]*)" with the following headers:$/ do |uri, table| + @request = Rack::MockRequest.new(@app) + + @response = @request.get(uri, table.rows_hash) +end + diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..29c6918 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,3 @@ +require 'rack' +require 'rack-livereload' + diff --git a/index.html b/index.html index 4d02e49..9182445 100644 --- a/index.html +++ b/index.html @@ -1,2 +1,2 @@ - HiLats + HiRats diff --git a/lib/rack/livereload.rb b/lib/rack/livereload.rb index 1f6837a..6945505 100644 --- a/lib/rack/livereload.rb +++ b/lib/rack/livereload.rb @@ -5,11 +5,12 @@ module Rack LIVERELOAD_JS_PATH = '/__rack/livereload.js' LIVERELOAD_LOCAL_URI = 'http://localhost:35729/livereload.js' + BAD_USER_AGENTS = [ %r{MSIE} ] + attr_reader :app def initialize(app, options = {}) - @app = app - @options = options + @app, @options = app, options end def use_vendored? @@ -48,9 +49,8 @@ module Rack else status, headers, body = @app.call(env) - if !@options[:ignore] || !@options[:ignore].any? { |filter| env['PATH_INFO'][filter] } - case headers['Content-Type'] - when %r{text/html} + if !ignored?(env['PATH_INFO']) && !bad_browser?(env['HTTP_USER_AGENT']) + if headers['Content-Type'][%r{text/html}] content_length = 0 body.each do |line| @@ -85,6 +85,14 @@ module Rack end end + def ignored?(path_info) + @options[:ignore] and @options[:ignore].any? { |filter| path_info[filter] } + end + + def bad_browser?(user_agent) + BAD_USER_AGENTS.any? { |pattern| (user_agent || '')[pattern] } + end + private def deliver_file(file) type = case ::File.extname(file) diff --git a/rack-livereload.gemspec b/rack-livereload.gemspec index f4412ec..80b29d1 100644 --- a/rack-livereload.gemspec +++ b/rack-livereload.gemspec @@ -20,6 +20,7 @@ Gem::Specification.new do |s| # specify any dependencies here; for example: s.add_development_dependency "rspec" + s.add_development_dependency "cucumber" s.add_development_dependency "httparty" s.add_development_dependency "sinatra" s.add_development_dependency "shotgun" @@ -28,6 +29,7 @@ Gem::Specification.new do |s| s.add_development_dependency "mocha" s.add_development_dependency "guard" s.add_development_dependency "guard-rspec" + s.add_development_dependency "guard-cucumber" s.add_development_dependency "guard-livereload" s.add_development_dependency "webmock" diff --git a/spec/rack/livereload_spec.rb b/spec/rack/livereload_spec.rb index e36e154..96d1cba 100644 --- a/spec/rack/livereload_spec.rb +++ b/spec/rack/livereload_spec.rb @@ -165,5 +165,25 @@ describe Rack::LiveReload do middleware.call(env).should be_true end end + + 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 end