| 1 |
package ban |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/require" |
| 7 |
) |
| 8 |
|
| 9 |
func TestParseWeights(t *testing.T) { |
| 10 |
// Exact codes, a bare 3-digit HTTP status (normalized to a family), an exempt code, and "*". |
| 11 |
weights, err := ParseWeights([]string{"42909:10", "403:2", "42908:0", "*:1"}) |
| 12 |
require.NoError(t, err) |
| 13 |
require.Equal(t, Weights{"42909": 10, "403*": 2, "42908": 0, "*": 1}, weights) |
| 14 |
|
| 15 |
// A bare 3-digit HTTP status normalizes to its family. |
| 16 |
weights, err = ParseWeights([]string{"429:5"}) |
| 17 |
require.NoError(t, err) |
| 18 |
require.Equal(t, Weights{"429*": 5}, weights) |
| 19 |
|
| 20 |
// An explicit family key stays as-is. |
| 21 |
weights, err = ParseWeights([]string{"429*:5"}) |
| 22 |
require.NoError(t, err) |
| 23 |
require.Equal(t, Weights{"429*": 5}, weights) |
| 24 |
|
| 25 |
// Weight 0 is valid and means exempt. |
| 26 |
weights, err = ParseWeights([]string{"42908:0"}) |
| 27 |
require.NoError(t, err) |
| 28 |
require.Equal(t, Weights{"42908": 0}, weights) |
| 29 |
|
| 30 |
_, err = ParseWeights([]string{"401"}) // Missing weight |
| 31 |
require.Error(t, err) |
| 32 |
_, err = ParseWeights([]string{"401:-1"}) // Negative weight |
| 33 |
require.Error(t, err) |
| 34 |
_, err = ParseWeights([]string{"401:abc"}) // Non-integer weight |
| 35 |
require.Error(t, err) |
| 36 |
_, err = ParseWeights([]string{"abc:10"}) // Non-numeric key |
| 37 |
require.Error(t, err) |
| 38 |
_, err = ParseWeights([]string{"4*3:10"}) // Star not at the end |
| 39 |
require.Error(t, err) |
| 40 |
} |
| 41 |
|
| 42 |
func TestWeights_WeightFor(t *testing.T) { |
| 43 |
weights, err := ParseWeights([]string{"42908:0", "42903:0", "42905:0", "42910:0", "42909:10", "429*:1", "403*:2", "4*:1", "5*:1"}) |
| 44 |
require.NoError(t, err) |
| 45 |
// Longest-match-wins: exact 5-digit beats "429*" beats "4*" beats "*". |
| 46 |
require.Equal(t, 0, weights.WeightFor(42908)) |
| 47 |
require.Equal(t, 10, weights.WeightFor(42909)) |
| 48 |
require.Equal(t, 1, weights.WeightFor(42901)) |
| 49 |
require.Equal(t, 2, weights.WeightFor(40311)) |
| 50 |
require.Equal(t, 1, weights.WeightFor(40011)) |
| 51 |
require.Equal(t, 1, weights.WeightFor(50312)) |
| 52 |
// No rule matches (this config has 4*/5* but no "*"), so the implied default weight 1 applies. |
| 53 |
require.Equal(t, 1, weights.WeightFor(30012)) |
| 54 |
} |
| 55 |
|
| 56 |
func TestWeights_WeightFor_NoStarRuleImpliesWeight1(t *testing.T) { |
| 57 |
// With no "*" rule, a code that matches nothing defaults to weight 1 (can be banned), so the |
| 58 |
// feature can't be silently turned into a no-op by forgetting "*". Explicit codes still win. |
| 59 |
weights, err := ParseWeights([]string{"42908:0", "42909:10"}) |
| 60 |
require.NoError(t, err) |
| 61 |
require.Equal(t, 0, weights.WeightFor(42908)) // explicitly exempt |
| 62 |
require.Equal(t, 10, weights.WeightFor(42909)) // explicit |
| 63 |
require.Equal(t, 1, weights.WeightFor(42901)) // unmatched -> implied 1 |
| 64 |
require.Equal(t, 1, weights.WeightFor(40001)) // unmatched -> implied 1 |
| 65 |
} |
| 66 |
|
| 67 |
func TestWeights_WeightFor_ExplicitStarZeroExemptsAll(t *testing.T) { |
| 68 |
// An explicit "*:0" is the opt-out: exempt everything not otherwise weighted. |
| 69 |
weights, err := ParseWeights([]string{"42909:10", "*:0"}) |
| 70 |
require.NoError(t, err) |
| 71 |
require.Equal(t, 10, weights.WeightFor(42909)) // explicit |
| 72 |
require.Equal(t, 0, weights.WeightFor(42901)) // *:0 -> exempt everything else |
| 73 |
} |