Skip to content

Commit

Permalink
Fix crash when specifying StripQueryExcludes in config file
Browse files Browse the repository at this point in the history
- Change type from []string → []interface{}
- Refactor InList function to accept []interface{}
  • Loading branch information
wjdp committed Jan 16, 2021
1 parent bb0c766 commit 3c05eff
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
22 changes: 22 additions & 0 deletions htmltest/check-link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,28 @@ func TestAnchorExternalInvalidBrackets(t *testing.T) {
tExpectIssue(t, hT, "bad reference", 1)
}

func TestAnchorExternalQueryStringDefault(t *testing.T) {
// passes when ignoring from default list of query string exempt URLs
hT := tTestFile("fixtures/links/query_strings.html")
tExpectIssueCount(t, hT, 0)
}

func TestAnchorExternalQueryStripQueryExcludesEmpty(t *testing.T) {
// fails when StripQueryExcludes blank and URL doesn't like query string hits
hT := tTestFileOpts("fixtures/links/query_strings.html",
map[string]interface{}{"StripQueryExcludes": []interface{}{}})
tExpectIssueCount(t, hT, 1)
tExpectIssue(t, hT, "Non-OK status: 400", 1)
}

func TestAnchorExternalQueryStringStripQueryExcludesDiffers(t *testing.T) {
// fails when StripQueryExcludes does not include URL and URL doesn't like query string hits
hT := tTestFileOpts("fixtures/links/query_strings.html",
map[string]interface{}{"StripQueryExcludes": []interface{}{"example.com", "test.invalid"}})
tExpectIssueCount(t, hT, 1)
tExpectIssue(t, hT, "Non-OK status: 400", 1)
}

func TestAnchorInternalBroken(t *testing.T) {
// fails for broken internal links
hT := tTestFile("fixtures/links/brokenLinkInternal.html")
Expand Down
11 changes: 11 additions & 0 deletions htmltest/fixtures/links/query_strings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,400italic,600|Montserrat:400,700' rel='stylesheet' type='text/css'>
</head>
<body>

</body>
</html>
8 changes: 4 additions & 4 deletions htmltest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Options struct {

ExternalTimeout int
StripQueryString bool
StripQueryExcludes []string
StripQueryExcludes []interface{}

EnableCache bool
EnableLog bool
Expand Down Expand Up @@ -128,7 +128,7 @@ func DefaultOptions() map[string]interface{} {

"ExternalTimeout": 15,
"StripQueryString": true,
"StripQueryExcludes": []string{"fonts.googleapis.com"},
"StripQueryExcludes": []interface{}{"fonts.googleapis.com"},

"EnableCache": true,
"EnableLog": true,
Expand Down Expand Up @@ -164,9 +164,9 @@ func (hT *HTMLTest) setOptions(optsUser map[string]interface{}) {
}

// InList tests if key is in a slice/list.
func InList(list []string, key string) bool {
func InList(list []interface{}, key string) bool {
for _, item := range list {
if strings.Contains(key, item) {
if strings.Contains(key, fmt.Sprintf("%s", item)) {
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion htmltest/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSetOptions(t *testing.T) {
}

func TestInList(t *testing.T) {
lst := []string{"alpha", "bravo", "charlie"}
lst := []interface{}{"alpha", "bravo", "charlie"}
assert.Equals(t, "alpha in lst", InList(lst, "alpha"), true)
assert.Equals(t, "bravo in lst", InList(lst, "bravo"), true)
assert.Equals(t, "charlie in lst", InList(lst, "charlie"), true)
Expand Down

0 comments on commit 3c05eff

Please sign in to comment.