Files
mcp/internal/rules/bind_rules_test.go
T

49 lines
1.1 KiB
Go
Raw Normal View History

package rules
import "testing"
const urlParamSnippet = `package handler
import "github.com/go-chi/chi/v5"
func (h *Handler) f(r *http.Request) {
id := chi.URLParam(r, "id")
_ = id
}
`
const urlQuerySnippet = `package handler
func (h *Handler) f(r *http.Request) {
page := r.URL.Query().Get("page")
_ = page
}
`
const boundHandlerSnippet = `package handler
import "code.nochebuena.dev/einherjar/web/httputil"
func (h *Handler) f() {
_ = httputil.Bind(h.v, h.logger, h.get)
}
`
func TestPreferBindFiresOnURLParam(t *testing.T) {
if got := findingsFor(Run(urlParamSnippet), "httputil.prefer-bind"); len(got) == 0 {
t.Fatal("httputil.prefer-bind did not fire on chi.URLParam")
}
}
func TestPreferBindFiresOnURLQuery(t *testing.T) {
if got := findingsFor(Run(urlQuerySnippet), "httputil.prefer-bind"); len(got) == 0 {
t.Fatal("httputil.prefer-bind did not fire on r.URL.Query()")
}
}
func TestPreferBindNoFalsePositiveOnBind(t *testing.T) {
if got := findingsFor(Run(boundHandlerSnippet), "httputil.prefer-bind"); len(got) != 0 {
t.Errorf("httputil.prefer-bind false-positived on a Bind handler: %+v", got)
}
}