proxy_probe_config_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //go:build unit
  2. package config
  3. import (
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestNormalizeProxyProbeURLs(t *testing.T) {
  8. t.Parallel()
  9. got, err := normalizeProxyProbeURLs([]ProbeURLConfig{
  10. {URL: " https://chatgpt.com/cdn-cgi/trace ", Parser: " CHATGPT-TRACE "},
  11. {URL: "https://api64.ipify.org?format=json", Parser: "ipify"},
  12. })
  13. require.NoError(t, err)
  14. require.Equal(t, []ProbeURLConfig{
  15. {URL: "https://chatgpt.com/cdn-cgi/trace", Parser: "chatgpt-trace"},
  16. {URL: "https://api64.ipify.org?format=json", Parser: "ipify"},
  17. }, got)
  18. }
  19. func TestNormalizeProxyProbeURLsRejectsInvalidEntries(t *testing.T) {
  20. t.Parallel()
  21. tests := []struct {
  22. name string
  23. target ProbeURLConfig
  24. wantErr string
  25. }{
  26. {name: "missing URL", target: ProbeURLConfig{Parser: "ipify"}, wantErr: "url is required"},
  27. {name: "missing parser", target: ProbeURLConfig{URL: "https://example.com"}, wantErr: "parser is required"},
  28. {name: "unknown parser", target: ProbeURLConfig{URL: "https://example.com", Parser: "ip_api"}, wantErr: "unsupported parser"},
  29. {name: "relative URL", target: ProbeURLConfig{URL: "/cdn-cgi/trace", Parser: "chatgpt-trace"}, wantErr: "invalid url"},
  30. {name: "unsupported scheme", target: ProbeURLConfig{URL: "ftp://example.com/file", Parser: "ipify"}, wantErr: "scheme must be http or https"},
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. t.Parallel()
  35. _, err := normalizeProxyProbeURLs([]ProbeURLConfig{tt.target})
  36. require.ErrorContains(t, err, tt.wantErr)
  37. })
  38. }
  39. }