From 863911466597659d3d61df6b633f0f03abc971d7 Mon Sep 17 00:00:00 2001 From: Florin Badita-Nistor Date: Tue, 7 May 2024 12:26:51 +0300 Subject: [PATCH] New methods to get just UA string --- README.md | 8 +++++--- useragent.go | 1 + useragent_test.go | 20 ++++++++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 02bd21e..81d477e 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ mobileAgents := commonuseragent.GetAllMobile() To get a random desktop user agent: ```go -randomDesktop := commonuseragent.GetRandomDesktop() +randomDesktop := commonuseragent.GetRandomDesktopUA() ``` ### Getting a Random Mobile User Agent @@ -57,7 +57,7 @@ randomDesktop := commonuseragent.GetRandomDesktop() To get a random mobile user agent: ```go -randomMobile := commonuseragent.GetRandomMobile() +randomMobile := commonuseragent.GetRandomMobileUA() ``` ### Getting Any Random User Agent @@ -65,7 +65,7 @@ randomMobile := commonuseragent.GetRandomMobile() To get a random user agent from either the desktop or mobile lists: ```go -randomUserAgent := commonuseragent.GetRandomUserAgent() +randomUserAgent := commonuseragent.GetRandomUA() ``` ## Contributing @@ -95,4 +95,6 @@ git ls-remote --tags origin ## License +UserAgent data comes from https://useragents.me/ + This project is licensed under the MIT License - see the LICENSE file for details. diff --git a/useragent.go b/useragent.go index f812114..dd62884 100644 --- a/useragent.go +++ b/useragent.go @@ -71,6 +71,7 @@ func GetRandomDesktopUA() string { func GetRandomMobileUA() string { return GetRandomMobile().UA } + func GetRandomUA() string { allAgents := append(desktopAgents, mobileAgents...) return allAgents[rand.Intn(len(allAgents))].UA diff --git a/useragent_test.go b/useragent_test.go index 91e4fab..e5e020c 100644 --- a/useragent_test.go +++ b/useragent_test.go @@ -26,6 +26,14 @@ func TestGetRandomDesktop(t *testing.T) { } } +func TestGetRandomDesktopUA(t *testing.T) { + // Calling the function to test + result := GetRandomDesktopUA() + if result == "" { + t.Errorf("GetRandomDesktop returned an empty user agent") + } +} + func TestGetRandomMobile(t *testing.T) { // Calling the function to test result := GetRandomMobile() @@ -34,9 +42,17 @@ func TestGetRandomMobile(t *testing.T) { } } +func TestGetRandomMobileUA(t *testing.T) { + // Calling the function to test + result := GetRandomMobileUA() + if result == "" { + t.Errorf("GetRandomMobile returned an empty user agent") + } +} + func TestGetRandomUserAgent(t *testing.T) { - result := GetRandomUserAgent() - if result.UA == "" { + result := GetRandomUA() + if result == "" { t.Errorf("GetRandomUserAgent returned an empty user agent") } }