Skip to content

Commit

Permalink
Add IgnoreInternalURLs option (Solve #168)
Browse files Browse the repository at this point in the history
  • Loading branch information
divinerites committed May 23, 2021
1 parent 183ed16 commit d3ffce7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
19 changes: 14 additions & 5 deletions htmltest/check-link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package htmltest
import (
"crypto/x509"
"fmt"
"github.com/badoux/checkmail"
"github.com/wjdp/htmltest/htmldoc"
"github.com/wjdp/htmltest/issues"
"github.com/wjdp/htmltest/output"
"golang.org/x/net/html"
"net/http"
"net/url"
"os"
"path"
"strings"

"github.com/badoux/checkmail"
"github.com/wjdp/htmltest/htmldoc"
"github.com/wjdp/htmltest/issues"
"github.com/wjdp/htmltest/output"
"golang.org/x/net/html"
)

func (hT *HTMLTest) checkLink(document *htmldoc.Document, node *html.Node) {
Expand Down Expand Up @@ -276,6 +277,14 @@ func (hT *HTMLTest) checkInternal(ref *htmldoc.Reference) {
return
}

// Solve #168
urlStr := ref.URLString()

// Does this internal url match a internal url ignore rule?
if hT.opts.isInternalURLIgnored(urlStr) {
return
}

// First lookup in document store,
refDoc, refExists := hT.documentStore.ResolveRef(ref)

Expand Down
21 changes: 17 additions & 4 deletions htmltest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ type Options struct {
EnforceHTML5 bool
EnforceHTTPS bool

IgnoreURLs []interface{}
IgnoreDirs []interface{}
IgnoreURLs []interface{}
IgnoreInternalURLs []interface{}
IgnoreDirs []interface{}

IgnoreInternalEmptyHash bool
IgnoreEmptyHref bool
Expand Down Expand Up @@ -102,8 +103,9 @@ func DefaultOptions() map[string]interface{} {
"EnforceHTML5": false,
"EnforceHTTPS": false,

"IgnoreURLs": []interface{}{},
"IgnoreDirs": []interface{}{},
"IgnoreURLs": []interface{}{},
"IgnoreInternalURLs": []interface{}{},
"IgnoreDirs": []interface{}{},

"IgnoreInternalEmptyHash": false,
"IgnoreEmptyHref": false,
Expand Down Expand Up @@ -182,3 +184,14 @@ func (opts *Options) isURLIgnored(url string) bool {
}
return false
}

// Solve #168
// Is the given local URL ignored by the current configuration
func (opts *Options) isInternalURLIgnored(url string) bool {
for _, item := range opts.IgnoreInternalURLs {
if item.(string) == url {
return true
}
}
return false
}
14 changes: 14 additions & 0 deletions htmltest/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ func TestIsURLIgnored(t *testing.T) {
assert.IsFalse(t, "url left alone", hT.opts.isURLIgnored("http://assetstore.info/lib/test.js"))
}

func TestIsInternalURLIgnored(t *testing.T) {
userOpts := map[string]interface{}{
"IgnoreInternalURLs": []interface{}{"/misc/js/script.js"},
"NoRun": true,
}

hT, err := Test(userOpts)
output.CheckErrorPanic(err)

assert.IsTrue(t, "url ignored", hT.opts.isURLIgnored("/misc/js/script.js"))
assert.IsFalse(t, "url left alone", hT.opts.isURLIgnored("misc/js/script.js"))
assert.IsFalse(t, "url left alone", hT.opts.isURLIgnored("/misc/js/script"))
}

func TestMergeHTTPHeaders(t *testing.T) {
userOpts := map[string]interface{}{
"HTTPHeaders": map[interface{}]interface{}{
Expand Down

0 comments on commit d3ffce7

Please sign in to comment.