Update regex and add test coverage for empty/multiple lists

This commit is contained in:
Jay Ohms
2026-03-17 10:45:37 -04:00
parent 139f4cb869
commit 8920946cb2
2 changed files with 11 additions and 1 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ class ApplicationPlatform < PlatformAgent
private
def extract_list_from_native_user_agent(prefix)
if native?
user_agent.to_s.match(/#{prefix}: \[(.+)\]/) { |matches| matches[1] }.to_s
user_agent.to_s.match(/#{Regexp.escape(prefix)}: \[(.*?)\]/) { |matches| matches[1] }.to_s
else
""
end
+10
View File
@@ -6,6 +6,8 @@ class ApplicationPlatformTest < ActiveSupport::TestCase
MOBILE_WEB_IOS_UA = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1"
DESKTOP_WEB_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
NATIVE_WITHOUT_COMPONENTS_UA = "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Hotwire Native Android/1.0"
NATIVE_WITH_EMPTY_COMPONENTS_UA = "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Hotwire Native Android/1.0 bridge-components: []"
NATIVE_WITH_MULTIPLE_LISTS_UA = "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Hotwire Native Android/1.0 bridge-components: [buttons] metadata: [ignored]"
test "bridge_name returns android for native android user agents" do
assert_equal :android, platform_for(NATIVE_ANDROID_UA).bridge_name
@@ -34,6 +36,14 @@ class ApplicationPlatformTest < ActiveSupport::TestCase
assert_equal "", platform_for(NATIVE_WITHOUT_COMPONENTS_UA).bridge_components
end
test "bridge_components supports empty lists" do
assert_equal "", platform_for(NATIVE_WITH_EMPTY_COMPONENTS_UA).bridge_components
end
test "bridge_components only matches through the first closing bracket" do
assert_equal "buttons", platform_for(NATIVE_WITH_MULTIPLE_LISTS_UA).bridge_components
end
private
def platform_for(user_agent)
ApplicationPlatform.new(user_agent)