A tool for crawling and finding links to URLs which no longer exist
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
deadlinks/parser_test.go

135 lines
2.1 KiB

package deadlinks
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParser(t *testing.T) {
t.Parallel()
parser := NewParser()
tests := []struct {
mimeType string
body string
wantURLs []URL
wantErrs []string
}{
{
"image/jpg",
"ANYTHING",
nil,
nil,
},
{
"text/gemini",
``,
nil,
nil,
},
{
"text/gemini",
`
# HEADER
=> https://foo.com some link
=> empty/path
=> /foo/bar here's an absolute path
=> what.com a domain?
ok here's some text
`,
[]URL{
"https://foo.com",
"empty/path",
"/foo/bar",
"what.com",
},
nil,
},
{
"text/gemini",
`
# HEADER
=> https://foo.com some link
=> empty/path
=> /foo/bar here's an absolute path
=> what.com a domain?
ok here's some text
`,
[]URL{
"https://foo.com",
"empty/path",
"/foo/bar",
"what.com",
},
nil,
},
{
"text/gemini",
`
=> : NO FISH ALLOWED
=> /good/dog
`,
[]URL{"/good/dog"},
[]string{
`parsing URL from line "=> : NO FISH ALLOWED\n": parse ":": missing protocol scheme`,
},
},
{
"text/html",
`
<a href="foo.com">OHAI</a>
<img src="/bar">
<img src="/bar/baz" />
<link rel="stylesheet" href="style.css" />
`,
[]URL{"foo.com", "/bar", "/bar/baz", "style.css"},
nil,
},
}
for i := range tests {
test := tests[i]
name := fmt.Sprintf(
"%d-%s", i, strings.ReplaceAll(test.mimeType, "/", "_"),
)
t.Run(name, func(t *testing.T) {
t.Parallel()
body := bytes.NewBufferString(test.body)
gotURLs, gotErr := parser.Parse(test.mimeType, body)
assert.Equal(t, test.wantURLs, gotURLs)
if len(test.wantErrs) == 0 {
assert.NoError(t, gotErr)
return
}
type joinedErr interface {
Unwrap() []error
}
var gotErrs []error
if joinedErr, ok := gotErr.(joinedErr); ok {
gotErrs = joinedErr.Unwrap()
} else if gotErr != nil {
gotErrs = []error{gotErr}
}
gotErrStrs := make([]string, len(gotErrs))
for i := range gotErrs {
gotErrStrs[i] = gotErrs[i].Error()
}
assert.Equal(t, test.wantErrs, gotErrStrs)
})
}
}