diff options
| author | Shulhan <ms@kilabit.info> | 2025-06-21 15:20:01 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-06-27 12:19:23 +0700 |
| commit | 1ca561ed0ecfa59b70a10191ac8e58cde90d126e (patch) | |
| tree | 80f0c65f7e9321ad92dfc1a53a444226cee4be3d /brokenlinks/brokenlinks_test.go | |
| parent | 8bc8fce1bd80b5a25c452ac5a24b1a1e3f5a4feb (diff) | |
| download | jarink-1ca561ed0ecfa59b70a10191ac8e58cde90d126e.tar.xz | |
brokenlinks: implement caching for external URLs
Any succesful fetch on external URLs, will be recorded into jarink
cache file, located in user's home cache directory.
For example, in Linux it would be `$HOME/.cache/jarink/cache.json`.
This help improve the future rescanning on the same or different target
URL, minimizing network requests.
Diffstat (limited to 'brokenlinks/brokenlinks_test.go')
| -rw-r--r-- | brokenlinks/brokenlinks_test.go | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/brokenlinks/brokenlinks_test.go b/brokenlinks/brokenlinks_test.go index d9f9a59..f957ae3 100644 --- a/brokenlinks/brokenlinks_test.go +++ b/brokenlinks/brokenlinks_test.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "os" + "path/filepath" "testing" "time" @@ -15,6 +16,7 @@ import ( "git.sr.ht/~shulhan/pakakeh.go/lib/test" "git.sr.ht/~shulhan/jarink/brokenlinks" + "git.sr.ht/~shulhan/jarink/internal" ) // The test run four web servers. @@ -41,6 +43,16 @@ const testAddressSlow = `127.0.0.1:11839` func TestMain(m *testing.M) { log.SetFlags(0) + + var orgCacheFile = internal.CacheFile + var tmpCacheFile = filepath.Join(os.TempDir(), `cache.json`) + internal.CacheFile = func() (string, error) { + return tmpCacheFile, nil + } + defer func() { + internal.CacheFile = orgCacheFile + }() + var httpDirWeb = http.Dir(`testdata/web`) var fshandle = http.FileServer(httpDirWeb) @@ -234,6 +246,7 @@ func TestScan(t *testing.T) { Url: testUrl, IgnoreStatus: `403`, Insecure: true, + IsVerbose: true, }, exp: map[string][]brokenlinks.Broken{ testUrl: []brokenlinks.Broken{ @@ -276,7 +289,8 @@ func TestScan(t *testing.T) { // Scanning on "/page2" should not scan the the "/" or other // pages other than below of "/page2" itself. opts: brokenlinks.Options{ - Url: testUrl + `/page2`, + Url: testUrl + `/page2`, + IsVerbose: true, }, exp: map[string][]brokenlinks.Broken{ testUrl + `/page2`: []brokenlinks.Broken{ @@ -406,3 +420,34 @@ func TestScan_slow(t *testing.T) { } test.Assert(t, `TestScan_slow`, expResult, gotResult) } + +func TestBrokenlinks_cache(t *testing.T) { + var orgCacheFile = internal.CacheFile + var gotCacheFile = filepath.Join(t.TempDir(), `cache.json`) + var expCacheFile = filepath.Join(`testdata`, `exp_cache.json`) + defer func() { + internal.CacheFile = orgCacheFile + }() + internal.CacheFile = func() (string, error) { + return gotCacheFile, nil + } + + var testUrl = `http://` + testAddress + var opts = brokenlinks.Options{ + Url: testUrl, + IgnoreStatus: `403`, + Insecure: true, + } + + var err error + _, err = brokenlinks.Scan(opts) + gotCache, err := os.ReadFile(gotCacheFile) + if err != nil { + t.Fatal(err) + } + expCache, err := os.ReadFile(expCacheFile) + if err != nil { + t.Fatal(err) + } + test.Assert(t, `cache`, string(gotCache), string(expCache)) +} |
