From 9ab22f4c2728f75dc7bbc8112b09caad3b65be96 Mon Sep 17 00:00:00 2001 From: Adriano Cunha <35786489+adrcunha@users.noreply.github.com> Date: Fri, 6 Dec 2019 08:16:06 -0800 Subject: [PATCH] Report 4XX and 3XX links as link errors (#24) * Report 4XX and 3XX links as link errors If a link points to a file that 4XXs or 5XXs, it should be treated as a failure. Most obvious case is a typo in a link leading to 404 when clicked. Also deal with the "small read buffer" error, so all tests pass. * Make lint happy --- url_checker.go | 21 +++++++++++++++------ url_checker_test.go | 2 ++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/url_checker.go b/url_checker.go index f3895a0..3104ce2 100644 --- a/url_checker.go +++ b/url_checker.go @@ -1,7 +1,8 @@ package main import ( - "errors" + "fmt" + "net/http" "net/url" "os" "path" @@ -42,12 +43,20 @@ func (c urlChecker) Check(u string, f string) error { c.semaphore.Request() defer c.semaphore.Release() + var sc int if c.timeout == 0 { - _, _, err := fasthttp.Get(nil, u) - return err + sc, _, err = fasthttp.Get(nil, u) + } else { + sc, _, err = fasthttp.GetTimeout(nil, u, c.timeout) + } + if sc >= http.StatusBadRequest { + return fmt.Errorf("%s (HTTP error %d)", http.StatusText(sc), sc) + } + // Ignore errors from fasthttp about small buffer for URL headers, + // the content is discarded anyway. + if _, ok := err.(*fasthttp.ErrSmallBuffer); ok { + err = nil } - - _, _, err = fasthttp.GetTimeout(nil, u, c.timeout) return err } @@ -83,7 +92,7 @@ func (c urlChecker) resolveURL(u string, f string) (string, bool, error) { } if c.documentRoot == "" { - return "", false, errors.New("document root directory is not specified") + return "", false, fmt.Errorf("document root directory is not specified") } return path.Join(c.documentRoot, uu.Path), true, nil diff --git a/url_checker_test.go b/url_checker_test.go index 435976b..5d53912 100644 --- a/url_checker_test.go +++ b/url_checker_test.go @@ -15,6 +15,8 @@ func TestURLCheckerCheck(t *testing.T) { assert.Equal(t, nil, c.Check(u, "README.md")) } + assert.NotEqual(t, nil, c.Check("http://www.google.com/README-I-DONT-EXIST.md", "README.md")) + for _, u := range []string{"https://hey-hey-hi-google.com", "READYOU.md", "://"} { assert.NotEqual(t, nil, c.Check(u, "README.md")) }