MM-53023: Add origin client to ObserveAPIEndpointDuration (#23631)

* Add origin device to ObserveAPIEndpointDuration

* Fix generation of einterfaces mocks

* make einterfaces-mocks

* Use request's query and headers to get origin

* Add desktop to the origin device identification

* Test originDevice function

* Rename origin device to origin client
Этот коммит содержится в:
Alejandro García Montoro
2023-10-12 19:09:52 +02:00
коммит произвёл GitHub
родитель 1fe2295c70
Коммит 2bc99398f9
4 изменённых файлов: 116 добавлений и 5 удалений

Просмотреть файл

@@ -882,3 +882,71 @@ func TestCheckCSRFToken(t *testing.T) {
assert.Nil(t, c.Err)
})
}
func TestOriginClient(t *testing.T) {
testCases := []struct {
name string
userAgent string
mobilev2 bool
expectedClient OriginClient
}{
{
name: "No user agent - unknown client",
userAgent: "",
expectedClient: OriginClientUnknown,
},
{
name: "Mozilla user agent",
userAgent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
expectedClient: OriginClientWeb,
},
{
name: "Chrome user agent",
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
expectedClient: OriginClientWeb,
},
{
name: "Mobile post v2",
userAgent: "someother-agent/3.2.4",
mobilev2: true,
expectedClient: OriginClientMobile,
},
{
name: "Mobile Android",
userAgent: "rnbeta/2.0.0.441 someother-agent/3.2.4",
expectedClient: OriginClientMobile,
},
{
name: "Mobile iOS",
userAgent: "Mattermost/2.0.0.441 someother-agent/3.2.4",
expectedClient: OriginClientMobile,
},
{
name: "Desktop user agent",
userAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.5481.177 Electron/23.1.2 Safari/537.36 Mattermost/5.3.1",
expectedClient: OriginClientDesktop,
},
}
for _, tc := range testCases {
req, err := http.NewRequest(http.MethodGet, "example.com", nil)
require.NoError(t, err)
// Set User-Agent header, if any
if tc.userAgent != "" {
req.Header.Set("User-Agent", tc.userAgent)
}
// Set mobilev2 query if needed
if tc.mobilev2 {
q := req.URL.Query()
q.Add("mobilev2", "true")
req.URL.RawQuery = q.Encode()
}
// Compute origin client
actualClient := originClient(req)
require.Equal(t, tc.expectedClient, actualClient)
}
}