Validate Weather API endpoints and retries

This commit is contained in:
2026-08-13 00:32:22 +00:00
parent 2d956f7315
commit 3c1ebab289
7 changed files with 125 additions and 12 deletions

View File

@@ -82,6 +82,36 @@ func TestDefaults(t *testing.T) {
}
}
func TestWeatherAPIBaseURLValidation(t *testing.T) {
tests := []struct {
name string
baseURL string
wantErr string
}{
{name: "local HTTP", baseURL: "http://127.0.0.1:8080/weather/"},
{name: "local HTTPS", baseURL: "https://127.0.0.1:8443/weather/"},
{name: "unsupported scheme", baseURL: "ftp://weather.example.test/", wantErr: "weather_api.base_url must use http or https"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := Defaults()
cfg.WeatherAPI.BaseURL = tt.baseURL
err := Validate(cfg)
if tt.wantErr == "" {
if err != nil {
t.Fatalf("Validate() error = %v", err)
}
return
}
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("Validate() error = %v, want %q", err, tt.wantErr)
}
})
}
}
func TestOutputDirectoryLoading(t *testing.T) {
tests := []struct {
name string

View File

@@ -23,6 +23,9 @@ func Validate(cfg Config) error {
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
return fmt.Errorf("weather_api.base_url must be an absolute URL")
}
if !strings.EqualFold(parsed.Scheme, "http") && !strings.EqualFold(parsed.Scheme, "https") {
return fmt.Errorf("weather_api.base_url must use http or https")
}
}
if cfg.WeatherAPI.Timeout <= 0 {
return fmt.Errorf("weather_api.timeout must be greater than zero")