forked from huydx/hget
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil_test.go
148 lines (127 loc) · 3.1 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"net"
"path/filepath"
"testing"
)
func TestFilterIpV4(t *testing.T) {
// Create a mix of IPv4 and IPv6 addresses
ips := []net.IP{
net.ParseIP("192.168.1.1"),
net.ParseIP("fe80::1"),
net.ParseIP("127.0.0.1"),
net.ParseIP("::1"),
net.ParseIP("10.0.0.1"),
}
// Filter IPv4 addresses
filtered := FilterIPV4(ips)
// Check if the filtered list contains exactly the 3 IPv4 addresses
if len(filtered) != 3 {
t.Fatalf("Expected 3 IPv4 addresses, got %d", len(filtered))
}
// Check if all filtered addresses are IPv4
expectedAddrs := map[string]bool{
"192.168.1.1": true,
"127.0.0.1": true,
"10.0.0.1": true,
}
for _, ip := range filtered {
if !expectedAddrs[ip] {
t.Fatalf("Unexpected IPv4 address: %s", ip)
}
}
}
func TestFolderOfPanic1(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
url := "http://foo.bar/.."
FolderOf(url)
}
func TestFolderOfPanic2(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
url := "http://foo.bar/../../../foobar"
FolderOf(url)
}
func TestFolderOfNormal(t *testing.T) {
url := "http://foo.bar/file"
u := FolderOf(url)
if filepath.Base(u) != "file" {
t.Fatalf("url of return incorrect value")
}
}
func TestFolderWithoutParams(t *testing.T) {
url := "http://foo.bar/file?param=value"
u := FolderOf(url)
if filepath.Base(u) != "file" {
t.Fatalf("url of return incorrect value")
}
}
func TestTaskFromURL(t *testing.T) {
testCases := []struct {
url string
expected string
}{
{"http://example.com/path/to/file.zip", "file.zip"},
{"https://download.com/file.tar.gz?token=123", "file.tar.gz"},
{"http://domain.com/path/", "path"},
{"https://test.org/path/to/file.txt#fragment", "file.txt"},
}
for _, tc := range testCases {
result := TaskFromURL(tc.url)
if result != tc.expected {
t.Errorf("TaskFromURL(%s) = %s; want %s", tc.url, result, tc.expected)
}
}
}
func TestIsURL(t *testing.T) {
validURLs := []string{
"http://example.com",
"https://test.org/path",
"ftp://files.org/file.zip",
"http://localhost:8080",
}
invalidURLs := []string{
"not a url",
"http:/missing-slash",
"://no-scheme",
}
for _, url := range validURLs {
if !IsURL(url) {
t.Errorf("IsURL(%s) = false; want true", url)
}
}
for _, url := range invalidURLs {
if IsURL(url) {
t.Errorf("IsURL(%s) = true; want false", url)
}
}
}
func TestMkdirIfNotExist(t *testing.T) {
// Test creating a temporary directory
tempDir := filepath.Join(t.TempDir(), "test-dir")
// Directory shouldn't exist yet
if ExistDir(tempDir) {
t.Fatalf("Directory %s should not exist yet", tempDir)
}
// Create the directory
err := MkdirIfNotExist(tempDir)
if err != nil {
t.Fatalf("MkdirIfNotExist failed: %v", err)
}
// Directory should now exist
if !ExistDir(tempDir) {
t.Fatalf("Directory %s should exist after MkdirIfNotExist", tempDir)
}
// Running again on existing directory should not error
err = MkdirIfNotExist(tempDir)
if err != nil {
t.Fatalf("MkdirIfNotExist on existing dir failed: %v", err)
}
}