From 117f251ff70fe55399220833707c050a91e71ac3 Mon Sep 17 00:00:00 2001 From: Stefan Wrobel Date: Tue, 20 Aug 2013 13:55:47 -0700 Subject: [PATCH] Skip processing for non-GET requests --- .gitignore | 1 + .../livereload/processing_skip_analyzer.rb | 6 +- .../processing_skip_analyzer_spec.rb | 97 ++++++++++++++----- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 3904386..4ee2c24 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Gemfile.lock pkg/* *.orig +tmp/ diff --git a/lib/rack/livereload/processing_skip_analyzer.rb b/lib/rack/livereload/processing_skip_analyzer.rb index 8c8f3b7..ba6ae63 100644 --- a/lib/rack/livereload/processing_skip_analyzer.rb +++ b/lib/rack/livereload/processing_skip_analyzer.rb @@ -16,7 +16,7 @@ module Rack end def skip_processing? - !html? || chunked? || inline? || ignored? || bad_browser? + !html? || chunked? || inline? || ignored? || bad_browser? || !get? end def chunked? @@ -38,6 +38,10 @@ module Rack def html? @headers['Content-Type'] =~ %r{text/html} end + + def get? + @env['REQUEST_METHOD'] == 'GET' + end end end end diff --git a/spec/rack/livereload/processing_skip_analyzer_spec.rb b/spec/rack/livereload/processing_skip_analyzer_spec.rb index 87fe661..e1c856e 100644 --- a/spec/rack/livereload/processing_skip_analyzer_spec.rb +++ b/spec/rack/livereload/processing_skip_analyzer_spec.rb @@ -18,45 +18,40 @@ describe Rack::LiveReload::ProcessingSkipAnalyzer do end end - describe '#bad_browser?' do - let(:user_agent) { described_class::BAD_USER_AGENTS.first.source } - - it { should be_bad_browser } - end - - context 'ignored' do + describe '#ignored?' do let(:options) { { :ignore => [ %r{file} ] } } - context 'not root' do + context 'path contains ignore pattern' do let(:env) { { 'PATH_INFO' => '/this/file' } } it { should be_ignored } end - context 'root' do + context 'root path' do let(:env) { { 'PATH_INFO' => '/' } } it { should_not be_ignored } end end - context 'not text/html' do - let(:headers) { { 'Content-Type' => 'application/pdf' } } + describe '#chunked?' do + context 'regular response' do + it { should_not be_chunked } + end - it { should_not be_html } + context 'chunked response' do + let(:headers) { { 'Transfer-Encoding' => 'chunked' } } + + it { should be_chunked } + end end - context 'chunked response' do - let(:headers) { { 'Transfer-Encoding' => 'chunked' } } + describe '#inline?' do + context 'inline disposition' do + let(:headers) { { 'Content-Disposition' => 'inline; filename=my_inlined_file' } } - it { should be_chunked } - end - - - context 'inline disposition' do - let(:headers) { { 'Content-Disposition' => 'inline; filename=my_inlined_file' } } - - it { should be_inline } + it { should be_inline } + end end describe '#ignored?' do @@ -73,5 +68,63 @@ describe Rack::LiveReload::ProcessingSkipAnalyzer do it { should be_ignored } end end + + describe '#bad_browser?' do + context 'Firefox' do + it { should_not be_bad_browser } + end + + context 'BAD browser' do + let(:user_agent) { described_class::BAD_USER_AGENTS.first.source } + + it { should be_bad_browser } + end + end + + describe '#html?' do + context 'HTML content' do + let(:headers) { { 'Content-Type' => 'text/html' } } + + it { should be_html } + end + + context 'PDF content' do + let(:headers) { { 'Content-Type' => 'application/pdf' } } + + it { should_not be_html } + end + end + + describe '#get?' do + context 'GET request' do + let(:env) { { 'REQUEST_METHOD' => 'GET' } } + + it { should be_get } + end + + context 'PUT request' do + let(:env) { { 'REQUEST_METHOD' => 'PUT' } } + + it { should_not be_get } + end + + context 'POST request' do + let(:env) { { 'REQUEST_METHOD' => 'POST' } } + + it { should_not be_get } + end + + context 'DELETE request' do + let(:env) { { 'REQUEST_METHOD' => 'DELETE' } } + + it { should_not be_get } + end + + context 'PATCH request' do + let(:env) { { 'REQUEST_METHOD' => 'PATCH' } } + + it { should_not be_get } + end + end end