diff options
Diffstat (limited to 'brokenlinks/brokenlinks_test.go')
| -rw-r--r-- | brokenlinks/brokenlinks_test.go | 132 |
1 files changed, 127 insertions, 5 deletions
diff --git a/brokenlinks/brokenlinks_test.go b/brokenlinks/brokenlinks_test.go index 6feb1e2..d9f9a59 100644 --- a/brokenlinks/brokenlinks_test.go +++ b/brokenlinks/brokenlinks_test.go @@ -4,6 +4,7 @@ package brokenlinks_test import ( + "encoding/json" "log" "net/http" "os" @@ -16,21 +17,27 @@ import ( "git.sr.ht/~shulhan/jarink/brokenlinks" ) -// The test run three web servers that serve content on "testdata/web/". -// The first web server is the one that we want to scan. -// The second web server is external web server, where HTML pages should not +// The test run four web servers. +// +// The first web server is the one that we want to scan, it serve content on +// "testdata/web". +// +// The second web server is an external web server, where HTML pages should not // be parsed. +// // The third web server is with insecure, self-signed TLS, for testing // "insecure" option. -// -// Command to generate certificate: +// Commands to generate certificate: // $ openssl genrsa -out 127.0.0.1.key // $ openssl x509 -new -key=127.0.0.1.key -subj="/CN=shulhan" \ // -days=3650 -out=127.0.0.1.pem +// +// The fourth web server is slow response web server. const testAddress = `127.0.0.1:11836` const testExternalAddress = `127.0.0.1:11900` const testInsecureAddress = `127.0.0.1:11838` +const testAddressSlow = `127.0.0.1:11839` func TestMain(m *testing.M) { log.SetFlags(0) @@ -40,6 +47,7 @@ func TestMain(m *testing.M) { go testServer(fshandle) go testExternalServer(fshandle) go testInsecureServer(fshandle) + go runServerSlow(testAddressSlow) var err = libnet.WaitAlive(`tcp`, testAddress, 5*time.Second) if err != nil { @@ -53,6 +61,10 @@ func TestMain(m *testing.M) { if err != nil { log.Fatal(err) } + err = libnet.WaitAlive(`tcp`, testAddressSlow, 5*time.Second) + if err != nil { + log.Fatal(err) + } os.Exit(m.Run()) } @@ -112,6 +124,80 @@ func testInsecureServer(fshandle http.Handler) { } } +func runServerSlow(addr string) { + var mux = http.NewServeMux() + mux.HandleFunc(`/`, func(resp http.ResponseWriter, req *http.Request) { + resp.WriteHeader(http.StatusOK) + var body = []byte(`<html><body> + <a href="/slow1">Slow 1</a> + <a href="/slow2">Slow 2</a> + <a href="/slow3">Slow 3</a> + </body></html>`) + resp.Write(body) + }) + + mux.HandleFunc(`/slow1`, + func(resp http.ResponseWriter, req *http.Request) { + resp.WriteHeader(http.StatusOK) + var body = []byte(`<html><body> + <a href="/slow1/sub">Slow 1, sub</a> + <a href="/slow2/sub">Slow 2, sub</a> + <a href="/slow3/sub">Slow 3, sub</a> + </body></html>`) + resp.Write(body) + }) + mux.HandleFunc(`/slow2`, + func(resp http.ResponseWriter, req *http.Request) { + time.Sleep(1 * time.Second) + resp.WriteHeader(http.StatusOK) + var body = []byte(`<html><body> + <a href="/slow1/sub">Slow 1, sub</a> + <a href="/slow2/sub">Slow 2, sub</a> + <a href="/slow3/sub">Slow 3, sub</a> + </body></html>`) + resp.Write(body) + }) + mux.HandleFunc(`/slow3`, + func(resp http.ResponseWriter, req *http.Request) { + time.Sleep(2 * time.Second) + resp.WriteHeader(http.StatusOK) + var body = []byte(`<html><body> + <a href="/slow1/sub">Slow 1, sub</a> + <a href="/slow2/sub">Slow 2, sub</a> + <a href="/slow3/sub">Slow 3, sub</a> + </body></html>`) + resp.Write(body) + }) + + mux.HandleFunc(`/slow1/sub`, + func(resp http.ResponseWriter, req *http.Request) { + time.Sleep(4 * time.Second) + resp.WriteHeader(http.StatusOK) + }) + mux.HandleFunc(`/slow2/sub`, + func(resp http.ResponseWriter, req *http.Request) { + time.Sleep(5 * time.Second) + resp.WriteHeader(http.StatusOK) + }) + mux.HandleFunc(`/slow3/sub`, + func(resp http.ResponseWriter, req *http.Request) { + time.Sleep(6 * time.Second) + resp.WriteHeader(http.StatusForbidden) + }) + + var httpServer = &http.Server{ + Addr: addr, + Handler: mux, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + MaxHeaderBytes: 1 << 20, + } + var err = httpServer.ListenAndServe() + if err != nil { + log.Fatal(err) + } +} + func TestScan(t *testing.T) { var testUrl = `http://` + testAddress @@ -284,3 +370,39 @@ func TestScan_pastResult(t *testing.T) { test.Assert(t, tcase.opts.Url, tcase.exp, result.BrokenLinks) } } + +func TestScan_slow(t *testing.T) { + const testUrl = `http://` + testAddressSlow + + var opts = brokenlinks.Options{ + Url: testUrl, + } + + var gotResult *brokenlinks.Result + var err error + gotResult, err = brokenlinks.Scan(opts) + if err != nil { + t.Fatal(err) + } + + got, _ := json.MarshalIndent(gotResult, ``, ` `) + 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, + }}, + testUrl + `/slow2`: []brokenlinks.Broken{{ + Link: testUrl + `/slow3/sub`, + Code: http.StatusForbidden, + }}, + testUrl + `/slow3`: []brokenlinks.Broken{{ + Link: testUrl + `/slow3/sub`, + Code: http.StatusForbidden, + }}, + }, + } + test.Assert(t, `TestScan_slow`, expResult, gotResult) +} |
