mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-06-05 23:03:48 +00:00
cedee416a8
* General improvements and bug fixes. * Improve tests coverage. * fixup! Improve tests coverage. * Update README.md with latest changes. * Fix the uint32 * Resolve issue with race condition for logging. * fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * Fix the test of the rate limiter * Add default ratelimit.json file * Update dependencies. * Significant refactor. * fixup! Significant refactor. * fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025
107 lines
3.2 KiB
Go
107 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/goccy/go-json"
|
|
)
|
|
|
|
// Test_IntervalConversion tests the conversion of various interval formats
|
|
func (suite *Tests) Test_IntervalConversion() {
|
|
// Test cases for string-based intervals
|
|
testCases := []struct {
|
|
name string
|
|
jsonString string
|
|
expectedDuration time.Duration
|
|
shouldError bool
|
|
}{
|
|
{
|
|
name: "second string",
|
|
jsonString: `{"interval": "second", "req": 100}`,
|
|
expectedDuration: time.Second,
|
|
shouldError: false,
|
|
},
|
|
{
|
|
name: "minute string",
|
|
jsonString: `{"interval": "minute", "req": 5}`,
|
|
expectedDuration: time.Minute,
|
|
shouldError: false,
|
|
},
|
|
{
|
|
name: "hour string",
|
|
jsonString: `{"interval": "hour", "req": 1000}`,
|
|
expectedDuration: time.Hour,
|
|
shouldError: false,
|
|
},
|
|
{
|
|
name: "day string",
|
|
jsonString: `{"interval": "day", "req": 10000}`,
|
|
expectedDuration: 24 * time.Hour,
|
|
shouldError: false,
|
|
},
|
|
{
|
|
name: "numeric value in seconds",
|
|
jsonString: `{"interval": 30, "req": 50}`,
|
|
expectedDuration: 30 * time.Second,
|
|
shouldError: false,
|
|
},
|
|
{
|
|
name: "go duration format",
|
|
jsonString: `{"interval": "5s", "req": 50}`,
|
|
expectedDuration: 5 * time.Second,
|
|
shouldError: false,
|
|
},
|
|
{
|
|
name: "invalid format",
|
|
jsonString: `{"interval": "invalid", "req": 100}`,
|
|
expectedDuration: 0,
|
|
shouldError: true,
|
|
},
|
|
}
|
|
|
|
// Run the tests
|
|
for _, tc := range testCases {
|
|
suite.Run(tc.name, func() {
|
|
var config RateLimitConfig
|
|
err := json.Unmarshal([]byte(tc.jsonString), &config)
|
|
|
|
if tc.shouldError {
|
|
suite.Error(err, "Expected error for invalid format")
|
|
} else {
|
|
suite.NoError(err, "Unexpected error during unmarshal")
|
|
suite.Equal(tc.expectedDuration, config.Interval,
|
|
fmt.Sprintf("Expected %v but got %v", tc.expectedDuration, config.Interval))
|
|
suite.NotNil(config.Interval, "Interval should not be nil")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Test_LoadRatelimitConfigFile tests the actual loading of the configuration file
|
|
func (suite *Tests) Test_LoadRatelimitConfigFile() {
|
|
// Setup
|
|
cfg = &config{}
|
|
parseConfig()
|
|
err := loadRatelimitConfig()
|
|
suite.NoError(err, "Should load ratelimit config without error")
|
|
|
|
// Verify that rate limits were loaded
|
|
suite.NotEmpty(rateLimits, "Rate limits should not be empty")
|
|
|
|
// Check specific roles
|
|
suite.Contains(rateLimits, "admin", "Should contain admin role")
|
|
suite.Contains(rateLimits, "guest", "Should contain guest role")
|
|
suite.Contains(rateLimits, "-", "Should contain default role")
|
|
|
|
// Verify interval values
|
|
suite.Equal(time.Second, rateLimits["admin"].Interval, "Admin should have 1 second interval")
|
|
suite.Equal(time.Second, rateLimits["guest"].Interval, "Guest should have 1 second interval")
|
|
suite.Equal(time.Minute, rateLimits["-"].Interval, "Default role should have 1 minute interval")
|
|
|
|
// Verify request limits
|
|
suite.Equal(100, rateLimits["admin"].Req, "Admin should allow 100 req/second")
|
|
suite.Equal(3, rateLimits["guest"].Req, "Guest should allow 3 req/second")
|
|
suite.Equal(10, rateLimits["-"].Req, "Default role should allow 10 req/minute")
|
|
}
|