mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
fix: user agent strign parsing (fix #53)
This commit is contained in:
parent
f843be8d12
commit
2ecbb3ea02
@ -21,7 +21,7 @@ func FormatDateHuman(date time.Time) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ParseUserAgent(ua string) (string, string, error) {
|
func ParseUserAgent(ua string) (string, string, error) {
|
||||||
re := regexp.MustCompile(`^wakatime\/[\d+.]+\s\((\w+).*\)\s.+\s(\w+)\/.+$`)
|
re := regexp.MustCompile(`(?iU)^wakatime\/[\d+.]+\s\((\w+)-.*\)\s.+\s([^\/\s]+)-wakatime\/.+$`)
|
||||||
groups := re.FindAllStringSubmatch(ua, -1)
|
groups := re.FindAllStringSubmatch(ua, -1)
|
||||||
if len(groups) == 0 || len(groups[0]) != 3 {
|
if len(groups) == 0 || len(groups[0]) != 3 {
|
||||||
return "", "", errors.New("failed to parse user agent string")
|
return "", "", errors.New("failed to parse user agent string")
|
||||||
@ -32,7 +32,7 @@ func ParseUserAgent(ua string) (string, string, error) {
|
|||||||
func MakeConnectionString(config *config.Config) string {
|
func MakeConnectionString(config *config.Config) string {
|
||||||
switch config.DbDialect {
|
switch config.DbDialect {
|
||||||
case "mysql":
|
case "mysql":
|
||||||
return mySqlConnectionString(config)
|
return mysqlConnectionString(config)
|
||||||
case "postgres":
|
case "postgres":
|
||||||
return postgresConnectionString(config)
|
return postgresConnectionString(config)
|
||||||
case "sqlite3":
|
case "sqlite3":
|
||||||
@ -41,7 +41,7 @@ func MakeConnectionString(config *config.Config) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func mySqlConnectionString(config *config.Config) string {
|
func mysqlConnectionString(config *config.Config) string {
|
||||||
//location, _ := time.LoadLocation("Local")
|
//location, _ := time.LoadLocation("Local")
|
||||||
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES",
|
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true&loc=%s&sql_mode=ANSI_QUOTES",
|
||||||
config.DbUser,
|
config.DbUser,
|
||||||
|
50
utils/common_test.go
Normal file
50
utils/common_test.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseUserAgent(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in string
|
||||||
|
outOs string
|
||||||
|
outEditor string
|
||||||
|
outError error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"wakatime/13.0.7 (Linux-4.15.0-96-generic-x86_64-with-glibc2.4) Python3.8.0.final.0 GoLand/2019.3.4 GoLand-wakatime/11.0.1",
|
||||||
|
"Linux",
|
||||||
|
"GoLand",
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"wakatime/13.0.4 (Linux-5.4.64-x86_64-with-glibc2.2.5) Python3.7.6.final.0 emacs-wakatime/1.0.2",
|
||||||
|
"Linux",
|
||||||
|
"emacs",
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
errors.New(""),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"wakatime/13.0.7 Python3.8.0.final.0 GoLand/2019.3.4 GoLand-wakatime/11.0.1",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
errors.New(""),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
if os, editor, err := ParseUserAgent(test.in); os != test.outOs || editor != test.outEditor || !checkErr(test.outError, err) {
|
||||||
|
t.Errorf("[%d] Unexpected result of parsing '%s'; got '%v', '%v', '%v'", i, test.in, os, editor, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkErr(expected, actual error) bool {
|
||||||
|
return (expected == nil && actual == nil) || (expected != nil && actual != nil)
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
1.11.2
|
1.11.3
|
Loading…
x
Reference in New Issue
Block a user