Skip to content

Commit

Permalink
New methods to get just UA string
Browse files Browse the repository at this point in the history
  • Loading branch information
baditaflorin committed May 7, 2024
1 parent b68c802 commit 8639114
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ mobileAgents := commonuseragent.GetAllMobile()
To get a random desktop user agent:

```go
randomDesktop := commonuseragent.GetRandomDesktop()
randomDesktop := commonuseragent.GetRandomDesktopUA()
```

### Getting a Random Mobile User Agent

To get a random mobile user agent:

```go
randomMobile := commonuseragent.GetRandomMobile()
randomMobile := commonuseragent.GetRandomMobileUA()
```

### Getting Any Random User Agent

To get a random user agent from either the desktop or mobile lists:

```go
randomUserAgent := commonuseragent.GetRandomUserAgent()
randomUserAgent := commonuseragent.GetRandomUA()
```

## Contributing
Expand Down Expand Up @@ -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.
1 change: 1 addition & 0 deletions useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
}
}

0 comments on commit 8639114

Please sign in to comment.