fix(url): detect local file paths and convert to file:// URLs
- commit
- 5738c09ea84cb386c7022b4ce4df8c49167274a1
- author
- bnema · · 2025-12-25 21:43 UTC
- parent
- 24473b6
- signature
- GPG Unverified
2B82207D370127A0The key is unknown
2 files · +282 -0
M internal/domain/url/normalize.go +22 -0
| @@ -4,11 +4,24 @@ package url | |||
| 4 | 4 | import ( | |
| 5 | 5 | "fmt" | |
| 6 | 6 | "net/url" | |
| 7 | + | "os" | |
| 8 | + | "path/filepath" | |
| 7 | 9 | "strings" | |
| 8 | 10 | ) | |
| 9 | 11 | ||
| 12 | + | // expandHome expands ~ prefix to user's home directory. | |
| 13 | + | func expandHome(path string) string { | |
| 14 | + | if strings.HasPrefix(path, "~/") { | |
| 15 | + | if home, err := os.UserHomeDir(); err == nil { | |
| 16 | + | return filepath.Join(home, path[2:]) | |
| 17 | + | } | |
| 18 | + | } | |
| 19 | + | return path | |
| 20 | + | } | |
| 21 | + | ||
| 10 | 22 | // Normalize adds https:// prefix if missing for URL-like inputs. | |
| 11 | 23 | // Returns the input unchanged if it already has a scheme or doesn't look like a URL. | |
| 24 | + | // If the input is an existing local file, returns a file:// URL. | |
| 12 | 25 | func Normalize(input string) string { | |
| 13 | 26 | if input == "" { | |
| 14 | 27 | return "" | |
| @@ -28,6 +41,15 @@ func Normalize(input string) string { | |||
| 28 | 41 | return input | |
| 29 | 42 | } | |
| 30 | 43 | ||
| 44 | + | // Check if input is an existing file path | |
| 45 | + | expanded := expandHome(input) | |
| 46 | + | absPath, err := filepath.Abs(expanded) | |
| 47 | + | if err == nil { | |
| 48 | + | if _, statErr := os.Stat(absPath); statErr == nil { | |
| 49 | + | return "file://" + absPath | |
| 50 | + | } | |
| 51 | + | } | |
| 52 | + | ||
| 31 | 53 | // Looks like a URL (contains . and no spaces) | |
| 32 | 54 | if strings.Contains(input, ".") && !strings.Contains(input, " ") { | |
| 33 | 55 | return "https://" + input | |
| @@ -4,11 +4,24 @@ package url | |||||
| 4 | import ( | 4 | import ( | ||
| 5 | "fmt" | 5 | "fmt" | ||
| 6 | "net/url" | 6 | "net/url" | ||
| 7 | + | "os" | |||
| 8 | + | "path/filepath" | |||
| 7 | "strings" | 9 | "strings" | ||
| 8 | ) | 10 | ) | ||
| 9 | 11 | ||||
| 12 | + | // expandHome expands ~ prefix to user's home directory. | |||
| 13 | + | func expandHome(path string) string { | |||
| 14 | + | if strings.HasPrefix(path, "~/") { | |||
| 15 | + | if home, err := os.UserHomeDir(); err == nil { | |||
| 16 | + | return filepath.Join(home, path[2:]) | |||
| 17 | + | } | |||
| 18 | + | } | |||
| 19 | + | return path | |||
| 20 | + | } | |||
| 21 | + | ||||
| 10 | // Normalize adds https:// prefix if missing for URL-like inputs. | 22 | // Normalize adds https:// prefix if missing for URL-like inputs. | ||
| 11 | // Returns the input unchanged if it already has a scheme or doesn't look like a URL. | 23 | // Returns the input unchanged if it already has a scheme or doesn't look like a URL. | ||
| 24 | + | // If the input is an existing local file, returns a file:// URL. | |||
| 12 | func Normalize(input string) string { | 25 | func Normalize(input string) string { | ||
| 13 | if input == "" { | 26 | if input == "" { | ||
| 14 | return "" | 27 | return "" | ||
| @@ -28,6 +41,15 @@ func Normalize(input string) string { | |||||
| 28 | return input | 41 | return input | ||
| 29 | } | 42 | } | ||
| 30 | 43 | ||||
| 44 | + | // Check if input is an existing file path | |||
| 45 | + | expanded := expandHome(input) | |||
| 46 | + | absPath, err := filepath.Abs(expanded) | |||
| 47 | + | if err == nil { | |||
| 48 | + | if _, statErr := os.Stat(absPath); statErr == nil { | |||
| 49 | + | return "file://" + absPath | |||
| 50 | + | } | |||
| 51 | + | } | |||
| 52 | + | ||||
| 31 | // Looks like a URL (contains . and no spaces) | 53 | // Looks like a URL (contains . and no spaces) | ||
| 32 | if strings.Contains(input, ".") && !strings.Contains(input, " ") { | 54 | if strings.Contains(input, ".") && !strings.Contains(input, " ") { | ||
| 33 | return "https://" + input | 55 | return "https://" + input | ||
A internal/domain/url/normalize_test.go +260 -0
| @@ -0,0 +1,260 @@ | |||
| 1 | + | package url | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "os" | |
| 5 | + | "path/filepath" | |
| 6 | + | "testing" | |
| 7 | + | ) | |
| 8 | + | ||
| 9 | + | func TestNormalize(t *testing.T) { | |
| 10 | + | tests := []struct { | |
| 11 | + | name string | |
| 12 | + | input string | |
| 13 | + | want string | |
| 14 | + | }{ | |
| 15 | + | { | |
| 16 | + | name: "empty string", | |
| 17 | + | input: "", | |
| 18 | + | want: "", | |
| 19 | + | }, | |
| 20 | + | { | |
| 21 | + | name: "http scheme unchanged", | |
| 22 | + | input: "http://example.com", | |
| 23 | + | want: "http://example.com", | |
| 24 | + | }, | |
| 25 | + | { | |
| 26 | + | name: "https scheme unchanged", | |
| 27 | + | input: "https://example.com", | |
| 28 | + | want: "https://example.com", | |
| 29 | + | }, | |
| 30 | + | { | |
| 31 | + | name: "file scheme unchanged", | |
| 32 | + | input: "file:///path/to/file.html", | |
| 33 | + | want: "file:///path/to/file.html", | |
| 34 | + | }, | |
| 35 | + | { | |
| 36 | + | name: "dumb scheme unchanged", | |
| 37 | + | input: "dumb://home", | |
| 38 | + | want: "dumb://home", | |
| 39 | + | }, | |
| 40 | + | { | |
| 41 | + | name: "about scheme unchanged", | |
| 42 | + | input: "about:blank", | |
| 43 | + | want: "about:blank", | |
| 44 | + | }, | |
| 45 | + | { | |
| 46 | + | name: "domain gets https", | |
| 47 | + | input: "example.com", | |
| 48 | + | want: "https://example.com", | |
| 49 | + | }, | |
| 50 | + | { | |
| 51 | + | name: "domain with path gets https", | |
| 52 | + | input: "example.com/path", | |
| 53 | + | want: "https://example.com/path", | |
| 54 | + | }, | |
| 55 | + | { | |
| 56 | + | name: "search query unchanged", | |
| 57 | + | input: "hello world", | |
| 58 | + | want: "hello world", | |
| 59 | + | }, | |
| 60 | + | { | |
| 61 | + | name: "single word unchanged", | |
| 62 | + | input: "hello", | |
| 63 | + | want: "hello", | |
| 64 | + | }, | |
| 65 | + | } | |
| 66 | + | ||
| 67 | + | for _, tt := range tests { | |
| 68 | + | t.Run(tt.name, func(t *testing.T) { | |
| 69 | + | got := Normalize(tt.input) | |
| 70 | + | if got != tt.want { | |
| 71 | + | t.Errorf("Normalize(%q) = %q, want %q", tt.input, got, tt.want) | |
| 72 | + | } | |
| 73 | + | }) | |
| 74 | + | } | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | func TestNormalize_LocalFiles(t *testing.T) { | |
| 78 | + | // Create a temporary file for testing | |
| 79 | + | tmpDir := t.TempDir() | |
| 80 | + | tmpFile := filepath.Join(tmpDir, "test.html") | |
| 81 | + | if err := os.WriteFile(tmpFile, []byte("<html></html>"), 0644); err != nil { | |
| 82 | + | t.Fatalf("failed to create temp file: %v", err) | |
| 83 | + | } | |
| 84 | + | ||
| 85 | + | // Create a subdirectory with a file | |
| 86 | + | subDir := filepath.Join(tmpDir, "subdir") | |
| 87 | + | if err := os.Mkdir(subDir, 0755); err != nil { | |
| 88 | + | t.Fatalf("failed to create subdir: %v", err) | |
| 89 | + | } | |
| 90 | + | subFile := filepath.Join(subDir, "page.html") | |
| 91 | + | if err := os.WriteFile(subFile, []byte("<html></html>"), 0644); err != nil { | |
| 92 | + | t.Fatalf("failed to create subdir file: %v", err) | |
| 93 | + | } | |
| 94 | + | ||
| 95 | + | tests := []struct { | |
| 96 | + | name string | |
| 97 | + | input string | |
| 98 | + | want string | |
| 99 | + | }{ | |
| 100 | + | { | |
| 101 | + | name: "absolute path to existing file", | |
| 102 | + | input: tmpFile, | |
| 103 | + | want: "file://" + tmpFile, | |
| 104 | + | }, | |
| 105 | + | { | |
| 106 | + | name: "absolute path to existing directory", | |
| 107 | + | input: tmpDir, | |
| 108 | + | want: "file://" + tmpDir, | |
| 109 | + | }, | |
| 110 | + | { | |
| 111 | + | name: "non-existent file treated as URL", | |
| 112 | + | input: "/nonexistent/file.html", | |
| 113 | + | want: "https:///nonexistent/file.html", | |
| 114 | + | }, | |
| 115 | + | } | |
| 116 | + | ||
| 117 | + | for _, tt := range tests { | |
| 118 | + | t.Run(tt.name, func(t *testing.T) { | |
| 119 | + | got := Normalize(tt.input) | |
| 120 | + | if got != tt.want { | |
| 121 | + | t.Errorf("Normalize(%q) = %q, want %q", tt.input, got, tt.want) | |
| 122 | + | } | |
| 123 | + | }) | |
| 124 | + | } | |
| 125 | + | } | |
| 126 | + | ||
| 127 | + | func TestNormalize_RelativePaths(t *testing.T) { | |
| 128 | + | // Create a temp file in current directory | |
| 129 | + | tmpFile, err := os.CreateTemp(".", "test-*.html") | |
| 130 | + | if err != nil { | |
| 131 | + | t.Fatalf("failed to create temp file: %v", err) | |
| 132 | + | } | |
| 133 | + | tmpFile.Close() | |
| 134 | + | defer os.Remove(tmpFile.Name()) | |
| 135 | + | ||
| 136 | + | // Get absolute path for comparison | |
| 137 | + | absPath, err := filepath.Abs(tmpFile.Name()) | |
| 138 | + | if err != nil { | |
| 139 | + | t.Fatalf("failed to get abs path: %v", err) | |
| 140 | + | } | |
| 141 | + | ||
| 142 | + | t.Run("relative path to existing file", func(t *testing.T) { | |
| 143 | + | got := Normalize(tmpFile.Name()) | |
| 144 | + | want := "file://" + absPath | |
| 145 | + | if got != want { | |
| 146 | + | t.Errorf("Normalize(%q) = %q, want %q", tmpFile.Name(), got, want) | |
| 147 | + | } | |
| 148 | + | }) | |
| 149 | + | } | |
| 150 | + | ||
| 151 | + | func TestExpandHome(t *testing.T) { | |
| 152 | + | home, err := os.UserHomeDir() | |
| 153 | + | if err != nil { | |
| 154 | + | t.Skip("cannot get home directory") | |
| 155 | + | } | |
| 156 | + | ||
| 157 | + | tests := []struct { | |
| 158 | + | name string | |
| 159 | + | input string | |
| 160 | + | want string | |
| 161 | + | }{ | |
| 162 | + | { | |
| 163 | + | name: "tilde expansion", | |
| 164 | + | input: "~/Documents", | |
| 165 | + | want: filepath.Join(home, "Documents"), | |
| 166 | + | }, | |
| 167 | + | { | |
| 168 | + | name: "no tilde unchanged", | |
| 169 | + | input: "/absolute/path", | |
| 170 | + | want: "/absolute/path", | |
| 171 | + | }, | |
| 172 | + | { | |
| 173 | + | name: "relative path unchanged", | |
| 174 | + | input: "./relative/path", | |
| 175 | + | want: "./relative/path", | |
| 176 | + | }, | |
| 177 | + | { | |
| 178 | + | name: "tilde in middle unchanged", | |
| 179 | + | input: "/path/~/file", | |
| 180 | + | want: "/path/~/file", | |
| 181 | + | }, | |
| 182 | + | } | |
| 183 | + | ||
| 184 | + | for _, tt := range tests { | |
| 185 | + | t.Run(tt.name, func(t *testing.T) { | |
| 186 | + | got := expandHome(tt.input) | |
| 187 | + | if got != tt.want { | |
| 188 | + | t.Errorf("expandHome(%q) = %q, want %q", tt.input, got, tt.want) | |
| 189 | + | } | |
| 190 | + | }) | |
| 191 | + | } | |
| 192 | + | } | |
| 193 | + | ||
| 194 | + | func TestLooksLikeURL(t *testing.T) { | |
| 195 | + | tests := []struct { | |
| 196 | + | name string | |
| 197 | + | input string | |
| 198 | + | want bool | |
| 199 | + | }{ | |
| 200 | + | { | |
| 201 | + | name: "empty string", | |
| 202 | + | input: "", | |
| 203 | + | want: false, | |
| 204 | + | }, | |
| 205 | + | { | |
| 206 | + | name: "http URL", | |
| 207 | + | input: "http://example.com", | |
| 208 | + | want: true, | |
| 209 | + | }, | |
| 210 | + | { | |
| 211 | + | name: "https URL", | |
| 212 | + | input: "https://example.com", | |
| 213 | + | want: true, | |
| 214 | + | }, | |
| 215 | + | { | |
| 216 | + | name: "file URL", | |
| 217 | + | input: "file:///path/to/file", | |
| 218 | + | want: true, | |
| 219 | + | }, | |
| 220 | + | { | |
| 221 | + | name: "dumb URL", | |
| 222 | + | input: "dumb://home", | |
| 223 | + | want: true, | |
| 224 | + | }, | |
| 225 | + | { | |
| 226 | + | name: "about URL", | |
| 227 | + | input: "about:blank", | |
| 228 | + | want: true, | |
| 229 | + | }, | |
| 230 | + | { | |
| 231 | + | name: "domain", | |
| 232 | + | input: "example.com", | |
| 233 | + | want: true, | |
| 234 | + | }, | |
| 235 | + | { | |
| 236 | + | name: "domain with path", | |
| 237 | + | input: "example.com/path", | |
| 238 | + | want: true, | |
| 239 | + | }, | |
| 240 | + | { | |
| 241 | + | name: "search query", | |
| 242 | + | input: "hello world", | |
| 243 | + | want: false, | |
| 244 | + | }, | |
| 245 | + | { | |
| 246 | + | name: "single word", | |
| 247 | + | input: "hello", | |
| 248 | + | want: false, | |
| 249 | + | }, | |
| 250 | + | } | |
| 251 | + | ||
| 252 | + | for _, tt := range tests { | |
| 253 | + | t.Run(tt.name, func(t *testing.T) { | |
| 254 | + | got := LooksLikeURL(tt.input) | |
| 255 | + | if got != tt.want { | |
| 256 | + | t.Errorf("LooksLikeURL(%q) = %v, want %v", tt.input, got, tt.want) | |
| 257 | + | } | |
| 258 | + | }) | |
| 259 | + | } | |
| 260 | + | } | |
| @@ -0,0 +1,260 @@ | |||||
| 1 | + | package url | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "os" | |||
| 5 | + | "path/filepath" | |||
| 6 | + | "testing" | |||
| 7 | + | ) | |||
| 8 | + | ||||
| 9 | + | func TestNormalize(t *testing.T) { | |||
| 10 | + | tests := []struct { | |||
| 11 | + | name string | |||
| 12 | + | input string | |||
| 13 | + | want string | |||
| 14 | + | }{ | |||
| 15 | + | { | |||
| 16 | + | name: "empty string", | |||
| 17 | + | input: "", | |||
| 18 | + | want: "", | |||
| 19 | + | }, | |||
| 20 | + | { | |||
| 21 | + | name: "http scheme unchanged", | |||
| 22 | + | input: "http://example.com", | |||
| 23 | + | want: "http://example.com", | |||
| 24 | + | }, | |||
| 25 | + | { | |||
| 26 | + | name: "https scheme unchanged", | |||
| 27 | + | input: "https://example.com", | |||
| 28 | + | want: "https://example.com", | |||
| 29 | + | }, | |||
| 30 | + | { | |||
| 31 | + | name: "file scheme unchanged", | |||
| 32 | + | input: "file:///path/to/file.html", | |||
| 33 | + | want: "file:///path/to/file.html", | |||
| 34 | + | }, | |||
| 35 | + | { | |||
| 36 | + | name: "dumb scheme unchanged", | |||
| 37 | + | input: "dumb://home", | |||
| 38 | + | want: "dumb://home", | |||
| 39 | + | }, | |||
| 40 | + | { | |||
| 41 | + | name: "about scheme unchanged", | |||
| 42 | + | input: "about:blank", | |||
| 43 | + | want: "about:blank", | |||
| 44 | + | }, | |||
| 45 | + | { | |||
| 46 | + | name: "domain gets https", | |||
| 47 | + | input: "example.com", | |||
| 48 | + | want: "https://example.com", | |||
| 49 | + | }, | |||
| 50 | + | { | |||
| 51 | + | name: "domain with path gets https", | |||
| 52 | + | input: "example.com/path", | |||
| 53 | + | want: "https://example.com/path", | |||
| 54 | + | }, | |||
| 55 | + | { | |||
| 56 | + | name: "search query unchanged", | |||
| 57 | + | input: "hello world", | |||
| 58 | + | want: "hello world", | |||
| 59 | + | }, | |||
| 60 | + | { | |||
| 61 | + | name: "single word unchanged", | |||
| 62 | + | input: "hello", | |||
| 63 | + | want: "hello", | |||
| 64 | + | }, | |||
| 65 | + | } | |||
| 66 | + | ||||
| 67 | + | for _, tt := range tests { | |||
| 68 | + | t.Run(tt.name, func(t *testing.T) { | |||
| 69 | + | got := Normalize(tt.input) | |||
| 70 | + | if got != tt.want { | |||
| 71 | + | t.Errorf("Normalize(%q) = %q, want %q", tt.input, got, tt.want) | |||
| 72 | + | } | |||
| 73 | + | }) | |||
| 74 | + | } | |||
| 75 | + | } | |||
| 76 | + | ||||
| 77 | + | func TestNormalize_LocalFiles(t *testing.T) { | |||
| 78 | + | // Create a temporary file for testing | |||
| 79 | + | tmpDir := t.TempDir() | |||
| 80 | + | tmpFile := filepath.Join(tmpDir, "test.html") | |||
| 81 | + | if err := os.WriteFile(tmpFile, []byte("<html></html>"), 0644); err != nil { | |||
| 82 | + | t.Fatalf("failed to create temp file: %v", err) | |||
| 83 | + | } | |||
| 84 | + | ||||
| 85 | + | // Create a subdirectory with a file | |||
| 86 | + | subDir := filepath.Join(tmpDir, "subdir") | |||
| 87 | + | if err := os.Mkdir(subDir, 0755); err != nil { | |||
| 88 | + | t.Fatalf("failed to create subdir: %v", err) | |||
| 89 | + | } | |||
| 90 | + | subFile := filepath.Join(subDir, "page.html") | |||
| 91 | + | if err := os.WriteFile(subFile, []byte("<html></html>"), 0644); err != nil { | |||
| 92 | + | t.Fatalf("failed to create subdir file: %v", err) | |||
| 93 | + | } | |||
| 94 | + | ||||
| 95 | + | tests := []struct { | |||
| 96 | + | name string | |||
| 97 | + | input string | |||
| 98 | + | want string | |||
| 99 | + | }{ | |||
| 100 | + | { | |||
| 101 | + | name: "absolute path to existing file", | |||
| 102 | + | input: tmpFile, | |||
| 103 | + | want: "file://" + tmpFile, | |||
| 104 | + | }, | |||
| 105 | + | { | |||
| 106 | + | name: "absolute path to existing directory", | |||
| 107 | + | input: tmpDir, | |||
| 108 | + | want: "file://" + tmpDir, | |||
| 109 | + | }, | |||
| 110 | + | { | |||
| 111 | + | name: "non-existent file treated as URL", | |||
| 112 | + | input: "/nonexistent/file.html", | |||
| 113 | + | want: "https:///nonexistent/file.html", | |||
| 114 | + | }, | |||
| 115 | + | } | |||
| 116 | + | ||||
| 117 | + | for _, tt := range tests { | |||
| 118 | + | t.Run(tt.name, func(t *testing.T) { | |||
| 119 | + | got := Normalize(tt.input) | |||
| 120 | + | if got != tt.want { | |||
| 121 | + | t.Errorf("Normalize(%q) = %q, want %q", tt.input, got, tt.want) | |||
| 122 | + | } | |||
| 123 | + | }) | |||
| 124 | + | } | |||
| 125 | + | } | |||
| 126 | + | ||||
| 127 | + | func TestNormalize_RelativePaths(t *testing.T) { | |||
| 128 | + | // Create a temp file in current directory | |||
| 129 | + | tmpFile, err := os.CreateTemp(".", "test-*.html") | |||
| 130 | + | if err != nil { | |||
| 131 | + | t.Fatalf("failed to create temp file: %v", err) | |||
| 132 | + | } | |||
| 133 | + | tmpFile.Close() | |||
| 134 | + | defer os.Remove(tmpFile.Name()) | |||
| 135 | + | ||||
| 136 | + | // Get absolute path for comparison | |||
| 137 | + | absPath, err := filepath.Abs(tmpFile.Name()) | |||
| 138 | + | if err != nil { | |||
| 139 | + | t.Fatalf("failed to get abs path: %v", err) | |||
| 140 | + | } | |||
| 141 | + | ||||
| 142 | + | t.Run("relative path to existing file", func(t *testing.T) { | |||
| 143 | + | got := Normalize(tmpFile.Name()) | |||
| 144 | + | want := "file://" + absPath | |||
| 145 | + | if got != want { | |||
| 146 | + | t.Errorf("Normalize(%q) = %q, want %q", tmpFile.Name(), got, want) | |||
| 147 | + | } | |||
| 148 | + | }) | |||
| 149 | + | } | |||
| 150 | + | ||||
| 151 | + | func TestExpandHome(t *testing.T) { | |||
| 152 | + | home, err := os.UserHomeDir() | |||
| 153 | + | if err != nil { | |||
| 154 | + | t.Skip("cannot get home directory") | |||
| 155 | + | } | |||
| 156 | + | ||||
| 157 | + | tests := []struct { | |||
| 158 | + | name string | |||
| 159 | + | input string | |||
| 160 | + | want string | |||
| 161 | + | }{ | |||
| 162 | + | { | |||
| 163 | + | name: "tilde expansion", | |||
| 164 | + | input: "~/Documents", | |||
| 165 | + | want: filepath.Join(home, "Documents"), | |||
| 166 | + | }, | |||
| 167 | + | { | |||
| 168 | + | name: "no tilde unchanged", | |||
| 169 | + | input: "/absolute/path", | |||
| 170 | + | want: "/absolute/path", | |||
| 171 | + | }, | |||
| 172 | + | { | |||
| 173 | + | name: "relative path unchanged", | |||
| 174 | + | input: "./relative/path", | |||
| 175 | + | want: "./relative/path", | |||
| 176 | + | }, | |||
| 177 | + | { | |||
| 178 | + | name: "tilde in middle unchanged", | |||
| 179 | + | input: "/path/~/file", | |||
| 180 | + | want: "/path/~/file", | |||
| 181 | + | }, | |||
| 182 | + | } | |||
| 183 | + | ||||
| 184 | + | for _, tt := range tests { | |||
| 185 | + | t.Run(tt.name, func(t *testing.T) { | |||
| 186 | + | got := expandHome(tt.input) | |||
| 187 | + | if got != tt.want { | |||
| 188 | + | t.Errorf("expandHome(%q) = %q, want %q", tt.input, got, tt.want) | |||
| 189 | + | } | |||
| 190 | + | }) | |||
| 191 | + | } | |||
| 192 | + | } | |||
| 193 | + | ||||
| 194 | + | func TestLooksLikeURL(t *testing.T) { | |||
| 195 | + | tests := []struct { | |||
| 196 | + | name string | |||
| 197 | + | input string | |||
| 198 | + | want bool | |||
| 199 | + | }{ | |||
| 200 | + | { | |||
| 201 | + | name: "empty string", | |||
| 202 | + | input: "", | |||
| 203 | + | want: false, | |||
| 204 | + | }, | |||
| 205 | + | { | |||
| 206 | + | name: "http URL", | |||
| 207 | + | input: "http://example.com", | |||
| 208 | + | want: true, | |||
| 209 | + | }, | |||
| 210 | + | { | |||
| 211 | + | name: "https URL", | |||
| 212 | + | input: "https://example.com", | |||
| 213 | + | want: true, | |||
| 214 | + | }, | |||
| 215 | + | { | |||
| 216 | + | name: "file URL", | |||
| 217 | + | input: "file:///path/to/file", | |||
| 218 | + | want: true, | |||
| 219 | + | }, | |||
| 220 | + | { | |||
| 221 | + | name: "dumb URL", | |||
| 222 | + | input: "dumb://home", | |||
| 223 | + | want: true, | |||
| 224 | + | }, | |||
| 225 | + | { | |||
| 226 | + | name: "about URL", | |||
| 227 | + | input: "about:blank", | |||
| 228 | + | want: true, | |||
| 229 | + | }, | |||
| 230 | + | { | |||
| 231 | + | name: "domain", | |||
| 232 | + | input: "example.com", | |||
| 233 | + | want: true, | |||
| 234 | + | }, | |||
| 235 | + | { | |||
| 236 | + | name: "domain with path", | |||
| 237 | + | input: "example.com/path", | |||
| 238 | + | want: true, | |||
| 239 | + | }, | |||
| 240 | + | { | |||
| 241 | + | name: "search query", | |||
| 242 | + | input: "hello world", | |||
| 243 | + | want: false, | |||
| 244 | + | }, | |||
| 245 | + | { | |||
| 246 | + | name: "single word", | |||
| 247 | + | input: "hello", | |||
| 248 | + | want: false, | |||
| 249 | + | }, | |||
| 250 | + | } | |||
| 251 | + | ||||
| 252 | + | for _, tt := range tests { | |||
| 253 | + | t.Run(tt.name, func(t *testing.T) { | |||
| 254 | + | got := LooksLikeURL(tt.input) | |||
| 255 | + | if got != tt.want { | |||
| 256 | + | t.Errorf("LooksLikeURL(%q) = %v, want %v", tt.input, got, tt.want) | |||
| 257 | + | } | |||
| 258 | + | }) | |||
| 259 | + | } | |||
| 260 | + | } | |||