@@ -8,10 +8,20 @@ def hello
8
8
head :ok
9
9
end
10
10
11
+ allow_browser versions : { safari : "16.4" , chrome : "119" , firefox : "123" , opera : "106" , ie : false } , block : :head_upgrade_required , only : :hello_method_name
12
+ def hello_method_name
13
+ head :ok
14
+ end
15
+
11
16
allow_browser versions : :modern , block : -> { head :upgrade_required } , only : :modern
12
17
def modern
13
18
head :ok
14
19
end
20
+
21
+ private
22
+ def head_upgrade_required
23
+ head :upgrade_required
24
+ end
15
25
end
16
26
17
27
class AllowBrowserTest < ActionController ::TestCase
@@ -25,11 +35,16 @@ class AllowBrowserTest < ActionController::TestCase
25
35
OPERA_106 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 OPR/106.0.0.0"
26
36
GOOGLE_BOT = "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
27
37
28
- test "blocked browser below version limit" do
38
+ test "blocked browser below version limit with callable " do
29
39
get_with_agent :hello , FIREFOX_114
30
40
assert_response :upgrade_required
31
41
end
32
42
43
+ test "blocked browser below version limit with method name" do
44
+ get_with_agent :hello_method_name , FIREFOX_114
45
+ assert_response :upgrade_required
46
+ end
47
+
33
48
test "blocked browser by name" do
34
49
get_with_agent :hello , IE_11
35
50
assert_response :upgrade_required
@@ -68,9 +83,25 @@ class AllowBrowserTest < ActionController::TestCase
68
83
assert_response :ok
69
84
end
70
85
86
+ test "a blocked request instruments a browser_block.action_controller event" do
87
+ event , *rest = capture_instrumentation_events "browser_block.action_controller" do
88
+ get_with_agent :modern , CHROME_118
89
+ end
90
+
91
+ assert_equal request , event . payload [ :request ]
92
+ assert_not_empty event . payload [ :versions ]
93
+ assert_empty rest
94
+ end
95
+
71
96
private
72
97
def get_with_agent ( action , agent )
73
98
@request . headers [ "User-Agent" ] = agent
74
99
get action
75
100
end
101
+
102
+ def capture_instrumentation_events ( pattern , &block )
103
+ events = [ ]
104
+ ActiveSupport ::Notifications . subscribed ( -> ( e ) { events << e } , pattern , &block )
105
+ events
106
+ end
76
107
end
0 commit comments