MCPcopy
hub / github.com/pocketbase/pocketbase / TestRateLimitsFindRateLimitRule

Function TestRateLimitsFindRateLimitRule

core/settings_model_test.go:728–782  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

726}
727
728func TestRateLimitsFindRateLimitRule(t *testing.T) {
729 t.Parallel()
730
731 limits := core.RateLimitsConfig{
732 Rules: []core.RateLimitRule{
733 {Label: "abc"},
734 {Label: "def", Audience: core.RateLimitRuleAudienceGuest},
735 {Label: "/test/a", Audience: core.RateLimitRuleAudienceGuest},
736 {Label: "POST /test/a"},
737 {Label: "/test/a/", Audience: core.RateLimitRuleAudienceAuth},
738 {Label: "POST /test/a/"},
739 },
740 }
741
742 scenarios := []struct {
743 labels []string
744 audience []string
745 expected string
746 }{
747 {[]string{}, []string{}, ""},
748 {[]string{"missing"}, []string{}, ""},
749 {[]string{"abc"}, []string{}, "abc"},
750 {[]string{"abc"}, []string{core.RateLimitRuleAudienceGuest}, ""},
751 {[]string{"abc"}, []string{core.RateLimitRuleAudienceAuth}, ""},
752 {[]string{"def"}, []string{core.RateLimitRuleAudienceGuest}, "def"},
753 {[]string{"def"}, []string{core.RateLimitRuleAudienceAuth}, ""},
754 {[]string{"/test"}, []string{}, ""},
755 {[]string{"/test/a"}, []string{}, "/test/a"},
756 {[]string{"/test/a"}, []string{core.RateLimitRuleAudienceAuth}, "/test/a/"},
757 {[]string{"/test/a"}, []string{core.RateLimitRuleAudienceGuest}, "/test/a"},
758 {[]string{"GET /test/a"}, []string{}, ""},
759 {[]string{"POST /test/a"}, []string{}, "POST /test/a"},
760 {[]string{"/test/a/b/c"}, []string{}, "/test/a/"},
761 {[]string{"/test/a/b/c"}, []string{core.RateLimitRuleAudienceAuth}, "/test/a/"},
762 {[]string{"/test/a/b/c"}, []string{core.RateLimitRuleAudienceGuest}, ""},
763 {[]string{"GET /test/a/b/c"}, []string{}, ""},
764 {[]string{"POST /test/a/b/c"}, []string{}, "POST /test/a/"},
765 {[]string{"/test/a", "abc"}, []string{}, "/test/a"}, // priority checks
766 }
767
768 for _, s := range scenarios {
769 t.Run(strings.Join(s.labels, "_")+":"+strings.Join(s.audience, "_"), func(t *testing.T) {
770 rule, ok := limits.FindRateLimitRule(s.labels, s.audience...)
771
772 hasLabel := rule.Label != ""
773 if hasLabel != ok {
774 t.Fatalf("Expected hasLabel %v, got %v", hasLabel, ok)
775 }
776
777 if rule.Label != s.expected {
778 t.Fatalf("Expected rule with label %q, got %q", s.expected, rule.Label)
779 }
780 })
781 }
782}
783
784func TestRateLimitRuleValidate(t *testing.T) {
785 t.Parallel()

Callers

nothing calls this directly

Calls 2

FindRateLimitRuleMethod · 0.95
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…