diff options
Diffstat (limited to 'brokenlinks/brokenlinks_test.go')
| -rw-r--r-- | brokenlinks/brokenlinks_test.go | 87 |
1 files changed, 53 insertions, 34 deletions
diff --git a/brokenlinks/brokenlinks_test.go b/brokenlinks/brokenlinks_test.go index b49bcc5..db3775a 100644 --- a/brokenlinks/brokenlinks_test.go +++ b/brokenlinks/brokenlinks_test.go @@ -1,10 +1,9 @@ -// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> // SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> package brokenlinks_test import ( - "encoding/json" "log" "net/http" "os" @@ -59,7 +58,7 @@ func TestMain(m *testing.M) { go testServer(fshandle) go testExternalServer(fshandle) go testInsecureServer(fshandle) - go runServerSlow(testAddressSlow) + go runServerSlow() var err = libnet.WaitAlive(`tcp`, testAddress, 5*time.Second) if err != nil { @@ -136,7 +135,7 @@ func testInsecureServer(fshandle http.Handler) { } } -func runServerSlow(addr string) { +func runServerSlow() { var mux = http.NewServeMux() mux.HandleFunc(`/`, func(resp http.ResponseWriter, req *http.Request) { resp.WriteHeader(http.StatusOK) @@ -171,7 +170,7 @@ func runServerSlow(addr string) { }) mux.HandleFunc(`/slow3`, func(resp http.ResponseWriter, req *http.Request) { - time.Sleep(2 * time.Second) + time.Sleep(1 * time.Second) resp.WriteHeader(http.StatusOK) var body = []byte(`<html><body> <a href="/slow1/sub">Slow 1, sub</a> @@ -183,22 +182,22 @@ func runServerSlow(addr string) { mux.HandleFunc(`/slow1/sub`, func(resp http.ResponseWriter, req *http.Request) { - time.Sleep(4 * time.Second) + time.Sleep(1 * time.Second) resp.WriteHeader(http.StatusOK) }) mux.HandleFunc(`/slow2/sub`, func(resp http.ResponseWriter, req *http.Request) { - time.Sleep(5 * time.Second) + time.Sleep(1 * time.Second) resp.WriteHeader(http.StatusOK) }) mux.HandleFunc(`/slow3/sub`, func(resp http.ResponseWriter, req *http.Request) { - time.Sleep(6 * time.Second) + time.Sleep(1 * time.Second) resp.WriteHeader(http.StatusForbidden) }) var httpServer = &http.Server{ - Addr: addr, + Addr: testAddressSlow, Handler: mux, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, @@ -216,32 +215,38 @@ func TestScan(t *testing.T) { type testCase struct { exp map[string][]brokenlinks.Broken expError string + desc string opts brokenlinks.Options } listCase := []testCase{{ + desc: `With invalid URL`, opts: brokenlinks.Options{ Url: `127.0.0.1:14594`, }, expError: `Scan: Options: invalid URL "127.0.0.1:14594"`, }, { + desc: `With non-open port`, opts: brokenlinks.Options{ Url: `http://127.0.0.1:14594`, }, expError: `Scan: Get "http://127.0.0.1:14594": dial tcp 127.0.0.1:14594: connect: connection refused`, }, { + desc: `With invalid IgnoreStatus`, opts: brokenlinks.Options{ Url: testUrl, IgnoreStatus: "abc", }, expError: `Scan: Options: invalid status code "abc"`, }, { + desc: `With unknown IgnoreStatus code`, opts: brokenlinks.Options{ Url: testUrl, IgnoreStatus: "50", }, expError: `Scan: Options: unknown status code "50"`, }, { + desc: `With ` + testUrl, opts: brokenlinks.Options{ Url: testUrl, IgnoreStatus: `403`, @@ -290,6 +295,7 @@ func TestScan(t *testing.T) { }, }, }, { + desc: `With ` + testUrl + `/page2`, // Scanning on "/page2" should not scan the the "/" or other // pages other than below of "/page2" itself. opts: brokenlinks.Options{ @@ -317,16 +323,23 @@ func TestScan(t *testing.T) { err error ) for _, tcase := range listCase { - t.Logf(`--- brokenlinks: %s`, tcase.opts.Url) - result, err = brokenlinks.Scan(tcase.opts) - if err != nil { - test.Assert(t, tcase.opts.Url+` error`, - tcase.expError, err.Error()) - continue - } - //got, _ := json.MarshalIndent(result.BrokenLinks, ``, ` `) - //t.Logf(`got=%s`, got) - test.Assert(t, tcase.opts.Url, tcase.exp, result.BrokenLinks) + t.Run(tcase.desc, func(tt *testing.T) { + internal.CacheFile = func() (string, error) { + return tt.TempDir() + `/cache.json`, nil + } + + result, err = brokenlinks.Scan(tcase.opts) + if err != nil { + test.Assert(tt, tcase.opts.Url+` error`, + tcase.expError, err.Error()) + return + } + + //got, _ := json.MarshalIndent(result.BrokenLinks, ``, ` `) + //tt.Logf(`got=%s`, got) + + test.Assert(tt, tcase.opts.Url, tcase.exp, result.BrokenLinks) + }) } } @@ -337,19 +350,20 @@ func TestScan_pastResult(t *testing.T) { type testCase struct { exp map[string][]brokenlinks.Broken + desc string expError string opts brokenlinks.Options } listCase := []testCase{{ - // With invalid file. + desc: `With invalid file`, opts: brokenlinks.Options{ Url: testUrl, PastResultFile: `testdata/invalid`, }, expError: `Scan: open testdata/invalid: no such file or directory`, }, { - // With valid file. + desc: `With valid file`, opts: brokenlinks.Options{ Url: testUrl, PastResultFile: `testdata/past_result.json`, @@ -376,16 +390,20 @@ func TestScan_pastResult(t *testing.T) { err error ) for _, tcase := range listCase { - t.Logf(`--- brokenlinks: %s`, tcase.opts.Url) - result, err = brokenlinks.Scan(tcase.opts) - if err != nil { - test.Assert(t, tcase.opts.Url+` error`, - tcase.expError, err.Error()) - continue - } - //got, _ := json.MarshalIndent(result.BrokenLinks, ``, ` `) - //t.Logf(`got=%s`, got) - test.Assert(t, tcase.opts.Url, tcase.exp, result.BrokenLinks) + t.Run(tcase.desc, func(tt *testing.T) { + internal.CacheFile = func() (string, error) { + return tt.TempDir() + `/cache.json`, nil + } + + result, err = brokenlinks.Scan(tcase.opts) + if err != nil { + test.Assert(tt, tcase.opts.Url+` error`, tcase.expError, err.Error()) + return + } + //got, _ := json.MarshalIndent(result.BrokenLinks, ``, ` `) + //tt.Logf(`got=%s`, got) + test.Assert(tt, tcase.opts.Url, tcase.exp, result.BrokenLinks) + }) } } @@ -393,7 +411,8 @@ func TestScan_slow(t *testing.T) { const testUrl = `http://` + testAddressSlow var opts = brokenlinks.Options{ - Url: testUrl, + Url: testUrl, + IsVerbose: true, } var gotResult *brokenlinks.Result @@ -403,8 +422,8 @@ func TestScan_slow(t *testing.T) { t.Fatal(err) } - got, _ := json.MarshalIndent(gotResult, ``, ` `) - t.Logf(`got=%s`, got) + //got, _ := json.MarshalIndent(gotResult, ``, ` `) + //t.Logf(`got=%s`, got) var expResult = &brokenlinks.Result{ BrokenLinks: map[string][]brokenlinks.Broken{ |
