diff options
Diffstat (limited to 'brokenlinks/brokenlinks_test.go')
| -rw-r--r-- | brokenlinks/brokenlinks_test.go | 231 |
1 files changed, 160 insertions, 71 deletions
diff --git a/brokenlinks/brokenlinks_test.go b/brokenlinks/brokenlinks_test.go index db3775a..a04be7f 100644 --- a/brokenlinks/brokenlinks_test.go +++ b/brokenlinks/brokenlinks_test.go @@ -4,8 +4,11 @@ package brokenlinks_test import ( + "errors" "log" + "net" "net/http" + "net/url" "os" "path/filepath" "testing" @@ -13,7 +16,9 @@ import ( libnet "git.sr.ht/~shulhan/pakakeh.go/lib/net" "git.sr.ht/~shulhan/pakakeh.go/lib/test" + "golang.org/x/net/html/atom" + "git.sr.ht/~shulhan/jarink" "git.sr.ht/~shulhan/jarink/brokenlinks" "git.sr.ht/~shulhan/jarink/internal" ) @@ -212,8 +217,27 @@ func runServerSlow() { func TestScan(t *testing.T) { var testUrl = `http://` + testAddress + // Generate ParentUrl. + + parsedTestUrl, err := url.Parse(testUrl) + if err != nil { + t.Fatal(err) + } + parsedUrlBrokenHtml, err := url.Parse(testUrl + `/broken.html`) + if err != nil { + t.Fatal(err) + } + parsedUrlPage2, err := url.Parse(testUrl + `/page2`) + if err != nil { + t.Fatal(err) + } + + // Generate error for ErrScan. + + _, errScanPort := url.Parse(`http://127.0.0.1:abc`) + type testCase struct { - exp map[string][]brokenlinks.Broken + exp map[string][]jarink.Link expError string desc string opts brokenlinks.Options @@ -246,92 +270,133 @@ func TestScan(t *testing.T) { }, expError: `Scan: Options: unknown status code "50"`, }, { - desc: `With ` + testUrl, + desc: `With Url=testUrl`, opts: brokenlinks.Options{ Url: testUrl, IgnoreStatus: `403`, Insecure: true, IsVerbose: true, }, - exp: map[string][]brokenlinks.Broken{ - testUrl: []brokenlinks.Broken{ + exp: map[string][]jarink.Link{ + testUrl: []jarink.Link{ { - Link: testUrl + `/broken.png`, - Code: http.StatusNotFound, + ParentUrl: parsedTestUrl, + Url: testUrl + `/broken.png`, + StatusCode: http.StatusNotFound, + Kind: int(atom.Img), }, { - Link: testUrl + `/brokenPage`, - Code: http.StatusNotFound, + ParentUrl: parsedTestUrl, + Url: testUrl + `/brokenPage`, + StatusCode: http.StatusNotFound, + Kind: int(atom.A), }, { - Link: `http://127.0.0.1:abc`, - Error: `parse "http://127.0.0.1:abc": invalid port ":abc" after host`, - Code: brokenlinks.StatusBadLink, + ParentUrl: parsedTestUrl, + Url: `http://127.0.0.1:abc`, + ErrScan: errScanPort, + Error: `parse "http://127.0.0.1:abc": invalid port ":abc" after host`, + StatusCode: brokenlinks.StatusBadLink, + Kind: int(atom.A), }, { - Link: `http:/127.0.0.1:11836`, - Error: `Get "http:/127.0.0.1:11836": http: no Host in request URL`, - Code: brokenlinks.StatusBadLink, + ParentUrl: parsedTestUrl, + Url: `http:/127.0.0.1:11836`, + ErrScan: &url.Error{ + Op: `Get`, + URL: `http:/127.0.0.1:11836`, + Err: errors.New(`http: no Host in request URL`), + }, + Error: `Get "http:/127.0.0.1:11836": http: no Host in request URL`, + StatusCode: brokenlinks.StatusBadLink, + Kind: int(atom.A), + IsExternal: true, }, { - Link: `https://domain`, - Error: `Get "https://domain": dial tcp: lookup domain: no such host`, - Code: 700, + ParentUrl: parsedTestUrl, + Url: `https://domain`, + ErrScan: &url.Error{ + Op: `Get`, + URL: `https://domain`, + Err: &net.OpError{ + Op: `dial`, + Net: `tcp`, + Err: &net.DNSError{ + Err: `no such host`, + Name: `domain`, + IsNotFound: true, + }, + }, + }, + Error: `Get "https://domain": dial tcp: lookup domain: no such host`, + StatusCode: 700, + Kind: int(atom.A), + IsExternal: true, }, }, - testUrl + `/broken.html`: []brokenlinks.Broken{ + testUrl + `/broken.html`: []jarink.Link{ { - Link: testUrl + `/brokenPage`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlBrokenHtml, + Url: testUrl + `/brokenPage`, + StatusCode: http.StatusNotFound, + Kind: int(atom.A), }, }, - testUrl + `/page2`: []brokenlinks.Broken{ + testUrl + `/page2`: []jarink.Link{ { - Link: testUrl + `/broken.png`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/broken.png`, + StatusCode: http.StatusNotFound, + Kind: int(atom.Img), }, { - Link: testUrl + `/page2/broken/relative`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/page2/broken/relative`, + StatusCode: http.StatusNotFound, + Kind: int(atom.A), }, { - Link: testUrl + `/page2/broken2.png`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/page2/broken2.png`, + StatusCode: http.StatusNotFound, + Kind: int(atom.Img), }, }, }, }, { - desc: `With ` + testUrl + `/page2`, // Scanning on "/page2" should not scan the the "/" or other // pages other than below of "/page2" itself. + desc: `With Url=/page2`, opts: brokenlinks.Options{ Url: testUrl + `/page2`, IsVerbose: true, }, - exp: map[string][]brokenlinks.Broken{ - testUrl + `/page2`: []brokenlinks.Broken{ + exp: map[string][]jarink.Link{ + testUrl + `/page2`: []jarink.Link{ { - Link: testUrl + `/broken.png`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/broken.png`, + StatusCode: http.StatusNotFound, + Kind: int(atom.Img), + IsExternal: true, }, { - Link: testUrl + `/page2/broken/relative`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/page2/broken/relative`, + StatusCode: http.StatusNotFound, + Kind: int(atom.A), }, { - Link: testUrl + `/page2/broken2.png`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/page2/broken2.png`, + StatusCode: http.StatusNotFound, + Kind: int(atom.Img), }, }, }, }} - var ( - result *brokenlinks.Result - err error - ) for _, tcase := range listCase { 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) + result, err := brokenlinks.Scan(tcase.opts) if err != nil { - test.Assert(tt, tcase.opts.Url+` error`, - tcase.expError, err.Error()) + test.Assert(tt, tcase.opts.Url+` error`, tcase.expError, err.Error()) return } @@ -348,8 +413,13 @@ func TestScan(t *testing.T) { func TestScan_pastResult(t *testing.T) { var testUrl = `http://` + testAddress + parsedUrlPage2, err := url.Parse(testUrl + `/page2`) + if err != nil { + t.Fatal(err) + } + type testCase struct { - exp map[string][]brokenlinks.Broken + exp map[string][]jarink.Link desc string expError string opts brokenlinks.Options @@ -369,33 +439,36 @@ func TestScan_pastResult(t *testing.T) { PastResultFile: `testdata/past_result.json`, IgnoreStatus: `403`, }, - exp: map[string][]brokenlinks.Broken{ - testUrl + `/page2`: []brokenlinks.Broken{ + exp: map[string][]jarink.Link{ + testUrl + `/page2`: []jarink.Link{ { - Link: testUrl + `/broken.png`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/broken.png`, + StatusCode: http.StatusNotFound, + Kind: int(atom.Img), + IsExternal: true, }, { - Link: testUrl + `/page2/broken/relative`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/page2/broken/relative`, + StatusCode: http.StatusNotFound, + Kind: int(atom.A), }, { - Link: testUrl + `/page2/broken2.png`, - Code: http.StatusNotFound, + ParentUrl: parsedUrlPage2, + Url: testUrl + `/page2/broken2.png`, + StatusCode: http.StatusNotFound, + Kind: int(atom.Img), }, }, }, }} - var ( - result *brokenlinks.Result - err error - ) for _, tcase := range listCase { 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) + result, err := brokenlinks.Scan(tcase.opts) if err != nil { test.Assert(tt, tcase.opts.Url+` error`, tcase.expError, err.Error()) return @@ -410,14 +483,24 @@ func TestScan_pastResult(t *testing.T) { func TestScan_slow(t *testing.T) { const testUrl = `http://` + testAddressSlow + parsedUrlSlow1, err := url.Parse(testUrl + `/slow1`) + if err != nil { + t.Fatal(err) + } + parsedUrlSlow2, err := url.Parse(testUrl + `/slow2`) + if err != nil { + t.Fatal(err) + } + parsedUrlSlow3, err := url.Parse(testUrl + `/slow3`) + if err != nil { + t.Fatal(err) + } + var opts = brokenlinks.Options{ Url: testUrl, IsVerbose: true, } - - var gotResult *brokenlinks.Result - var err error - gotResult, err = brokenlinks.Scan(opts) + gotResult, err := brokenlinks.Scan(opts) if err != nil { t.Fatal(err) } @@ -426,18 +509,24 @@ func TestScan_slow(t *testing.T) { //t.Logf(`got=%s`, got) var expResult = &brokenlinks.Result{ - BrokenLinks: map[string][]brokenlinks.Broken{ - testUrl + `/slow1`: []brokenlinks.Broken{{ - Link: testUrl + `/slow3/sub`, - Code: http.StatusForbidden, + BrokenLinks: map[string][]jarink.Link{ + testUrl + `/slow1`: []jarink.Link{{ + ParentUrl: parsedUrlSlow1, + Url: testUrl + `/slow3/sub`, + StatusCode: http.StatusForbidden, + Kind: int(atom.A), }}, - testUrl + `/slow2`: []brokenlinks.Broken{{ - Link: testUrl + `/slow3/sub`, - Code: http.StatusForbidden, + testUrl + `/slow2`: []jarink.Link{{ + ParentUrl: parsedUrlSlow2, + Url: testUrl + `/slow3/sub`, + StatusCode: http.StatusForbidden, + Kind: int(atom.A), }}, - testUrl + `/slow3`: []brokenlinks.Broken{{ - Link: testUrl + `/slow3/sub`, - Code: http.StatusForbidden, + testUrl + `/slow3`: []jarink.Link{{ + ParentUrl: parsedUrlSlow3, + Url: testUrl + `/slow3/sub`, + StatusCode: http.StatusForbidden, + Kind: int(atom.A), }}, }, } |
