-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
125 lines (119 loc) · 3.06 KB
/
api_test.go
File metadata and controls
125 lines (119 loc) · 3.06 KB
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
package main
import (
"testing"
)
func TestExtractIDFromURL(t *testing.T) {
tests := []struct {
name string
url string
expected string
}{
{
name: "standard watch url - returns 'watch' due to query param removal",
url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
expected: "watch",
},
{
name: "watch url with query params - returns 'watch'",
url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=10s",
expected: "watch",
},
{
name: "short url",
url: "https://youtu.be/dQw4w9WgXcQ",
expected: "dQw4w9WgXcQ",
},
{
name: "short url with query params",
url: "https://youtu.be/dQw4w9WgXcQ?t=5",
expected: "dQw4w9WgXcQ",
},
{
name: "shorts url",
url: "https://www.youtube.com/shorts/abc123xyz",
expected: "abc123xyz",
},
{
name: "shorts url with query params",
url: "https://www.youtube.com/shorts/abc123xyz?feature=share",
expected: "abc123xyz",
},
{
name: "embed url",
url: "https://www.youtube.com/embed/dQw4w9WgXcQ",
expected: "dQw4w9WgXcQ",
},
{
name: "playlist url - returns 'playlist' due to query param removal",
url: "https://www.youtube.com/playlist?list=PLabcdef123456",
expected: "playlist",
},
{
name: "channel url with @",
url: "https://www.youtube.com/@channelname",
expected: "@channelname",
},
{
name: "channel url with /c/",
url: "https://www.youtube.com/c/channelname",
expected: "channelname",
},
{
name: "user url",
url: "https://www.youtube.com/user/username",
expected: "username",
},
{
name: "empty url",
url: "",
expected: "",
},
{
name: "url with only domain",
url: "https://www.youtube.com",
expected: "www.youtube.com",
},
{
name: "url with trailing slash",
url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ/",
expected: "",
},
{
name: "url with fragment - fragment not split by slash",
url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ#t=10",
expected: "watch",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := extractIDFromURL(tt.url)
if result != tt.expected {
t.Errorf("extractIDFromURL(%q) = %q, want %q", tt.url, result, tt.expected)
}
})
}
}
func TestExtractIDFromURLEdgeCases(t *testing.T) {
t.Run("non-youtube url", func(t *testing.T) {
url := "https://example.com/video/12345"
result := extractIDFromURL(url)
if result != "12345" {
t.Errorf("Expected '12345', got %q", result)
}
})
t.Run("url with multiple slashes", func(t *testing.T) {
url := "https://www.youtube.com//watch//v=dQw4w9WgXcQ"
result := extractIDFromURL(url)
// Function takes last non-empty part
if result == "" {
t.Error("Expected non-empty result for malformed URL")
}
})
t.Run("url with special characters in id", func(t *testing.T) {
url := "https://www.youtube.com/watch?v=abc-_123"
result := extractIDFromURL(url)
if result != "watch" {
t.Errorf("Expected 'watch', got %q", result)
}
})
}