|
4 | 4 | "os" |
5 | 5 | "testing" |
6 | 6 | "time" |
| 7 | + |
| 8 | + "github.com/Owloops/updo/net" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | func TestLoadConfig(t *testing.T) { |
@@ -151,6 +153,104 @@ name = "Inherit" |
151 | 153 | } |
152 | 154 | } |
153 | 155 |
|
| 156 | +// TestBodySizeLimitInheritance verifies that a target inherits |
| 157 | +// global.body_size_limit when unset, overrides when explicitly set, |
| 158 | +// and supports 0 as "unlimited". |
| 159 | +func TestBodySizeLimitInheritance(t *testing.T) { |
| 160 | + configContent := ` |
| 161 | +[global] |
| 162 | +body_size_limit = 2097152 |
| 163 | +
|
| 164 | +[[targets]] |
| 165 | +url = "https://inherit.example.com" |
| 166 | +name = "Inherit" |
| 167 | +
|
| 168 | +[[targets]] |
| 169 | +url = "https://override.example.com" |
| 170 | +name = "Override" |
| 171 | +body_size_limit = 524288 |
| 172 | +
|
| 173 | +[[targets]] |
| 174 | +url = "https://unlimited.example.com" |
| 175 | +name = "Unlimited" |
| 176 | +body_size_limit = 0 |
| 177 | +` |
| 178 | + |
| 179 | + tmpFile, err := os.CreateTemp("", "test-config-bodysize-*.toml") |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("Failed to create temp file: %v", err) |
| 182 | + } |
| 183 | + defer func() { |
| 184 | + if err := os.Remove(tmpFile.Name()); err != nil { |
| 185 | + t.Logf("Failed to remove temp file: %v", err) |
| 186 | + } |
| 187 | + }() |
| 188 | + |
| 189 | + if _, err := tmpFile.WriteString(configContent); err != nil { |
| 190 | + t.Fatalf("Failed to write config: %v", err) |
| 191 | + } |
| 192 | + if err := tmpFile.Close(); err != nil { |
| 193 | + t.Fatalf("Failed to close temp file: %v", err) |
| 194 | + } |
| 195 | + |
| 196 | + cfg, err := LoadConfig(tmpFile.Name()) |
| 197 | + if err != nil { |
| 198 | + t.Fatalf("LoadConfig failed: %v", err) |
| 199 | + } |
| 200 | + |
| 201 | + if cfg.Global.BodySizeLimit != 2097152 { |
| 202 | + t.Errorf("Global.BodySizeLimit = %d, want 2097152", cfg.Global.BodySizeLimit) |
| 203 | + } |
| 204 | + |
| 205 | + if got := Int64Val(cfg.Targets[0].BodySizeLimit, -1); got != 2097152 { |
| 206 | + t.Errorf("Inherit target: BodySizeLimit = %d, want 2097152 (inherited)", got) |
| 207 | + } |
| 208 | + if got := Int64Val(cfg.Targets[1].BodySizeLimit, -1); got != 524288 { |
| 209 | + t.Errorf("Override target: BodySizeLimit = %d, want 524288", got) |
| 210 | + } |
| 211 | + if got := Int64Val(cfg.Targets[2].BodySizeLimit, -1); got != 0 { |
| 212 | + t.Errorf("Unlimited target: BodySizeLimit = %d, want 0 (explicit unlimited)", got) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +// TestBodySizeLimitDefault verifies that when neither global nor target |
| 217 | +// set body_size_limit, the viper default of net.DefaultBodySizeLimit applies. |
| 218 | +func TestBodySizeLimitDefault(t *testing.T) { |
| 219 | + configContent := ` |
| 220 | +[[targets]] |
| 221 | +url = "https://example.com" |
| 222 | +` |
| 223 | + |
| 224 | + tmpFile, err := os.CreateTemp("", "test-config-bodysize-default-*.toml") |
| 225 | + if err != nil { |
| 226 | + t.Fatalf("Failed to create temp file: %v", err) |
| 227 | + } |
| 228 | + defer func() { |
| 229 | + if err := os.Remove(tmpFile.Name()); err != nil { |
| 230 | + t.Logf("Failed to remove temp file: %v", err) |
| 231 | + } |
| 232 | + }() |
| 233 | + |
| 234 | + if _, err := tmpFile.WriteString(configContent); err != nil { |
| 235 | + t.Fatalf("Failed to write config: %v", err) |
| 236 | + } |
| 237 | + if err := tmpFile.Close(); err != nil { |
| 238 | + t.Fatalf("Failed to close temp file: %v", err) |
| 239 | + } |
| 240 | + |
| 241 | + cfg, err := LoadConfig(tmpFile.Name()) |
| 242 | + if err != nil { |
| 243 | + t.Fatalf("LoadConfig failed: %v", err) |
| 244 | + } |
| 245 | + |
| 246 | + if cfg.Global.BodySizeLimit != net.DefaultBodySizeLimit { |
| 247 | + t.Errorf("Global.BodySizeLimit = %d, want %d (default)", cfg.Global.BodySizeLimit, net.DefaultBodySizeLimit) |
| 248 | + } |
| 249 | + if got := Int64Val(cfg.Targets[0].BodySizeLimit, -1); got != net.DefaultBodySizeLimit { |
| 250 | + t.Errorf("Target BodySizeLimit = %d, want %d (default inherited)", got, net.DefaultBodySizeLimit) |
| 251 | + } |
| 252 | +} |
| 253 | + |
154 | 254 | func TestLoadConfigDefaults(t *testing.T) { |
155 | 255 | configContent := ` |
156 | 256 | [[targets]] |
|
0 commit comments