Skip to content

Commit

Permalink
Use \A and \z for extra correctness.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Sep 5, 2023
1 parent a393a49 commit 1ae303f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
28 changes: 14 additions & 14 deletions spec/chrome_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
describe ".random" do
it "must return a random Chrome User-Agent string" do
expect(subject.random).to match(
%r{^Mozilla/5\.0 \([^\)]+(?:\(\d+\)[^\)]+)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{Mozilla/5\.0 \([^\)]+(?:\(\d+\)[^\)]+)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end

Expand All @@ -383,7 +383,7 @@

it "must return a random Windows Chrome User-Agent string" do
expect(subject.random(os: os)).to match(
%r{^Mozilla/5\.0 \(Windows NT \d+(?:\.\d+)*(?:; (?:WOW64|Win64; x64))?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Windows NT \d+(?:\.\d+)*(?:; (?:WOW64|Win64; x64))?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end

Expand All @@ -392,7 +392,7 @@

it "must return a random Windows Chrome User-Agent string for that version of Windows" do
expect(subject.random(os: os, os_version: os_version)).to match(
%r{^Mozilla/5\.0 \(Windows NT 10\.0(?:; (?:WOW64|Win64; x64))?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Windows NT 10\.0(?:; (?:WOW64|Win64; x64))?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand All @@ -402,7 +402,7 @@

it "must return a random Windows Chrome User-Agent string for the WOW64 architecture" do
expect(subject.random(os: os, arch: arch)).to match(
%r{^Mozilla/5\.0 \(Windows NT \d+(?:\.\d+)*; WOW64\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Windows NT \d+(?:\.\d+)*; WOW64\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand All @@ -412,7 +412,7 @@

it "must return a random Windows Chrome User-Agent string for the WOW64 architecture" do
expect(subject.random(os: os, arch: arch)).to match(
%r{^Mozilla/5\.0 \(Windows NT \d+(?:\.\d+)*; Win64; x64\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Windows NT \d+(?:\.\d+)*; Win64; x64\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand All @@ -423,7 +423,7 @@

it "must return a macOS Chrome User-Agent string" do
expect(subject.random(os: os)).to match(
%r{^Mozilla/5\.0 \(Macintosh; Intel Mac OS X \d+(_\d+){1,2}\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Macintosh; Intel Mac OS X \d+(_\d+){1,2}\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end

Expand All @@ -432,7 +432,7 @@

it "must return a macOS Chrome User-Agent string for that macOS version" do
expect(subject.random(os: os, os_version: os_version)).to match(
%r{^Mozilla/5\.0 \(Macintosh; Intel Mac OS X 10_11_12\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Macintosh; Intel Mac OS X 10_11_12\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand All @@ -443,7 +443,7 @@

it "must return a Linux Chrome User-Agent string" do
expect(subject.random(os: os)).to match(
%r{^Mozilla/5\.0 \(X11(?:; (?:Ubuntu|Fedora|Arch))?; Linux (?:x86_64|aarch64|i686)\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(X11(?:; (?:Ubuntu|Fedora|Arch))?; Linux (?:x86_64|aarch64|i686)\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end

Expand All @@ -452,7 +452,7 @@

it "must return a Linux Chrome User-Agent string for that Linux Distro" do
expect(subject.random(os: os, linux_distro: linux_distro)).to match(
%r{^Mozilla/5\.0 \(X11; Ubuntu; Linux (?:x86_64|aarch64|i686)\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(X11; Ubuntu; Linux (?:x86_64|aarch64|i686)\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand All @@ -462,7 +462,7 @@

it "must return a Linux Chrome User-Agent string for that architecture" do
expect(subject.random(os: os, arch: arch)).to match(
%r{^Mozilla/5\.0 \(X11(?:; (?:Ubuntu|Fedora|Arch))?; Linux aarch64\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(X11(?:; (?:Ubuntu|Fedora|Arch))?; Linux aarch64\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand All @@ -473,7 +473,7 @@

it "must return a Android Chrome User-Agent string" do
expect(subject.random(os: os)).to match(
%r{^Mozilla/5\.0 \(Linux(?:; (?:arm|arm_64))?; Android \d+(?:\.\d+)*(?:; [^\(\)]+(?:\([^\)]+\)[^\(\)]+)?)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Linux(?:; (?:arm|arm_64))?; Android \d+(?:\.\d+)*(?:; [^\(\)]+(?:\([^\)]+\)[^\(\)]+)?)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end

Expand All @@ -482,7 +482,7 @@

it "must return a Android Chrome User-Agent string for that Android version" do
expect(subject.random(os: os, os_version: os_version)).to match(
%r{^Mozilla/5\.0 \(Linux(?:; (?:arm|arm_64))?; Android 8\.1(?:; [^\(\)]+(?:\([^\)]+\)[^\(\)]+)?)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Linux(?:; (?:arm|arm_64))?; Android 8\.1(?:; [^\(\)]+(?:\([^\)]+\)[^\(\)]+)?)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand All @@ -492,7 +492,7 @@

it "must return a Linux Chrome User-Agent string for that architecture" do
expect(subject.random(os: os, arch: arch)).to match(
%r{^Mozilla/5\.0 \(Linux; arm_64; Android (?:\d+(?:\.\d+)*)(?:; [^\(\)]+(?:\([^\)]+\)[^\(\)]+)?)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Linux; arm_64; Android (?:\d+(?:\.\d+)*)(?:; [^\(\)]+(?:\([^\)]+\)[^\(\)]+)?)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand All @@ -502,7 +502,7 @@

it "must return a Linux Chrome User-Agent string with that Android device" do
expect(subject.random(os: os, android_device: android_device)).to match(
%r{^Mozilla/5\.0 \(Linux(?:; (?:arm|arm_64))?; Android (?:\d+(?:\.\d+)*); #{android_device}\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \(Linux(?:; (?:arm|arm_64))?; Android (?:\d+(?:\.\d+)*); #{android_device}\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
)
end
end
Expand Down
24 changes: 12 additions & 12 deletions spec/firefox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
describe ".random" do
it "must return a random Chrome User-Agent string" do
expect(subject.random).to match(
%r{^Mozilla/5\.0 \([^\)]+\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \([^\)]+\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end

Expand All @@ -306,7 +306,7 @@

it "must return a random Windows Chrome User-Agent string" do
expect(subject.random(os: os)).to match(
%r{^Mozilla/5\.0 \(Windows NT \d+(?:\.\d+)*(?:; (?:WOW64|Win64; x64))?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(Windows NT \d+(?:\.\d+)*(?:; (?:WOW64|Win64; x64))?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end

Expand All @@ -315,7 +315,7 @@

it "must return a random Windows Chrome User-Agent string for that version of Windows" do
expect(subject.random(os: os, os_version: os_version)).to match(
%r{^Mozilla/5\.0 \(Windows NT 10\.0(?:; (?:WOW64|Win64; x64))?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(Windows NT 10\.0(?:; (?:WOW64|Win64; x64))?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end
end
Expand All @@ -325,7 +325,7 @@

it "must return a random Windows Chrome User-Agent string for the WOW64 architecture" do
expect(subject.random(os: os, arch: arch)).to match(
%r{^Mozilla/5\.0 \(Windows NT \d+(?:\.\d+)*; WOW64; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(Windows NT \d+(?:\.\d+)*; WOW64; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end
end
Expand All @@ -335,7 +335,7 @@

it "must return a random Windows Chrome User-Agent string for the WOW64 architecture" do
expect(subject.random(os: os, arch: arch)).to match(
%r{^Mozilla/5\.0 \(Windows NT \d+(?:\.\d+)*; Win64; x64; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(Windows NT \d+(?:\.\d+)*; Win64; x64; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end
end
Expand All @@ -346,7 +346,7 @@

it "must return a macOS Chrome User-Agent string" do
expect(subject.random(os: os)).to match(
%r{^Mozilla/5\.0 \(Macintosh; Intel Mac OS X \d+(\.\d+){1,2}; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(Macintosh; Intel Mac OS X \d+(\.\d+){1,2}; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end

Expand All @@ -355,7 +355,7 @@

it "must return a macOS Chrome User-Agent string for that macOS version" do
expect(subject.random(os: os, os_version: os_version)).to match(
%r{^Mozilla/5\.0 \(Macintosh; Intel Mac OS X 10\.11\.12; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(Macintosh; Intel Mac OS X 10\.11\.12; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end
end
Expand All @@ -366,7 +366,7 @@

it "must return a Linux Chrome User-Agent string" do
expect(subject.random(os: os)).to match(
%r{^Mozilla/5\.0 \(X11(?:; (?:Ubuntu|Fedora|Arch))?; Linux (?:x86_64|aarch64|i686)(?:; [a-z]+(?:-[A-Z]+)?)?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(X11(?:; (?:Ubuntu|Fedora|Arch))?; Linux (?:x86_64|aarch64|i686)(?:; [a-z]+(?:-[A-Z]+)?)?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end

Expand All @@ -375,7 +375,7 @@

it "must return a Linux Chrome User-Agent string for that Linux Distro" do
expect(subject.random(os: os, linux_distro: linux_distro)).to match(
%r{^Mozilla/5\.0 \(X11; Ubuntu; Linux (?:x86_64|aarch64|i686)(?:; [a-z]+(?:-[A-Z]+)?)?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(X11; Ubuntu; Linux (?:x86_64|aarch64|i686)(?:; [a-z]+(?:-[A-Z]+)?)?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end
end
Expand All @@ -385,7 +385,7 @@

it "must return a Linux Chrome User-Agent string for that architecture" do
expect(subject.random(os: os, arch: arch)).to match(
%r{^Mozilla/5\.0 \(X11(?:; (?:Ubuntu|Fedora|Arch))?; Linux aarch64(?:; [a-zA-Z-]+)?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(X11(?:; (?:Ubuntu|Fedora|Arch))?; Linux aarch64(?:; [a-zA-Z-]+)?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end
end
Expand All @@ -396,7 +396,7 @@

it "must return a Android Chrome User-Agent string" do
expect(subject.random(os: os)).to match(
%r{^Mozilla/5\.0 \(Android(?:; (?:Mobile|Tablet))?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(Android(?:; (?:Mobile|Tablet))?; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end

Expand All @@ -405,7 +405,7 @@

it "must return a Linux Firefox User-Agent string with that device type" do
expect(subject.random(os: os, device_type: device_type)).to match(
%r{^Mozilla/5\.0 \(Android; Tablet; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \(Android; Tablet; rv:\d+(?:\.\d+)*\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/user_agents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
describe ".random" do
it "must return a random User-Agent string" do
expect(subject.random).to match(
%r{^Mozilla/5\.0 \([^\)]+(?:\(\d+\)[^\)]+)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36$}
%r{\AMozilla/5\.0 \([^\)]+(?:\(\d+\)[^\)]+)?\) AppleWebKit/537\.36 \(KHTML, like Gecko\) Chrome/\d+(\.\d+)* (?:Mobile )?Safari/537\.36\z}
).or(
match(
%r{^Mozilla/5\.0 \([^\)]+\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*$}
%r{\AMozilla/5\.0 \([^\)]+\) Gecko/(?:20100101|\d+(?:\.\d+)*) Firefox/\d+(\.\d+)*\z}
)
)
end
Expand Down

0 comments on commit 1ae303f

Please sign in to comment.