validator_test.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package urlvalidator
  2. import "testing"
  3. func TestValidateURLFormat(t *testing.T) {
  4. if _, err := ValidateURLFormat("", false); err == nil {
  5. t.Fatalf("expected empty url to fail")
  6. }
  7. if _, err := ValidateURLFormat("://bad", false); err == nil {
  8. t.Fatalf("expected invalid url to fail")
  9. }
  10. if _, err := ValidateURLFormat("http://example.com", false); err == nil {
  11. t.Fatalf("expected http to fail when allow_insecure_http is false")
  12. }
  13. if _, err := ValidateURLFormat("https://example.com", false); err != nil {
  14. t.Fatalf("expected https to pass, got %v", err)
  15. }
  16. if _, err := ValidateURLFormat("http://example.com", true); err != nil {
  17. t.Fatalf("expected http to pass when allow_insecure_http is true, got %v", err)
  18. }
  19. if _, err := ValidateURLFormat("https://example.com:bad", true); err == nil {
  20. t.Fatalf("expected invalid port to fail")
  21. }
  22. // 验证末尾斜杠被移除
  23. normalized, err := ValidateURLFormat("https://example.com/", false)
  24. if err != nil {
  25. t.Fatalf("expected trailing slash url to pass, got %v", err)
  26. }
  27. if normalized != "https://example.com" {
  28. t.Fatalf("expected trailing slash to be removed, got %s", normalized)
  29. }
  30. // 验证多个末尾斜杠被移除
  31. normalized, err = ValidateURLFormat("https://example.com///", false)
  32. if err != nil {
  33. t.Fatalf("expected multiple trailing slashes to pass, got %v", err)
  34. }
  35. if normalized != "https://example.com" {
  36. t.Fatalf("expected all trailing slashes to be removed, got %s", normalized)
  37. }
  38. // 验证带路径的 URL 末尾斜杠被移除
  39. normalized, err = ValidateURLFormat("https://example.com/api/v1/", false)
  40. if err != nil {
  41. t.Fatalf("expected trailing slash url with path to pass, got %v", err)
  42. }
  43. if normalized != "https://example.com/api/v1" {
  44. t.Fatalf("expected trailing slash to be removed from path, got %s", normalized)
  45. }
  46. }
  47. func TestValidateHTTPURL(t *testing.T) {
  48. if _, err := ValidateHTTPURL("http://example.com", false, ValidationOptions{}); err == nil {
  49. t.Fatalf("expected http to fail when allow_insecure_http is false")
  50. }
  51. if _, err := ValidateHTTPURL("http://example.com", true, ValidationOptions{}); err != nil {
  52. t.Fatalf("expected http to pass when allow_insecure_http is true, got %v", err)
  53. }
  54. if _, err := ValidateHTTPURL("https://example.com", false, ValidationOptions{RequireAllowlist: true}); err == nil {
  55. t.Fatalf("expected require allowlist to fail when empty")
  56. }
  57. if _, err := ValidateHTTPURL("https://example.com", false, ValidationOptions{AllowedHosts: []string{"api.example.com"}}); err == nil {
  58. t.Fatalf("expected host not in allowlist to fail")
  59. }
  60. if _, err := ValidateHTTPURL("https://api.example.com", false, ValidationOptions{AllowedHosts: []string{"api.example.com"}}); err != nil {
  61. t.Fatalf("expected allowlisted host to pass, got %v", err)
  62. }
  63. if _, err := ValidateHTTPURL("https://sub.api.example.com", false, ValidationOptions{AllowedHosts: []string{"*.example.com"}}); err != nil {
  64. t.Fatalf("expected wildcard allowlist to pass, got %v", err)
  65. }
  66. if _, err := ValidateHTTPURL("https://localhost", false, ValidationOptions{AllowPrivate: false}); err == nil {
  67. t.Fatalf("expected localhost to be blocked when allow_private_hosts is false")
  68. }
  69. }
  70. func TestIsBlockedHost(t *testing.T) {
  71. for _, host := range []string{
  72. "localhost", "LOCALHOST", "foo.localhost", " 127.0.0.1 ",
  73. "127.0.0.1", "::1", "10.0.0.5", "172.16.0.1", "192.168.1.1",
  74. "169.254.169.254", "0.0.0.0", "::", "fe80::1", "fc00::1", "::ffff:127.0.0.1",
  75. } {
  76. if !IsBlockedHost(host) {
  77. t.Fatalf("expected %q to be blocked", host)
  78. }
  79. }
  80. for _, host := range []string{"example.com", "cdn.example.com", "93.184.216.34", "8.8.8.8", "2606:4700:4700::1111", ""} {
  81. if IsBlockedHost(host) {
  82. t.Fatalf("expected %q to be allowed", host)
  83. }
  84. }
  85. }