Skip to content

Commit

Permalink
test: add tests for malformed links
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshNarayanJha committed Jul 25, 2024
1 parent 669680e commit 933182c
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions internal/pkg/crawl/link_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,64 @@ func TestParseMultipleLinks(t *testing.T) {
}
}

func TestParseOneMalformedLink(t *testing.T) {
var links []Link
links = append(links, Link{URL: "https://one.example.com", Rel: "preconnect"})

var link = `https://one.example.com>;; rel=preconnect";`

got := Parse(link)
want := links

if !slices.Equal(got, want) {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestParseMultipleMalformedLinks(t *testing.T) {
var links []Link
links = append(links,
Link{URL: "", Rel: "preconnect"},
Link{URL: "https://app.test.com", Rel: ""},
Link{URL: "", Rel: ""},
)

var link = `; rel="preconnect", https://app.test.com; rel=""; "bar", <>; ="preconnect"`

got := Parse(link)
want := links

if !slices.Equal(got, want) {
t.Errorf("got %q, wanted %q", got, want)
}
}

func TestParseAttr(t *testing.T) {
attr := `rel="preconnect"`

gotKey, gotValue := ParseAttr(attr)
wantKey, wantValue := "rel", "preconnect"

if (gotKey != wantKey) {
if gotKey != wantKey {
t.Errorf("got %q, wanted %q", gotKey, wantKey)
}

if gotValue != wantValue {
t.Errorf("got %q, wanted %q", gotValue, wantValue)
}
}

func TestParseMalformedAttr(t *testing.T) {
attr := `="preconnect"`

gotKey, gotValue := ParseAttr(attr)
wantKey, wantValue := "", "preconnect"

if gotKey != wantKey {
t.Errorf("got %q, wanted %q", gotKey, wantKey)
}

if (gotValue != wantValue) {
if gotValue != wantValue {
t.Errorf("got %q, wanted %q", gotValue, wantValue)
}
}

0 comments on commit 933182c

Please sign in to comment.