-
Notifications
You must be signed in to change notification settings - Fork 435
/
client_test.go
276 lines (229 loc) · 7.61 KB
/
client_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package youtube
import (
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)
const (
dwlURL string = "https://www.youtube.com/watch?v=rFejpH_tAHM"
streamURL string = "https://www.youtube.com/watch?v=a9LDPn-MO4I"
errURL string = "https://www.youtube.com/watch?v=I8oGsuQ"
)
var testClient = Client{}
var testWebClient = Client{client: &WebClient}
func TestParseVideo(t *testing.T) {
video, err := testClient.GetVideo(dwlURL)
assert.NoError(t, err)
assert.NotNil(t, video)
_, err = testClient.GetVideo(errURL)
assert.IsType(t, err, &ErrPlayabiltyStatus{})
}
func TestYoutube_findVideoID(t *testing.T) {
type args struct {
url string
}
tests := []struct {
name string
args args
wantErr bool
expectedErr error
}{
{
name: "valid url",
args: args{
dwlURL,
},
wantErr: false,
expectedErr: nil,
},
{
name: "valid id",
args: args{
"rFejpH_tAHM",
},
wantErr: false,
expectedErr: nil,
},
{
name: "invalid character in id",
args: args{
"<M13",
},
wantErr: true,
expectedErr: ErrInvalidCharactersInVideoID,
},
{
name: "video id is less than 10 characters",
args: args{
"rFejpH",
},
wantErr: true,
expectedErr: ErrVideoIDMinLength,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := ExtractVideoID(tt.args.url); (err != nil) != tt.wantErr || err != tt.expectedErr {
t.Errorf("extractVideoID() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetVideoWithoutManifestURL(t *testing.T) {
assert, require := assert.New(t), require.New(t)
video, err := testClient.GetVideo(dwlURL)
require.NoError(err, "get video")
require.NotNil(video)
assert.NotEmpty(video.Thumbnails)
assert.Greater(len(video.Thumbnails), 0)
assert.NotEmpty(video.Thumbnails[0].URL)
assert.Empty(video.HLSManifestURL)
assert.Empty(video.DASHManifestURL)
assert.NotEmpty(video.CaptionTracks)
assert.Greater(len(video.CaptionTracks), 0)
assert.NotEmpty(video.CaptionTracks[0].BaseURL)
assert.Equal("rFejpH_tAHM", video.ID)
assert.Equal("dotGo 2015 - Rob Pike - Simplicity is Complicated", video.Title)
assert.Equal("dotconferences", video.Author)
assert.GreaterOrEqual(video.Duration, 1390*time.Second)
assert.Contains(video.Description, "Go is often described as a simple language.")
// Publishing date doesn't seem to be present in android client
// assert.Equal("2015-12-02 00:00:00 +0000 UTC", video.PublishDate.String())
}
func TestWebClientGetVideoWithoutManifestURL(t *testing.T) {
assert, require := assert.New(t), require.New(t)
video, err := testWebClient.GetVideo(dwlURL)
require.NoError(err, "get video")
require.NotNil(video)
assert.NotEmpty(video.Thumbnails)
assert.Greater(len(video.Thumbnails), 0)
assert.NotEmpty(video.Thumbnails[0].URL)
assert.Empty(video.HLSManifestURL)
assert.Empty(video.DASHManifestURL)
assert.NotEmpty(video.CaptionTracks)
assert.Greater(len(video.CaptionTracks), 0)
assert.NotEmpty(video.CaptionTracks[0].BaseURL)
assert.Equal("rFejpH_tAHM", video.ID)
assert.Equal("dotGo 2015 - Rob Pike - Simplicity is Complicated", video.Title)
assert.Equal("dotconferences", video.Author)
assert.GreaterOrEqual(video.Duration, 1390*time.Second)
assert.Contains(video.Description, "Go is often described as a simple language.")
// Publishing date and channel handle are present in web client
//assert.Equal("2015-12-02 00:00:00 +0000 UTC", video.PublishDate.String())
assert.Equal("@dotconferences", video.ChannelHandle)
}
func TestGetVideoWithManifestURL(t *testing.T) {
assert, require := assert.New(t), require.New(t)
video, err := testClient.GetVideo(streamURL)
require.NoError(err)
require.NotNil(video)
assert.NotEmpty(video.Formats)
assert.NotEmpty(video.Thumbnails)
assert.Greater(len(video.Thumbnails), 0)
assert.NotEmpty(video.Thumbnails[0].URL)
format := video.Formats[0]
r, size, err := testClient.GetStream(video, &format)
if assert.NoError(err) {
r.Close()
}
assert.NotZero(size)
}
func TestGetVideo_MultiLanguage(t *testing.T) {
assert, require := assert.New(t), require.New(t)
video, err := testClient.GetVideo("https://www.youtube.com/watch?v=pU9sHwNKc2c")
require.NoError(err)
require.NotNil(video)
// collect languages
var languageNames, lanaguageIDs []string
for _, format := range video.Formats {
if format.AudioTrack != nil {
languageNames = append(languageNames, format.LanguageDisplayName())
lanaguageIDs = append(lanaguageIDs, format.AudioTrack.ID)
}
}
assert.Contains(languageNames, "English original")
assert.Contains(languageNames, "Portuguese (Brazil)")
assert.Contains(lanaguageIDs, "en.4")
assert.Contains(lanaguageIDs, "pt-BR.3")
assert.Empty(video.Formats.Language("Does not exist"))
assert.NotEmpty(video.Formats.Language("English original"))
}
func TestGetStream(t *testing.T) {
assert, require := assert.New(t), require.New(t)
expectedSize := 988479
// Create testclient to enforce re-using of routines
testClient := Client{
MaxRoutines: 10,
ChunkSize: int64(expectedSize) / 11,
}
// Download should not last longer than a minute.
// Otherwise we assume Youtube is throtteling us.
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
video, err := testClient.GetVideoContext(ctx, "https://www.youtube.com/watch?v=BaW_jenozKc")
require.NoError(err)
require.NotNil(video)
require.Greater(len(video.Formats), 0)
reader, size, err := testClient.GetStreamContext(ctx, video, &video.Formats[0])
require.NoError(err)
assert.EqualValues(expectedSize, size)
data, err := io.ReadAll(reader)
require.NoError(err)
assert.Len(data, int(size))
}
func TestGetPlaylist(t *testing.T) {
assert, require := assert.New(t), require.New(t)
playlist, err := testClient.GetPlaylist("https://www.youtube.com/playlist?list=PL59FEE129ADFF2B12")
require.NoError(err)
require.NotNil(playlist)
assert.Equal(playlist.Title, "Test Playlist")
assert.Equal(playlist.Description, "")
assert.Equal(playlist.Author, "GoogleVoice")
assert.Equal(len(playlist.Videos), 8)
v := playlist.Videos[7]
assert.Equal(v.ID, "dsUXAEzaC3Q")
assert.Equal(v.Title, "Michael Jackson - Bad (Shortened Version)")
assert.Equal(v.Author, "Michael Jackson")
assert.Equal(v.Duration, 4*time.Minute+20*time.Second)
assert.NotEmpty(v.Thumbnails)
assert.NotEmpty(v.Thumbnails[0].URL)
}
func TestGetBigPlaylist(t *testing.T) {
assert, require := assert.New(t), require.New(t)
playlist, err := testClient.GetPlaylist("https://www.youtube.com/playlist?list=PLTC7VQ12-9raqhLCx1S1E_ic35t94dj28")
require.NoError(err)
require.NotNil(playlist)
assert.NotEmpty(playlist.Title)
assert.NotEmpty(playlist.Description)
assert.NotEmpty(playlist.Author)
assert.Greater(len(playlist.Videos), 300)
assert.NotEmpty(playlist.Videos[300].ID)
t.Logf("Playlist Title: %s, Video Count: %d", playlist.Title, len(playlist.Videos))
}
func TestClient_httpGetBodyBytes(t *testing.T) {
tests := []struct {
url string
errorContains string
}{
{"unknown://", "unsupported protocol scheme"},
{"invalid\nurl", "invalid control character in URL"},
{"http://unknown-host/", "dial tcp"},
{"https://www.google.com/404", "unexpected status code: 404"},
{"http://example.com/", ""},
}
for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := testClient.httpGetBodyBytes(ctx, tt.url)
if tt.errorContains == "" {
assert.NoError(t, err)
} else if assert.Error(t, err) {
assert.Contains(t, err.Error(), tt.errorContains)
}
})
}
}