27 lines
799 B
Go
27 lines
799 B
Go
package ratelimit
|
|||
|
|
|
||
|
|
import "testing"
|
||
|
|
|
||
|
|
func TestIdentifierHashDoesNotExposeIdentifier(t *testing.T) {
|
||
|
|
identifier := "192.0.2.10"
|
||
|
|
first := IdentifierHash(identifier)
|
||
|
|
if len(first) != 32 || first == identifier || first != IdentifierHash(identifier) {
|
||
|
|
t.Fatalf("unexpected identifier hash: %s", first)
|
||
|
|
}
|
||
|
|
if first == IdentifierHash("192.0.2.11") {
|
||
|
|
t.Fatal("different identifiers should not share a hash")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNewRejectsInvalidPolicy(t *testing.T) {
|
||
|
|
if _, err := New(nil, "", Policy{}); err == nil {
|
||
|
|
t.Fatal("invalid limiter configuration should fail")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNewPoliciesRejectsInvalidScopedPolicy(t *testing.T) {
|
||
|
|
if _, err := NewPolicies(nil, "prefix", map[string]Policy{"device": {RatePerSecond: 1, Burst: 1}}); err == nil {
|
||
|
|
t.Fatal("nil Redis client should fail")
|
||
|
|
}
|
||
|
|
}
|