All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m40s
Disable default read and write timeouts (set to 0s) to prevent Go from prematurely closing connections during large multi-GB uploads. Introduce `WARPBOX_READ_HEADER_TIMEOUT` (defaulting to 15s) to protect against slowloris-style attacks while still allowing long-running uploads to complete. Update documentation and example configurations accordingly.
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package config
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParseMegabytes(t *testing.T) {
|
|
tests := map[string]int64{
|
|
"0.5": 512 * 1024,
|
|
"0.5Mb": 512 * 1024,
|
|
"1mb": 1024 * 1024,
|
|
"1MB": 1024 * 1024,
|
|
"1.5Mb": 1536 * 1024,
|
|
" 2 ": 2 * 1024 * 1024,
|
|
}
|
|
|
|
for input, want := range tests {
|
|
got, err := parseMegabytes(input)
|
|
if err != nil {
|
|
t.Fatalf("parseMegabytes(%q) returned error: %v", input, err)
|
|
}
|
|
if got != want {
|
|
t.Fatalf("parseMegabytes(%q) = %d, want %d", input, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseMegabytesRejectsInvalidValues(t *testing.T) {
|
|
tests := []string{"", "0", "-1", "abc"}
|
|
|
|
for _, input := range tests {
|
|
if _, err := parseMegabytes(input); err == nil {
|
|
t.Fatalf("parseMegabytes(%q) returned nil error", input)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEnvBool(t *testing.T) {
|
|
t.Setenv("WARPBOX_TEST_BOOL", "false")
|
|
if got := envBool("WARPBOX_TEST_BOOL", true); got {
|
|
t.Fatalf("envBool() = true, want false")
|
|
}
|
|
|
|
t.Setenv("WARPBOX_TEST_BOOL", "1")
|
|
if got := envBool("WARPBOX_TEST_BOOL", false); !got {
|
|
t.Fatalf("envBool() = false, want true")
|
|
}
|
|
|
|
t.Setenv("WARPBOX_TEST_BOOL", "not-a-bool")
|
|
if got := envBool("WARPBOX_TEST_BOOL", true); !got {
|
|
t.Fatalf("envBool() did not fall back to true")
|
|
}
|
|
}
|
|
|
|
func TestLoadDefaultsUseLargeUploadFriendlyTimeouts(t *testing.T) {
|
|
t.Setenv("WARPBOX_BASE_URL", "http://example.test")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("Load returned error: %v", err)
|
|
}
|
|
if cfg.ReadHeaderTimeout != 15*time.Second {
|
|
t.Fatalf("ReadHeaderTimeout = %s, want 15s", cfg.ReadHeaderTimeout)
|
|
}
|
|
if cfg.ReadTimeout != 0 {
|
|
t.Fatalf("ReadTimeout = %s, want 0 for long uploads", cfg.ReadTimeout)
|
|
}
|
|
if cfg.WriteTimeout != 0 {
|
|
t.Fatalf("WriteTimeout = %s, want 0 for long uploads", cfg.WriteTimeout)
|
|
}
|
|
}
|