leifcr-rack-livereload 0.3.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,200 @@
1
+ require 'spec_helper'
2
+ require 'nokogiri'
3
+
4
+ describe Rack::LiveReload::BodyProcessor do
5
+ describe 'head tag regex' do
6
+ let(:regex) { described_class::HEAD_TAG_REGEX }
7
+ subject { regex }
8
+
9
+ it { should be_kind_of(Regexp) }
10
+
11
+ it 'only picks a valid <head> tag' do
12
+ regex.match("<head></head>").to_s.should eq('<head>')
13
+ regex.match("<head><title></title></head>").to_s.should eq('<head>')
14
+ regex.match("<head attribute='something'><title></title></head>").to_s.should eq("<head attribute='something'>")
15
+ end
16
+
17
+ it 'responds false when no head tag' do
18
+ regex.match("<header></header>").should be_falsey
19
+ end
20
+ end
21
+
22
+ let(:processor) { described_class.new(body, options) }
23
+ let(:body) { [ page_html ] }
24
+ let(:options) { {} }
25
+ let(:page_html) { '<head></head>' }
26
+
27
+ let(:processor_result) do
28
+ if !processor.processed?
29
+ processor.process!(env)
30
+ end
31
+
32
+ processor
33
+ end
34
+
35
+ subject { processor }
36
+
37
+ describe "livereload local uri" do
38
+ context 'does not exist' do
39
+ before do
40
+ stub_request(:any, 'localhost:35729/livereload.js').to_timeout
41
+ end
42
+
43
+ it { should use_vendored }
44
+ end
45
+
46
+ context 'exists' do
47
+ before do
48
+ stub_request(:any, 'localhost:35729/livereload.js')
49
+ end
50
+
51
+ it { should_not use_vendored }
52
+ end
53
+
54
+ context 'with custom port' do
55
+ let(:options) { {:live_reload_port => '12348'}}
56
+
57
+ context 'exists' do
58
+ before do
59
+ stub_request(:any, 'localhost:12348/livereload.js')
60
+ end
61
+ it { should_not use_vendored }
62
+ end
63
+ end
64
+
65
+ context 'specify vendored' do
66
+ let(:options) { { :source => :vendored } }
67
+
68
+ it { should use_vendored }
69
+ end
70
+
71
+ context 'specify LR' do
72
+ let(:options) { { :source => :livereload } }
73
+
74
+ it { should_not use_vendored }
75
+ end
76
+ end
77
+
78
+ context 'text/html' do
79
+ before do
80
+ processor.stubs(:use_vendored?).returns(true)
81
+ end
82
+
83
+ let(:host) { 'host' }
84
+ let(:env) { { 'HTTP_HOST' => host } }
85
+
86
+ let(:processed_body) { processor_result.new_body.join('') }
87
+ let(:length) { processor_result.content_length }
88
+
89
+ let(:page_html) { '<head></head>' }
90
+
91
+ context 'vendored' do
92
+ it 'should add the vendored livereload js script tag' do
93
+ processed_body.should include("script")
94
+ processed_body.should include(described_class::LIVERELOAD_JS_PATH)
95
+
96
+ length.to_s.should == processed_body.length.to_s
97
+
98
+ described_class::LIVERELOAD_JS_PATH.should_not include(host)
99
+
100
+ processed_body.should include('swfobject')
101
+ processed_body.should include('web_socket')
102
+ end
103
+ end
104
+
105
+ context 'at the top of the head tag' do
106
+ let(:page_html) { '<head attribute="attribute"><script type="text/javascript" insert="first"></script><script type="text/javascript" insert="before"></script></head>' }
107
+
108
+ let(:body_dom) { Nokogiri::XML(processed_body) }
109
+
110
+ it 'should add the livereload js script tag before all other script tags' do
111
+ body_dom.at_css("head")[:attribute].should == 'attribute'
112
+ body_dom.at_css("script:eq(5)")[:src].should include(described_class::LIVERELOAD_JS_PATH)
113
+ body_dom.at_css("script:last-child")[:insert].should == "before"
114
+ end
115
+
116
+ context 'when a relative URL root is specified' do
117
+ before do
118
+ ENV['RAILS_RELATIVE_URL_ROOT'] = '/a_relative_path'
119
+ end
120
+
121
+ it 'should prepend the relative path to the script src' do
122
+ body_dom.at_css("script:eq(5)")[:src].should match(%r{^/a_relative_path/})
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "LIVERELOAD_PORT value" do
128
+ let(:options) { { :live_reload_port => 12345 }}
129
+
130
+ it "sets the variable at the top of the file" do
131
+ processed_body.should include 'RACK_LIVERELOAD_PORT = 12345'
132
+ end
133
+ end
134
+
135
+ context 'in header tags' do
136
+ let(:page_html) { "<header class='hero'><h1>Just a normal header tag</h1></header>" }
137
+
138
+ let(:body_dom) { Nokogiri::XML(processed_body) }
139
+
140
+ it 'should not add the livereload js' do
141
+ body_dom.at_css("header")[:class].should == 'hero'
142
+ body_dom.css('script').should be_empty
143
+ end
144
+ end
145
+
146
+ context 'not vendored' do
147
+ before do
148
+ processor.stubs(:use_vendored?).returns(false)
149
+ end
150
+
151
+ it 'should add the LR livereload js script tag' do
152
+ processed_body.should include("script")
153
+ processed_body.should include(processor.livereload_local_uri.gsub('localhost', 'host'))
154
+ end
155
+ end
156
+
157
+ context 'set options' do
158
+ let(:options) { { :host => new_host, :port => port, :min_delay => min_delay, :max_delay => max_delay } }
159
+ let(:min_delay) { 5 }
160
+ let(:max_delay) { 10 }
161
+ let(:port) { 23 }
162
+ let(:new_host) { 'myhost' }
163
+
164
+ it 'should add the livereload.js script tag' do
165
+ processed_body.should include("mindelay=#{min_delay}")
166
+ processed_body.should include("maxdelay=#{max_delay}")
167
+ processed_body.should include("port=#{port}")
168
+ processed_body.should include("host=#{new_host}")
169
+ end
170
+ end
171
+
172
+ context 'force flash' do
173
+ let(:options) { { :force_swf => true } }
174
+
175
+ it 'should not add the flash shim' do
176
+ processed_body.should include('WEB_SOCKET_FORCE_FLASH')
177
+ processed_body.should include('swfobject')
178
+ processed_body.should include('web_socket')
179
+ end
180
+ end
181
+
182
+ context 'no flash' do
183
+ let(:options) { { :no_swf => true } }
184
+
185
+ it 'should not add the flash shim' do
186
+ processed_body.should_not include('swfobject')
187
+ processed_body.should_not include('web_socket')
188
+ end
189
+ end
190
+
191
+ context 'no host at all' do
192
+ let(:env) { {} }
193
+
194
+ it 'should use localhost' do
195
+ processed_body.should include('localhost')
196
+ end
197
+ end
198
+ end
199
+ end
200
+
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::LiveReload::ProcessingSkipAnalyzer do
4
+ subject { described_class.new(result, env, options) }
5
+
6
+ let(:result) { [ status, headers, body ] }
7
+ let(:env) { { 'HTTP_USER_AGENT' => user_agent } }
8
+ let(:options) { {} }
9
+
10
+ let(:user_agent) { 'Firefox' }
11
+ let(:status) { 200 }
12
+ let(:headers) { {} }
13
+ let(:body) { [] }
14
+
15
+ describe '#skip_processing?' do
16
+ it "should skip processing" do
17
+ subject.skip_processing?.should be_truthy
18
+ end
19
+ end
20
+
21
+ describe '#ignored?' do
22
+ let(:options) { { :ignore => [ %r{file} ] } }
23
+
24
+ context 'path contains ignore pattern' do
25
+ let(:env) { { 'PATH_INFO' => '/this/file', 'QUERY_STRING' => '' } }
26
+
27
+ it { should be_ignored }
28
+ end
29
+
30
+ context 'root path' do
31
+ let(:env) { { 'PATH_INFO' => '/', 'QUERY_STRING' => '' } }
32
+
33
+ it { should_not be_ignored }
34
+ end
35
+ end
36
+
37
+ describe '#chunked?' do
38
+ context 'regular response' do
39
+ it { should_not be_chunked }
40
+ end
41
+
42
+ context 'chunked response' do
43
+ let(:headers) { { 'Transfer-Encoding' => 'chunked' } }
44
+
45
+ it { should be_chunked }
46
+ end
47
+ end
48
+
49
+ describe '#inline?' do
50
+ context 'inline disposition' do
51
+ let(:headers) { { 'Content-Disposition' => 'inline; filename=my_inlined_file' } }
52
+
53
+ it { should be_inline }
54
+ end
55
+ end
56
+
57
+ describe '#ignored?' do
58
+ let(:path_info) { 'path info' }
59
+ let(:query_string) { 'query_string' }
60
+ let(:env) { { 'PATH_INFO' => path_info, 'QUERY_STRING' => query_string } }
61
+
62
+ context 'no ignore set' do
63
+ it { should_not be_ignored }
64
+ end
65
+
66
+ context 'ignore set' do
67
+ let(:options) { { :ignore => [ %r{#{path_info}} ] } }
68
+
69
+ it { should be_ignored }
70
+ end
71
+
72
+ context 'ignore set including query_string' do
73
+ let(:options) { { :ignore => [ %r{#{path_info}\?#{query_string}} ] } }
74
+
75
+ it { should be_ignored }
76
+ end
77
+ end
78
+
79
+ describe '#bad_browser?' do
80
+ context 'Firefox' do
81
+ it { should_not be_bad_browser }
82
+ end
83
+
84
+ context 'BAD browser' do
85
+ let(:user_agent) { described_class::BAD_USER_AGENTS.first.source }
86
+
87
+ it { should be_bad_browser }
88
+ end
89
+ end
90
+
91
+ describe '#html?' do
92
+ context 'HTML content' do
93
+ let(:headers) { { 'Content-Type' => 'text/html' } }
94
+
95
+ it { should be_html }
96
+ end
97
+
98
+ context 'PDF content' do
99
+ let(:headers) { { 'Content-Type' => 'application/pdf' } }
100
+
101
+ it { should_not be_html }
102
+ end
103
+ end
104
+
105
+ describe '#get?' do
106
+ context 'GET request' do
107
+ let(:env) { { 'REQUEST_METHOD' => 'GET' } }
108
+
109
+ it { should be_get }
110
+ end
111
+
112
+ context 'PUT request' do
113
+ let(:env) { { 'REQUEST_METHOD' => 'PUT' } }
114
+
115
+ it { should_not be_get }
116
+ end
117
+
118
+ context 'POST request' do
119
+ let(:env) { { 'REQUEST_METHOD' => 'POST' } }
120
+
121
+ it { should_not be_get }
122
+ end
123
+
124
+ context 'DELETE request' do
125
+ let(:env) { { 'REQUEST_METHOD' => 'DELETE' } }
126
+
127
+ it { should_not be_get }
128
+ end
129
+
130
+ context 'PATCH request' do
131
+ let(:env) { { 'REQUEST_METHOD' => 'PATCH' } }
132
+
133
+ it { should_not be_get }
134
+ end
135
+ end
136
+ end
137
+
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'nokogiri'
3
+
4
+ describe Rack::LiveReload do
5
+ let(:middleware) { described_class.new(app, options) }
6
+ let(:app) { stub }
7
+
8
+ subject { middleware }
9
+
10
+ it 'should be an app' do
11
+ middleware.app.should be == app
12
+ end
13
+
14
+ let(:env) { {} }
15
+ let(:options) { {} }
16
+
17
+ context '/__rack/livereload.js' do
18
+ let(:env) { { 'PATH_INFO' => described_class::BodyProcessor::LIVERELOAD_JS_PATH } }
19
+
20
+ before do
21
+ middleware.expects(:deliver_file).returns(true)
22
+ end
23
+
24
+ it 'should return the js file' do
25
+ middleware._call(env).should be_truthy
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,16 @@
1
+ require 'mocha/api'
2
+ require 'webmock/rspec'
3
+
4
+ require 'rack-livereload'
5
+
6
+ RSpec.configure do |c|
7
+ c.mock_with :mocha
8
+ end
9
+
10
+ module RSpec::Matchers
11
+ define :use_vendored do
12
+ match do |subject|
13
+ subject.use_vendored?
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,299 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leifcr-rack-livereload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.16
5
+ platform: ruby
6
+ authors:
7
+ - John Bintz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cucumber
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: shotgun
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thin
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: mocha
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: guard-rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: guard-cucumber
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: guard-livereload
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: webmock
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: nokogiri
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: appraisal
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '0.4'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '0.4'
223
+ - !ruby/object:Gem::Dependency
224
+ name: rack
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ">="
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ description: Insert LiveReload into your app easily as Rack middleware
238
+ email:
239
+ - john@coswellproductions.com
240
+ executables: []
241
+ extensions: []
242
+ extra_rdoc_files: []
243
+ files:
244
+ - ".gitignore"
245
+ - ".travis.yml"
246
+ - Appraisals
247
+ - Gemfile
248
+ - Guardfile
249
+ - LICENSE
250
+ - README.md
251
+ - Rakefile
252
+ - config.ru
253
+ - features/skip_certain_browsers.feature
254
+ - features/step_definitions/given/i_have_a_rack_app_with_live_reload.rb
255
+ - features/step_definitions/then/i_should_not_have_livereload_code.rb
256
+ - features/step_definitions/when/i_make_a_request_with_headers.rb
257
+ - features/support/env.rb
258
+ - gemfiles/rails32.gemfile
259
+ - gemfiles/rails40.gemfile
260
+ - index.html
261
+ - js/WebSocketMain.swf
262
+ - js/livereload.js
263
+ - js/swfobject.js
264
+ - js/web_socket.js
265
+ - lib/rack-livereload.rb
266
+ - lib/rack/livereload.rb
267
+ - lib/rack/livereload/body_processor.rb
268
+ - lib/rack/livereload/processing_skip_analyzer.rb
269
+ - rack-livereload.gemspec
270
+ - skel/livereload.html.erb
271
+ - spec/rack/livereload/body_processor_spec.rb
272
+ - spec/rack/livereload/processing_skip_analyzer_spec.rb
273
+ - spec/rack/livereload_spec.rb
274
+ - spec/spec_helper.rb
275
+ homepage: ''
276
+ licenses:
277
+ - MIT
278
+ metadata: {}
279
+ post_install_message:
280
+ rdoc_options: []
281
+ require_paths:
282
+ - lib
283
+ required_ruby_version: !ruby/object:Gem::Requirement
284
+ requirements:
285
+ - - ">="
286
+ - !ruby/object:Gem::Version
287
+ version: '0'
288
+ required_rubygems_version: !ruby/object:Gem::Requirement
289
+ requirements:
290
+ - - ">="
291
+ - !ruby/object:Gem::Version
292
+ version: '0'
293
+ requirements: []
294
+ rubyforge_project: rack-livereload
295
+ rubygems_version: 2.5.2
296
+ signing_key:
297
+ specification_version: 4
298
+ summary: Insert LiveReload into your app easily as Rack middleware
299
+ test_files: []