|
| 1 | +package directlinks |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseFilenameFromURL(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + url string |
| 11 | + expected string |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "simple filename", |
| 15 | + url: "https://example.com/files/document.pdf", |
| 16 | + expected: "document.pdf", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "filename with encoded characters", |
| 20 | + url: "https://example.com/files/%E6%B5%8B%E8%AF%95.zip", |
| 21 | + expected: "测试.zip", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "filename with query string in URL", |
| 25 | + url: "https://example.com/files/image.png?token=abc123", |
| 26 | + expected: "image.png", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "nested path", |
| 30 | + url: "https://example.com/a/b/c/file.txt", |
| 31 | + expected: "file.txt", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "URL with port", |
| 35 | + url: "https://example.com:8080/downloads/archive.tar.gz", |
| 36 | + expected: "archive.tar.gz", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "empty path", |
| 40 | + url: "https://example.com", |
| 41 | + expected: "", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "root path only", |
| 45 | + url: "https://example.com/", |
| 46 | + expected: "", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "filename with spaces encoded", |
| 50 | + url: "https://example.com/my%20file%20name.pdf", |
| 51 | + expected: "my file name.pdf", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "complex encoded filename", |
| 55 | + url: "https://example.com/downloads/%E4%B8%AD%E6%96%87%E6%96%87%E4%BB%B6.docx", |
| 56 | + expected: "中文文件.docx", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "invalid URL", |
| 60 | + url: "://invalid-url", |
| 61 | + expected: "", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + result := parseFilenameFromURL(tt.url) |
| 68 | + if result != tt.expected { |
| 69 | + t.Errorf("parseFilenameFromURL(%q) = %q, want %q", tt.url, result, tt.expected) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments