-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* play around with default resolver tests * Fix tests with custom regexp checker * Add changelog entry
- Loading branch information
Showing
7 changed files
with
233 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package defaultresolver | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/Chatterino/api/internal/logger" | ||
"github.com/Chatterino/api/pkg/config" | ||
"github.com/Chatterino/api/pkg/resolver" | ||
qt "github.com/frankban/quicktest" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/pashagolub/pgxmock" | ||
) | ||
|
||
func newRequest(t *testing.T, ctx context.Context, method string, url string, payload io.Reader) *http.Request { | ||
req, err := http.NewRequestWithContext(ctx, method, url, payload) | ||
if err != nil { | ||
t.Fatal("Unable to create request") | ||
} | ||
|
||
return req | ||
} | ||
|
||
func newLinkResolverRequest(t *testing.T, ctx context.Context, method string, u string, payload io.Reader) *http.Request { | ||
finalURL := "/link_resolver/" + url.QueryEscape(u) | ||
fmt.Println("Final URL", finalURL) | ||
req, err := http.NewRequestWithContext(ctx, method, finalURL, payload) | ||
if err != nil { | ||
t.Fatal("Unable to create request") | ||
} | ||
|
||
return req | ||
} | ||
|
||
func TestLinkResolver(t *testing.T) { | ||
ctx := logger.OnContext(context.Background(), logger.NewTest()) | ||
c := qt.New(t) | ||
|
||
cfg := config.APIConfig{ | ||
MaxContentLength: 1024, | ||
} | ||
pool, _ := pgxmock.NewPool() | ||
|
||
resolver.InitializeStaticResponses(ctx, cfg) | ||
|
||
router := chi.NewRouter() | ||
|
||
r := New(ctx, cfg, pool, nil) | ||
|
||
router.Get("/link_resolver/{url}", r.HandleRequest) | ||
router.Get("/thumbnail/{url}", r.HandleThumbnailRequest) | ||
|
||
var resolverResponses = map[string]string{} | ||
|
||
resolverResponses["/"] = "<html><head><title>/ title</title></head><body>xD</body></html>" | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if response, ok := resolverResponses[r.URL.Path]; ok { | ||
w.Write([]byte(response)) | ||
return | ||
} | ||
http.Error(w, http.StatusText(404), 404) | ||
})) | ||
defer ts.Close() | ||
|
||
c.Run("Request", func(c *qt.C) { | ||
tests := []struct { | ||
inputReq *http.Request | ||
inputLinkKey string | ||
expected resolver.Response | ||
}{ | ||
{ | ||
inputReq: newLinkResolverRequest(t, ctx, "GET", ts.URL, nil), | ||
inputLinkKey: ts.URL, | ||
expected: resolver.Response{ | ||
Status: 200, | ||
Link: ts.URL, | ||
Tooltip: `<div style="text-align: left;"> | ||
<b>/ title</b><hr> | ||
<b>URL:</b> http://127\.0\.0\.1:[\d]{2,7}</div>`, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
c.Run("", func(c *qt.C) { | ||
respRec := httptest.NewRecorder() | ||
|
||
pool.ExpectQuery("SELECT").WillReturnError(pgx.ErrNoRows) | ||
pool.ExpectExec("INSERT INTO cache"). | ||
WithArgs("default:link:"+test.inputLinkKey, pgxmock.AnyArg(), pgxmock.AnyArg()). | ||
WillReturnResult(pgxmock.NewResult("INSERT", 1)) | ||
|
||
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) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package defaultresolver | ||
|
||
import ( | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestTooltipData(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
c.Run("Truncate", func(c *qt.C) { | ||
tests := []struct { | ||
input tooltipData | ||
expected tooltipData | ||
}{ | ||
{ | ||
input: tooltipData{ | ||
Title: "foo", | ||
Description: "bar", | ||
}, | ||
expected: tooltipData{ | ||
Title: "foo", | ||
Description: "bar", | ||
}, | ||
}, | ||
{ | ||
input: tooltipData{ | ||
Title: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy", | ||
Description: "bar", | ||
}, | ||
expected: tooltipData{ | ||
Title: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy", | ||
Description: "bar", | ||
}, | ||
}, | ||
{ | ||
input: tooltipData{ | ||
Title: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyz", | ||
Description: "bar", | ||
}, | ||
expected: tooltipData{ | ||
Title: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx…", | ||
Description: "bar", | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
c.Run("", func(c *qt.C) { | ||
test.input.Truncate() | ||
c.Assert(test.input, qt.DeepEquals, test.expected) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package defaultresolver | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
type argNames []string | ||
|
||
func (a argNames) ArgNames() []string { | ||
return a | ||
} | ||
|
||
var MatchesRegexp qt.Checker = ®expChecker{ | ||
argNames: []string{"got value", "regexp"}, | ||
} | ||
|
||
type regexpChecker struct { | ||
argNames | ||
} | ||
|
||
// match checks that the given error message matches the given pattern. | ||
func match(got string, pattern *regexp.Regexp, msg string, note func(key string, value interface{})) error { | ||
if pattern.MatchString(got) { | ||
return nil | ||
} | ||
|
||
return errors.New(msg) | ||
} | ||
|
||
func (c *regexpChecker) Check(got interface{}, args []interface{}, note func(key string, value interface{})) error { | ||
switch pattern := args[0].(type) { | ||
case *regexp.Regexp: | ||
switch v := got.(type) { | ||
case string: | ||
return match(v, pattern, "value does not match regexp", note) | ||
case fmt.Stringer: | ||
return match(v.String(), pattern, "value.String() does not match regexp", note) | ||
} | ||
return qt.BadCheckf("value is not a string or a fmt.Stringer") | ||
} | ||
return qt.BadCheckf("pattern is not a *regexp.Regexp") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters