1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 17:44:59 +09:00

GO-677 fix lint

This commit is contained in:
kirillston 2022-12-21 14:02:01 +01:00
parent 1194b4cba4
commit 7a9a9f4345
No known key found for this signature in database
GPG key ID: 88218A7F1109754B
3 changed files with 45 additions and 38 deletions

View file

@ -126,11 +126,11 @@ func (l *linkPreview) makeNonHtml(fetchUrl string, resp *http.Response) (i model
} else {
i.Type = model.LinkPreview_Unknown
}
pUrl, e := uri.ValidateAndParseURI(fetchUrl)
pURL, e := uri.ValidateAndParseURI(fetchUrl)
if e == nil {
pUrl.Path = "favicon.ico"
pUrl.RawQuery = ""
i.FaviconUrl = pUrl.String()
pURL.Path = "favicon.ico"
pURL.RawQuery = ""
i.FaviconUrl = pURL.String()
}
return
}

View file

@ -18,8 +18,8 @@ var (
winFilepathPrefixRegex = regexp.MustCompile(`^[a-zA-Z]:[\\\/]`)
// errors
urlEmptyError = fmt.Errorf("url is empty")
filepathNotSupportedError = fmt.Errorf("filepath not supported")
errUrlEmpty = fmt.Errorf("url is empty")
errFilepathNotSupported = fmt.Errorf("filepath not supported")
)
func ValidateURI(uri string) error {
@ -38,16 +38,20 @@ func ValidateURI(uri string) error {
}
func ParseURI(uri string) *url.URL {
u, _ := url.Parse(uri)
u, err := url.Parse(uri)
if err != nil {
// do nothing as validation is implemented in ValidateAndParseURI
}
return u
}
func NormalizeURI(uri string) string {
if noPrefixEmailRegexp.MatchString(uri) {
switch {
case noPrefixEmailRegexp.MatchString(uri):
return "mailto:" + uri
} else if noPrefixTelRegexp.MatchString(uri) {
case noPrefixTelRegexp.MatchString(uri):
return "tel:" + uri
} else if noPrefixHttpRegex.MatchString(uri) {
case noPrefixHttpRegex.MatchString(uri):
return "http://" + uri
}
return uri
@ -56,12 +60,15 @@ func NormalizeURI(uri string) string {
func ValidateAndParseURI(uri string) (*url.URL, error) {
uri = strings.TrimSpace(uri)
if len(uri) == 0 {
return nil, urlEmptyError
} else if winFilepathPrefixRegex.MatchString(uri) {
return nil, filepathNotSupportedError
} else if strings.HasPrefix(uri, string(os.PathSeparator)) || strings.HasPrefix(uri, ".") {
return nil, filepathNotSupportedError
switch {
case len(uri) == 0:
return nil, errUrlEmpty
case winFilepathPrefixRegex.MatchString(uri):
return nil, errFilepathNotSupported
case strings.HasPrefix(uri, string(os.PathSeparator)):
return nil, errFilepathNotSupported
case strings.HasPrefix(uri, "."):
return nil, errFilepathNotSupported
}
u, err := url.Parse(uri)

View file

@ -9,56 +9,56 @@ import (
func TestURI_NormalizeURI(t *testing.T) {
t.Run("should process mailto uri", func(t *testing.T) {
uri := "john@doe.com"
processedUri := NormalizeURI(uri)
assert.Equal(t, "mailto:"+uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, "mailto:"+uri, processedURI)
})
t.Run("should process tel uri", func(t *testing.T) {
uri := "+491234567"
processedUri := NormalizeURI(uri)
assert.Equal(t, "tel:"+uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, "tel:"+uri, processedURI)
})
t.Run("should process url", func(t *testing.T) {
uri := "website.com"
processedUri := NormalizeURI(uri)
assert.Equal(t, "http://"+uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, "http://"+uri, processedURI)
})
t.Run("should process url with additional content 1", func(t *testing.T) {
uri := "website.com/123/456"
processedUri := NormalizeURI(uri)
assert.Equal(t, "http://"+uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, "http://"+uri, processedURI)
})
t.Run("should process url with additional content 2", func(t *testing.T) {
uri := "website.com?content=11"
processedUri := NormalizeURI(uri)
assert.Equal(t, "http://"+uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, "http://"+uri, processedURI)
})
t.Run("should process url with additional content and numbers", func(t *testing.T) {
uri := "webs1te.com/123/456"
processedUri := NormalizeURI(uri)
assert.Equal(t, "http://"+uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, "http://"+uri, processedURI)
})
t.Run("should not modify url with http://", func(t *testing.T) {
uri := "http://website.com"
processedUri := NormalizeURI(uri)
assert.Equal(t, uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, uri, processedURI)
})
t.Run("should not modify url with https://", func(t *testing.T) {
uri := "https://website.com"
processedUri := NormalizeURI(uri)
assert.Equal(t, uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, uri, processedURI)
})
t.Run("should not modify non url/tel/mailto uri", func(t *testing.T) {
uri := "type:content"
processedUri := NormalizeURI(uri)
assert.Equal(t, uri, processedUri)
processedURI := NormalizeURI(uri)
assert.Equal(t, uri, processedURI)
})
}
@ -67,28 +67,28 @@ func TestURI_ValidateURI(t *testing.T) {
uri := ""
err := ValidateURI(uri)
assert.Error(t, err)
assert.Equal(t, err, urlEmptyError)
assert.Equal(t, err, errUrlEmpty)
})
t.Run("should return error on win filepath", func(t *testing.T) {
uri := "D://folder//file.txt"
err := ValidateURI(uri)
assert.Error(t, err)
assert.Equal(t, err, filepathNotSupportedError)
assert.Equal(t, err, errFilepathNotSupported)
})
t.Run("should return error on unix abs filepath", func(t *testing.T) {
uri := "/folder/file.txt"
err := ValidateURI(uri)
assert.Error(t, err)
assert.Equal(t, err, filepathNotSupportedError)
assert.Equal(t, err, errFilepathNotSupported)
})
t.Run("should return error on unix rel filepath", func(t *testing.T) {
uri := "../folder/file.txt"
err := ValidateURI(uri)
assert.Error(t, err)
assert.Equal(t, err, filepathNotSupportedError)
assert.Equal(t, err, errFilepathNotSupported)
})
t.Run("should not return error if url is surrounded by whitespaces", func(t *testing.T) {