2019-09-04 04:12:00 -07:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2020-04-07 13:29:03 -07:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-09-04 04:12:00 -07:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-04-07 13:29:03 -07:00
|
|
|
func testStartFilterListener() net.Listener {
|
|
|
|
http.HandleFunc("/filters/1.txt", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
content := `||example.org^$third-party
|
2020-04-30 07:19:10 -07:00
|
|
|
# Inline comment example
|
2020-04-07 13:29:03 -07:00
|
|
|
||example.com^$third-party
|
|
|
|
0.0.0.0 example.com
|
|
|
|
`
|
|
|
|
_, _ = w.Write([]byte(content))
|
|
|
|
})
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() { _ = http.Serve(listener, nil) }()
|
|
|
|
return listener
|
|
|
|
}
|
|
|
|
|
2019-09-04 04:12:00 -07:00
|
|
|
func TestFilters(t *testing.T) {
|
2020-04-07 13:29:03 -07:00
|
|
|
l := testStartFilterListener()
|
|
|
|
defer func() { _ = l.Close() }()
|
|
|
|
|
2020-02-13 08:42:07 -07:00
|
|
|
dir := prepareTestDir()
|
|
|
|
defer func() { _ = os.RemoveAll(dir) }()
|
|
|
|
Context = homeContext{}
|
|
|
|
Context.workDir = dir
|
|
|
|
Context.client = &http.Client{
|
2020-03-17 05:00:40 -07:00
|
|
|
Timeout: 5 * time.Second,
|
2019-09-04 04:12:00 -07:00
|
|
|
}
|
2020-03-17 05:00:40 -07:00
|
|
|
Context.filters.Init()
|
2019-09-04 04:12:00 -07:00
|
|
|
|
|
|
|
f := filter{
|
2020-04-07 13:29:03 -07:00
|
|
|
URL: fmt.Sprintf("http://127.0.0.1:%d/filters/1.txt", l.Addr().(*net.TCPAddr).Port),
|
2019-09-04 04:12:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// download
|
2020-03-17 05:00:40 -07:00
|
|
|
ok, err := Context.filters.update(&f)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.True(t, ok)
|
2020-04-07 13:29:03 -07:00
|
|
|
assert.Equal(t, 3, f.RulesCount)
|
2019-09-04 04:12:00 -07:00
|
|
|
|
|
|
|
// refresh
|
2020-03-17 05:00:40 -07:00
|
|
|
ok, err = Context.filters.update(&f)
|
2019-09-04 04:12:00 -07:00
|
|
|
assert.True(t, !ok && err == nil)
|
|
|
|
|
2020-03-17 05:00:40 -07:00
|
|
|
err = Context.filters.load(&f)
|
2019-09-04 04:12:00 -07:00
|
|
|
assert.True(t, err == nil)
|
|
|
|
|
|
|
|
f.unload()
|
2020-02-13 08:42:07 -07:00
|
|
|
_ = os.Remove(f.Path())
|
2019-09-04 04:12:00 -07:00
|
|
|
}
|