github.com/avenga/couper@v1.12.2/handler/ratelimit/config_internal_test.go (about)

     1  package ratelimit
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/avenga/couper/config"
     8  )
     9  
    10  func TestConfig_Errors(t *testing.T) {
    11  	type testCase struct {
    12  		configured config.RateLimits
    13  		expMessage string
    14  	}
    15  
    16  	var (
    17  		foo     string = "foo"
    18  		min     string = "1m"
    19  		sec     string = "60s"
    20  		neg     string = "-1s"
    21  		num     uint   = 123
    22  		zeroStr string = "0s"
    23  		zeroInt uint   = 0
    24  	)
    25  
    26  	for _, tc := range []testCase{
    27  		{
    28  			[]*config.RateLimit{
    29  				{PerPeriod: num, PeriodWindow: ""},
    30  			},
    31  			"'period' must not be 0 (zero)",
    32  		},
    33  		{
    34  			[]*config.RateLimit{
    35  				{Period: min, PeriodWindow: ""},
    36  			},
    37  			"'per_period' must not be 0 (zero)",
    38  		},
    39  		{
    40  			[]*config.RateLimit{
    41  				{Period: foo, PerPeriod: num, PeriodWindow: ""},
    42  			},
    43  			`period: time: invalid duration "foo"`,
    44  		},
    45  		{
    46  			[]*config.RateLimit{
    47  				{Period: neg, PerPeriod: num, PeriodWindow: ""},
    48  			},
    49  			`period: cannot be negative: '-1s'`,
    50  		},
    51  		{
    52  			[]*config.RateLimit{
    53  				{Period: zeroStr, PerPeriod: num, PeriodWindow: ""},
    54  			},
    55  			`'period' must not be 0 (zero)`,
    56  		},
    57  		{
    58  			[]*config.RateLimit{
    59  				{Period: min, PerPeriod: zeroInt, PeriodWindow: ""},
    60  			},
    61  			`'per_period' must not be 0 (zero)`,
    62  		},
    63  		{
    64  			[]*config.RateLimit{
    65  				{Period: min, PerPeriod: num, PeriodWindow: ""},
    66  				{Period: sec, PerPeriod: num, PeriodWindow: ""},
    67  			},
    68  			`duplicate period ("60s") found`,
    69  		},
    70  		{
    71  			[]*config.RateLimit{
    72  				{Period: min, PerPeriod: num, PeriodWindow: "test"},
    73  			},
    74  			`unsupported 'period_window' ("test") given`,
    75  		},
    76  		{
    77  			[]*config.RateLimit{
    78  				{Period: min, PerPeriod: num, PeriodWindow: "", Mode: "test"},
    79  			},
    80  			`unsupported 'mode' ("test") given`,
    81  		},
    82  	} {
    83  		_, err := ConfigureRateLimits(context.TODO(), tc.configured, nil)
    84  		if err == nil {
    85  			t.Fatal("Missing error")
    86  		}
    87  
    88  		if got := err.Error(); got != tc.expMessage {
    89  			t.Errorf("exp: %q\ngot: %q", tc.expMessage, got)
    90  		}
    91  	}
    92  }