Create constraint package
parent
d62af0889e
commit
a70a958ee4
@ -0,0 +1,28 @@
|
||||
package constraint
|
||||
|
||||
import (
|
||||
configapi "github.com/creekorful/trandoshan/internal/configapi/client"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CheckHostnameAllowed check if given URL hostname is allowed for crawling
|
||||
func CheckHostnameAllowed(configClient configapi.Client, rawurl string) (bool, error) {
|
||||
u, err := url.Parse(rawurl)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
forbiddenHostnames, err := configClient.GetForbiddenHostnames()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, hostname := range forbiddenHostnames {
|
||||
if strings.Contains(u.Hostname(), hostname.Hostname) {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package constraint
|
||||
|
||||
import (
|
||||
"github.com/creekorful/trandoshan/internal/configapi/client"
|
||||
"github.com/creekorful/trandoshan/internal/configapi/client_mock"
|
||||
"github.com/golang/mock/gomock"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckHostnameAllowed(t *testing.T) {
|
||||
mockCtrl := gomock.NewController(t)
|
||||
defer mockCtrl.Finish()
|
||||
|
||||
configClientMock := client_mock.NewMockClient(mockCtrl)
|
||||
|
||||
configClientMock.EXPECT().GetForbiddenHostnames().Return([]client.ForbiddenHostname{
|
||||
{Hostname: "google.onion"},
|
||||
}, nil)
|
||||
if allowed, err := CheckHostnameAllowed(configClientMock, "https://google.onion"); allowed || err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
configClientMock.EXPECT().GetForbiddenHostnames().Return([]client.ForbiddenHostname{
|
||||
{Hostname: "google.onion"},
|
||||
}, nil)
|
||||
if allowed, err := CheckHostnameAllowed(configClientMock, "https://google2.onion"); !allowed || err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue