Browse Source

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
master
Adriano Cunha 6 years ago
committed by Yota Toyama
parent
commit
9ab22f4c27
  1. 21
      url_checker.go
  2. 2
      url_checker_test.go

21
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

2
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"))
}

Loading…
Cancel
Save