|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestGetDefaultPayloadSizeThreshold(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + envValue string |
| 14 | + expected int |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "no env var - returns default", |
| 18 | + envValue: "", |
| 19 | + expected: defaultPayloadSizeThreshold, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "valid env var", |
| 23 | + envValue: "2048", |
| 24 | + expected: 2048, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "very large threshold", |
| 28 | + envValue: "10240", |
| 29 | + expected: 10240, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "small threshold", |
| 33 | + envValue: "512", |
| 34 | + expected: 512, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "invalid value - non-numeric", |
| 38 | + envValue: "invalid", |
| 39 | + expected: defaultPayloadSizeThreshold, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "invalid value - negative", |
| 43 | + envValue: "-100", |
| 44 | + expected: defaultPayloadSizeThreshold, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "invalid value - zero", |
| 48 | + envValue: "0", |
| 49 | + expected: defaultPayloadSizeThreshold, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tt := range tests { |
| 54 | + t.Run(tt.name, func(t *testing.T) { |
| 55 | + // Set or unset environment variable |
| 56 | + if tt.envValue != "" { |
| 57 | + os.Setenv("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD", tt.envValue) |
| 58 | + defer os.Unsetenv("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD") |
| 59 | + } else { |
| 60 | + os.Unsetenv("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD") |
| 61 | + } |
| 62 | + |
| 63 | + result := getDefaultPayloadSizeThreshold() |
| 64 | + assert.Equal(t, tt.expected, result, "Threshold should match expected value") |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestPayloadSizeThresholdFlagDefault(t *testing.T) { |
| 70 | + // Ensure environment is clean |
| 71 | + os.Unsetenv("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD") |
| 72 | + |
| 73 | + result := getDefaultPayloadSizeThreshold() |
| 74 | + assert.Equal(t, 1024, result, "Default should be 1024 bytes") |
| 75 | +} |
| 76 | + |
| 77 | +func TestPayloadSizeThresholdEnvVar(t *testing.T) { |
| 78 | + // Test that environment variable overrides default |
| 79 | + os.Setenv("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD", "4096") |
| 80 | + defer os.Unsetenv("MCP_GATEWAY_PAYLOAD_SIZE_THRESHOLD") |
| 81 | + |
| 82 | + result := getDefaultPayloadSizeThreshold() |
| 83 | + assert.Equal(t, 4096, result, "Environment variable should override default") |
| 84 | +} |
0 commit comments