github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/app/user_agent_test.go (about)

     1  package app
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/avct/uasurfer"
     8  )
     9  
    10  type testUserAgent struct {
    11  	Name      string
    12  	UserAgent string
    13  }
    14  
    15  var testUserAgents = []testUserAgent{
    16  	{"Mozilla 40.1", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"},
    17  	{"Chrome 60", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"},
    18  	{"Chrome Mobile", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Mobile Safari/537.36"},
    19  	{"MM Classic App", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR6.170623.013; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/61.0.3163.81 Mobile Safari/537.36 Web-Atoms-Mobile-WebView"},
    20  	{"MM App 3.7.1", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Mattermost/3.7.1 Chrome/56.0.2924.87 Electron/1.6.11 Safari/537.36"},
    21  	{"Franz 4.0.4", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Franz/4.0.4 Chrome/52.0.2743.82 Electron/1.3.1 Safari/537.36"},
    22  	{"Edge 14", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"},
    23  	{"Internet Explorer 9", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0"},
    24  	{"Internet Explorer 11", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"},
    25  	{"Internet Explorer 11 2", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0; rv:11.0) like Gecko"},
    26  	{"Internet Explorer 11 (Compatibility Mode) 1", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; .NET CLR 1.1.4322; InfoPath.3; Zoom 3.6.0)"},
    27  	{"Internet Explorer 11 (Compatibility Mode) 2", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0)"},
    28  	{"Safari 9", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38"},
    29  	{"Safari 8", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12"},
    30  	{"Safari Mobile", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B137 Safari/601.1"},
    31  }
    32  
    33  func TestGetPlatformName(t *testing.T) {
    34  	expected := []string{
    35  		"Windows",
    36  		"Macintosh",
    37  		"Linux",
    38  		"Linux",
    39  		"Macintosh",
    40  		"Macintosh",
    41  		"Windows",
    42  		"Windows",
    43  		"Windows",
    44  		"Windows",
    45  		"Windows",
    46  		"Windows",
    47  		"Macintosh",
    48  		"Macintosh",
    49  		"iPhone",
    50  	}
    51  
    52  	for i, userAgent := range testUserAgents {
    53  		t.Run(fmt.Sprintf("GetPlatformName_%v", i), func(t *testing.T) {
    54  			ua := uasurfer.Parse(userAgent.UserAgent)
    55  
    56  			if actual := getPlatformName(ua); actual != expected[i] {
    57  				t.Fatalf("%v Got %v, expected %v", userAgent.Name, actual, expected[i])
    58  			}
    59  		})
    60  	}
    61  }
    62  
    63  func TestGetOSName(t *testing.T) {
    64  	expected := []string{
    65  		"Windows 7",
    66  		"Mac OS",
    67  		"Android",
    68  		"Android",
    69  		"Mac OS",
    70  		"Mac OS",
    71  		"Windows 10",
    72  		"Windows",
    73  		"Windows 10",
    74  		"Windows 10",
    75  		"Windows 10",
    76  		"Windows 10",
    77  		"Mac OS",
    78  		"Mac OS",
    79  		"iOS",
    80  	}
    81  
    82  	for i, userAgent := range testUserAgents {
    83  		t.Run(fmt.Sprintf("GetOSName_%v", i), func(t *testing.T) {
    84  			ua := uasurfer.Parse(userAgent.UserAgent)
    85  
    86  			if actual := getOSName(ua); actual != expected[i] {
    87  				t.Fatalf("Got %v, expected %v", actual, expected[i])
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  func TestGetBrowserName(t *testing.T) {
    94  	expected := []string{
    95  		"Firefox",
    96  		"Chrome",
    97  		"Chrome",
    98  		"Chrome",
    99  		"Desktop App",
   100  		"Chrome",
   101  		"Edge",
   102  		"Internet Explorer",
   103  		"Internet Explorer",
   104  		"Internet Explorer",
   105  		"Internet Explorer",
   106  		"Internet Explorer",
   107  		"Safari",
   108  		"Safari",
   109  		"Safari",
   110  	}
   111  
   112  	for i, userAgent := range testUserAgents {
   113  		t.Run(fmt.Sprintf("GetBrowserName_%v", i), func(t *testing.T) {
   114  			ua := uasurfer.Parse(userAgent.UserAgent)
   115  
   116  			if actual := getBrowserName(ua, userAgent.UserAgent); actual != expected[i] {
   117  				t.Fatalf("Got %v, expected %v", actual, expected[i])
   118  			}
   119  		})
   120  	}
   121  }
   122  
   123  func TestGetBrowserVersion(t *testing.T) {
   124  	expected := []string{
   125  		"40.1",
   126  		"60.0.3112", // Doesn't report the fourth part of the version
   127  		"60.0.3112", // Doesn't report the fourth part of the version
   128  		"61.0.3163",
   129  		"3.7.1",
   130  		"4.0.4",
   131  		"14.14393",
   132  		"9.0",
   133  		"11.0",
   134  		"11.0",
   135  		"7.0",
   136  		"7.0",
   137  		"11.0",
   138  		"8.0.7",
   139  		"9.0",
   140  	}
   141  
   142  	for i, userAgent := range testUserAgents {
   143  		t.Run(fmt.Sprintf("GetBrowserVersion_%v", i), func(t *testing.T) {
   144  			ua := uasurfer.Parse(userAgent.UserAgent)
   145  
   146  			if actual := getBrowserVersion(ua, userAgent.UserAgent); actual != expected[i] {
   147  				t.Fatalf("Got %v, expected %v", actual, expected[i])
   148  			}
   149  		})
   150  	}
   151  }