-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_test.go
63 lines (58 loc) · 1.49 KB
/
youtube_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
package dyatl
import (
"net/url"
"strings"
"testing"
)
func TestYoutubeIdFromUrl(t *testing.T) {
for link, id := range map[string]string{
"https://www.youtube.com/watch?v=iJHV-96Z6ZY": "iJHV-96Z6ZY",
"https://youtu.be/GCYZMjFczmM?t=7": "GCYZMjFczmM",
"https://xxxxx.com": "",
} {
t.Run(link, func(t *testing.T) {
u, err := url.Parse(link)
if err != nil {
t.Fatal(err)
}
v := YoutubeIdFromUrl(u)
if v != id {
t.Error("invalid id, want:", id, "got:", v)
}
})
}
}
func TestGetYoutubeData(t *testing.T) {
c := NewClient()
t.Run("private video", func(t *testing.T) {
u, err := url.Parse("https://www.youtube.com/watch?v=Ud3p8p9hqHc")
if err != nil {
t.Fatal(err)
}
if _, err := c.GetYoutubeData(YoutubeIdFromUrl(u)); err != Unauthorized {
t.Fatal("must be Unauthorized, got:", err)
}
})
t.Run("unknown video", func(t *testing.T) {
u, err := url.Parse("https://www.youtube.com/watch?v=jfoiwufoisudofi")
if err != nil {
t.Fatal(err)
}
if _, err := c.GetYoutubeData(YoutubeIdFromUrl(u)); err != BadRequest {
t.Fatal("must be BadRequest, got:", err)
}
})
t.Run("valid video", func(t *testing.T) {
u, err := url.Parse("https://www.youtube.com/watch?v=zFLp-lJfA64")
if err != nil {
t.Fatal(err)
}
data, err := c.GetYoutubeData(YoutubeIdFromUrl(u))
if err != nil{
t.Fatal(err)
}
if !strings.HasPrefix(data.Title, "А.Лаэртский") {
t.Fatal("invalid title:", data.Title)
}
})
}