browser 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/gemfiles/rails3.gemfile.lock +1 -1
- data/lib/browser.rb +21 -4
- data/lib/browser/middleware.rb +14 -2
- data/lib/browser/version.rb +1 -1
- data/test/middleware_test.rb +10 -4
- data/test/sample_app.rb +4 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0373ece943efe30b86eea58744eaa628c2a682c9
|
4
|
+
data.tar.gz: 29276aa59fd5d2a8f7ef066fcd14ed911d8bbea2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 470c3d3f2e2f521adf0734c6f4e9ee28da7ff29ceb946d0d513c0c818eab4d04b6c85758078d246b5a9c9d47923f6d71fc60e96a797d329b0e6edacf99ddd5a4
|
7
|
+
data.tar.gz: 48bf2e3967f5304d090c017b74732eba63fcc1e5bbd4796e7a1ede411f38f0d315e64d700f923fe2040374b8ea731f666ee692e1db50ba3b3c35980c629382c1
|
data/Gemfile.lock
CHANGED
data/lib/browser.rb
CHANGED
@@ -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
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
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
|
data/lib/browser/middleware.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/browser/version.rb
CHANGED
data/test/middleware_test.rb
CHANGED
@@ -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
|
data/test/sample_app.rb
CHANGED
@@ -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
|