aboutsummaryrefslogtreecommitdiff
path: root/deadlinks_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'deadlinks_test.go')
-rw-r--r--deadlinks_test.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/deadlinks_test.go b/deadlinks_test.go
new file mode 100644
index 0000000..b449b22
--- /dev/null
+++ b/deadlinks_test.go
@@ -0,0 +1,79 @@
+// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-only
+
+package deadlinks_test
+
+import (
+ "log"
+ "net/http"
+ "os"
+ "testing"
+ "time"
+
+ "git.sr.ht/~shulhan/deadlinks"
+ libnet "git.sr.ht/~shulhan/pakakeh.go/lib/net"
+ "git.sr.ht/~shulhan/pakakeh.go/lib/test"
+)
+
+const testListenAddress = `127.0.0.1:11836`
+
+func TestMain(m *testing.M) {
+ var httpDirWeb = http.Dir(`testdata/web`)
+ var fshandle = http.FileServer(httpDirWeb)
+
+ http.Handle(`/`, fshandle)
+ go func() {
+ var err = http.ListenAndServe(testListenAddress, nil)
+ if err != nil {
+ log.Fatal(err)
+ }
+ }()
+
+ var err = libnet.WaitAlive(`tcp`, testListenAddress, 5*time.Second)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ os.Exit(m.Run())
+}
+
+func TestDeadLinks_Scan(t *testing.T) {
+ var testUrl = `http://` + testListenAddress
+
+ type testCase struct {
+ exp map[string][]deadlinks.Broken
+ baseUrl string
+ }
+
+ listCase := []testCase{{
+ baseUrl: testUrl,
+ exp: map[string][]deadlinks.Broken{
+ testUrl: []deadlinks.Broken{{
+ Code: http.StatusNotFound,
+ Link: testUrl + `broken.png`,
+ }, {
+ Code: http.StatusNotFound,
+ Link: testUrl + `brokenPage`,
+ }},
+ testUrl + `page2`: []deadlinks.Broken{{
+ Code: http.StatusNotFound,
+ Link: testUrl + `broken2.png`,
+ }, {
+ Code: http.StatusNotFound,
+ Link: testUrl + `page2/broken/relative`,
+ }},
+ },
+ }}
+
+ var (
+ result *deadlinks.Result
+ err error
+ )
+ for _, tcase := range listCase {
+ result, err = deadlinks.Scan(tcase.baseUrl)
+ if err != nil {
+ t.Fatal(err)
+ }
+ test.Assert(t, tcase.baseUrl, tcase.exp, result.PageLinks)
+ }
+}