Skip to content

Commit

Permalink
Fix link resolver crashing when provided url is invalid (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Apr 23, 2022
1 parent ff81073 commit 6cc1bb0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- YouTube: Added support for 'YouTube shorts' URLs. (#299)
- Fix: SevenTV emotes now resolve correctly. (#281, #288, #307)
- Fix: YouTube videos are no longer resolved as channels. (#284)
- Fix: Default resolver no longer crashes when provided url is broken. (#310)
- Dev: Improve BetterTTV emote tests. (#282)
- Minor: BetterTTV cache key changed from plural to singular form. (#282)
- Dev: Improve Twitch.tv clip tests. (#283)
Expand Down
1 change: 1 addition & 0 deletions internal/resolvers/default/link_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (r *LinkResolver) HandleRequest(w http.ResponseWriter, req *http.Request) {
"error", err,
)
}
return
}

for _, m := range r.customResolvers {
Expand Down
45 changes: 45 additions & 0 deletions internal/resolvers/default/link_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,49 @@ func TestLinkResolver(t *testing.T) {
})
}
})

c.Run("Request with early error", func(c *qt.C) {
tests := []struct {
inputReq *http.Request
inputLinkKey string
expected resolver.Response
}{
{
inputReq: newLinkResolverRequest(t, ctx, "GET", " :", nil),
inputLinkKey: ts.URL,
expected: resolver.Response{
Status: 500,
Link: "",
Message: `Could not fetch link info: Invalid URL`,
},
},
}

for _, test := range tests {
c.Run("", func(c *qt.C) {
respRec := httptest.NewRecorder()

router.ServeHTTP(respRec, test.inputReq)
resp := respRec.Result()
response := resolver.Response{}
err := json.NewDecoder(resp.Body).Decode(&response)
c.Assert(err, qt.IsNil)

c.Assert(response.Status, qt.Equals, test.expected.Status)
c.Assert(response.Link, qt.Equals, test.expected.Link)

unescapedTooltip, err := url.QueryUnescape(response.Tooltip)
c.Assert(err, qt.IsNil)

if test.expected.Tooltip != "" {
c.Assert(unescapedTooltip, MatchesRegexp, regexp.MustCompile(test.expected.Tooltip), qt.Commentf("%s does not match %s", unescapedTooltip, test.expected.Tooltip))
}
if test.expected.Message != "" {
c.Assert(response.Message, qt.Matches, test.expected.Message, qt.Commentf("%s does not match %s", response.Message, test.expected.Message))
}

c.Assert(pool.ExpectationsWereMet(), qt.IsNil)
})
}
})
}

0 comments on commit 6cc1bb0

Please sign in to comment.