browser 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a021a6e70a34b34a47cf016fc88851dec3aaebe
4
- data.tar.gz: f76665580a4eed8cfeb95fb1a67c8424aeacdeaf
3
+ metadata.gz: 0373ece943efe30b86eea58744eaa628c2a682c9
4
+ data.tar.gz: 29276aa59fd5d2a8f7ef066fcd14ed911d8bbea2
5
5
  SHA512:
6
- metadata.gz: f2f700870abd8cddd6b9598699eee7547c6c5dc3e18d55bceedf32dc482e537b927136d295204fcc674a04da4c71e5f092c31ee43e08b562ce331336cba1e3b2
7
- data.tar.gz: ac41b49b5e3ca509a40245bddde88838a2bffde851816f70e2e463ce0793a30b1e232383ae92b3efcc79aaa41db82b616c9cab6ff1c59f5964649acd0ac11f0b
6
+ metadata.gz: 470c3d3f2e2f521adf0734c6f4e9ee28da7ff29ceb946d0d513c0c818eab4d04b6c85758078d246b5a9c9d47923f6d71fc60e96a797d329b0e6edacf99ddd5a4
7
+ data.tar.gz: 48bf2e3967f5304d090c017b74732eba63fcc1e5bbd4796e7a1ede411f38f0d315e64d700f923fe2040374b8ea731f666ee692e1db50ba3b3c35980c629382c1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- browser (0.3.0)
4
+ browser (0.3.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- browser (0.3.0)
4
+ browser (0.3.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -106,10 +106,10 @@ class Browser
106
106
  # Return true if browser is modern (Webkit, Firefox 17+, IE9+, Opera 12+).
107
107
  def modern?
108
108
  webkit? ||
109
- (firefox? && version.to_i >= 17) ||
110
- (ie? && version.to_i >= 9) ||
111
- (opera? && version.to_i >= 12) ||
112
- (firefox? && tablet? && android? && version.to_i >= 14)
109
+ newer_firefox? ||
110
+ newer_ie? ||
111
+ newer_opera? ||
112
+ newer_firefox_tablet?
113
113
  end
114
114
 
115
115
  # Detect if browser is WebKit-based.
@@ -171,4 +171,21 @@ class Browser
171
171
  def to_s
172
172
  meta.to_a.join(" ")
173
173
  end
174
+
175
+ private
176
+ def newer_firefox?
177
+ firefox? && version.to_i >= 17
178
+ end
179
+
180
+ def newer_ie?
181
+ ie? && version.to_i >= 9
182
+ end
183
+
184
+ def newer_opera?
185
+ opera? && version.to_i >= 12
186
+ end
187
+
188
+ def newer_firefox_tablet?
189
+ firefox? && tablet? && android? && version.to_i >= 14
190
+ end
174
191
  end
@@ -12,14 +12,22 @@ class Browser
12
12
  def call(env)
13
13
  request = Rack::Request.new(env)
14
14
 
15
+ # Only apply verification on HTML requests.
16
+ # This ensures that images, CSS and JavaScript
17
+ # will be rendered.
18
+ return run_app(env) unless html?(request)
19
+
15
20
  path = catch(:redirected) do
16
21
  Context.new(request).instance_eval(&@block)
17
22
  end
18
23
 
19
- path ? resolve_redirection(request.path, path) : run_app(env)
24
+ # No path, no match.
25
+ return run_app(env) unless path
26
+
27
+ resolve_redirection(env, request.path, path)
20
28
  end
21
29
 
22
- def resolve_redirection(current_path, path)
30
+ def resolve_redirection(env, current_path, path)
23
31
  uri = URI.parse(path)
24
32
 
25
33
  if uri.path == current_path
@@ -36,5 +44,9 @@ class Browser
36
44
  def run_app(env)
37
45
  @app.call(env)
38
46
  end
47
+
48
+ def html?(request)
49
+ request.env["HTTP_ACCEPT"].to_s.include?("text/html")
50
+ end
39
51
  end
40
52
  end
@@ -2,7 +2,7 @@ class Browser
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 3
5
- PATCH = 0
5
+ PATCH = 1
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -10,28 +10,34 @@ class MiddlewareTest < Test::Unit::TestCase
10
10
  end
11
11
 
12
12
  def test_redirect_uses_302
13
- get "/", {}, {"HTTP_USER_AGENT" => "MSIE 6"}
13
+ get "/", {}, {"HTTP_USER_AGENT" => "MSIE 6", "HTTP_ACCEPT" => "text/html"}
14
14
  assert_equal 302, last_response.status
15
15
  end
16
16
 
17
17
  def test_redirect_ie6_to_upgrade_path
18
- get "/", {}, {"HTTP_USER_AGENT" => "MSIE 6"}
18
+ get "/", {}, {"HTTP_USER_AGENT" => "MSIE 6", "HTTP_ACCEPT" => "text/html"}
19
19
  follow_redirect!
20
20
 
21
21
  assert_equal "UPGRADE: ie6", last_response.body
22
22
  end
23
23
 
24
24
  def test_redirect_ie7_to_upgrade_path
25
- get "/", {}, {"HTTP_USER_AGENT" => "MSIE 7"}
25
+ get "/", {}, {"HTTP_USER_AGENT" => "MSIE 7", "HTTP_ACCEPT" => "text/html"}
26
26
  follow_redirect!
27
27
 
28
28
  assert_equal "UPGRADE: ie7", last_response.body
29
29
  end
30
30
 
31
31
  def test_redirect_ie8_and_404
32
- get "/", {}, {"HTTP_USER_AGENT" => "MSIE 8"}
32
+ get "/", {}, {"HTTP_USER_AGENT" => "MSIE 8", "HTTP_ACCEPT" => "text/html"}
33
33
  follow_redirect!
34
34
 
35
35
  assert_equal 404, last_response.status
36
36
  end
37
+
38
+ def test_ignores_non_html_requests
39
+ get "/", {}, {"HTTP_USER_AGENT" => "MSIE 6", "HTTP_ACCEPT" => "image/png"}
40
+
41
+ assert_equal 200, last_response.status
42
+ end
37
43
  end
@@ -15,6 +15,10 @@ class SampleApp < Rails::Application
15
15
  browser = Rack::Request.new(env).params["browser"]
16
16
  [200, default_headers, ["UPGRADE: #{browser}"]]
17
17
  }, as: "upgrade"
18
+
19
+ get "/asset", to: -> env {
20
+ [200, {"Content-Type" => "image/png"}, []]
21
+ }
18
22
  end
19
23
 
20
24
  config.middleware.use Browser::Middleware do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira