cabinet/internal/router/rate_test.go

39 lines
1.1 KiB
Go

package router
import (
"testing"
"github.com/matryer/is"
)
func TestLimit(t *testing.T) {
localhost := "127.0.0.1:80"
t.Run("Requests Per Minute", func(t *testing.T) {
assert := is.New(t)
limit := NewLimit(5, 5, 10, 10)
for idx := 0; idx < 5; idx++ {
assert.Equal(limit.hasHitRequestLimit(localhost), false) // Five requests is okay
}
assert.Equal(limit.hasHitRequestLimit(localhost), true) // A sixth requests is not okay
})
t.Run("Size Per Minute", func(t *testing.T) {
assert := is.New(t)
limit := NewLimit(5, 5, 5, 10)
for idx := 0; idx < 5; idx++ {
assert.Equal(limit.hasHitSizeLimit(localhost, 1), false) // Five requests is okay
}
assert.Equal(limit.hasHitSizeLimit(localhost, 1), true) // A sixth requests is not okay
})
t.Run("Size Per Minute w/Burst", func(t *testing.T) {
assert := is.New(t)
limit := NewLimit(5, 5, 10, 10)
for idx := 0; idx < 10; idx++ {
assert.Equal(limit.hasHitSizeLimit(localhost, 1), false) // Ten requests is okay (with Burst)
}
assert.Equal(limit.hasHitSizeLimit(localhost, 1), true) // An eleventh requests is not okay (with Burst)
})
}