]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - test/youtube_test.rb
Added tests/fixtures for the most recent Youtube changes.
[dead/whatever-dl.git] / test / youtube_test.rb
1 #
2 # Copyright Michael Orlitzky
3 #
4 # http://michael.orlitzky.com/
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # http://www.fsf.org/licensing/licenses/gpl.html
17 #
18
19 require 'test/unit'
20 require 'src/websites/youtube'
21
22 class YoutubeTest < Test::Unit::TestCase
23
24 def test_owns_youtube_urls
25 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=SudixyugiX4'))
26 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=SudixyugiX4&hl=en'))
27 assert(Youtube.owns_url?('http://youtube.com/watch?v=SudixyugiX4&hl=en'))
28 assert(Youtube.owns_url?('http://www.youtube.com/v/SudixyugiX4'))
29 assert(Youtube.owns_url?('http://www.youtube.com/v/SudixyugiX4&hl=en'))
30 assert(Youtube.owns_url?('http://youtube.com/v/SudixyugiX4&hl=en'))
31 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=K9iDMcmm0tE'))
32 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=K9iDMcmm0tE#'))
33 assert(Youtube.owns_url?('http://www.youtube.com/v/K9iDMcmm0tE'))
34 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=83-hlYMH1XE'))
35 assert(Youtube.owns_url?('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir'))
36 assert(Youtube.owns_url?('http://in.youtube.com/watch?v=VcydqSpYN00&feature=channel_page'))
37 assert(Youtube.owns_url?('http://uk.youtube.com/watch?v=LN4Ov6ZLcrI'))
38 assert(Youtube.owns_url?('http://www.youtube.com/meetlocalbiz#p/u/0/rJVWV4aA6Jk'))
39 end
40
41
42 def test_doesnt_own_redtube_urls
43 assert(!Youtube.owns_url?('http://www.redtube.com/6807'))
44 assert(!Youtube.owns_url?('www.redtube.com/6807'))
45 assert(!Youtube.owns_url?('http://redtube.com/6807'))
46 assert(!Youtube.owns_url?('redtube.com/6807'))
47 end
48
49
50 def test_parse_video_id
51 yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4')
52 expected_result = 'SudixyugiX4'
53 actual_result = yt.send('parse_video_id')
54 assert_equal(expected_result, actual_result)
55 end
56
57
58 def test_parse_video_id_again
59 yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE')
60 expected_result = 'K9iDMcmm0tE'
61 actual_result = yt.send('parse_video_id')
62 assert_equal(expected_result, actual_result)
63 end
64
65
66 def test_parse_t_parameter
67 yt = Youtube.new(nil)
68
69 page_data = nil
70
71 File.open('test/fixtures/youtube/SudixyugiX4.html') do |f|
72 page_data = f.read
73 end
74
75 expected_result = 'OEgsToPDskLQUAntWWpzhEMhBMlgqHdo'
76 actual_result = yt.send('parse_t_parameter', page_data)
77 assert_equal(expected_result, actual_result)
78 end
79
80
81 def test_parse_t_parameter_again
82 # This was failing once because my regex missed a hyphen.
83 # I modified the regex to match anything between the pair
84 # of quotes, so it should now catch the hyphen and underscore.
85 yt = Youtube.new(nil)
86
87 page_data = nil
88
89 File.open('test/fixtures/youtube/K9iDMcmm0tE.html') do |f|
90 page_data = f.read
91 end
92
93 expected_result = 'O_EgsToPDskJsXVvAXpAct1zug-lBJBz'
94 actual_result = yt.send('parse_t_parameter', page_data)
95 assert_equal(expected_result, actual_result)
96 end
97
98
99 def test_parse_t_parameter_from_url
100 # Tests a different regex than the other 't' parameter tests.
101 yt = Youtube.new(nil)
102
103 page_data = nil
104
105 File.open('test/fixtures/youtube/cpU27Xp8zdM.html') do |f|
106 page_data = f.read
107 end
108
109 expected_result = 'vjVQa1PpcFNyKz8nP3yZ3MHcwOvLXsOWpCJHkgJVShI='
110 actual_result = yt.send('parse_t_parameter', page_data)
111 assert_equal(expected_result, actual_result)
112 end
113
114
115 def test_get_available_formats
116 # Make sure that we can parse the available formats from a page.
117 yt = Youtube.new(nil)
118
119 page_data = nil
120
121 File.open('test/fixtures/youtube/BUer8Dv2HW8.html') do |f|
122 page_data = f.read
123 end
124
125 actual_formats = yt.send('get_available_formats', page_data)
126 expected_formats = [ 37, 22, 35, 18, 34, 5 ]
127
128 # Make sure that all of the elements of the "expected" set are in
129 # the "actual" set and vice-versa.
130 assert_equal([], expected_formats - actual_formats)
131 assert_equal([], actual_formats - expected_formats)
132 end
133
134
135 def test_get_available_formats_from_url
136 # Tests a different regex than the other available formats test.
137 yt = Youtube.new(nil)
138
139 page_data = nil
140
141 File.open('test/fixtures/youtube/cpU27Xp8zdM.html') do |f|
142 page_data = f.read
143 end
144
145 actual_formats = yt.send('get_available_formats', page_data)
146 expected_formats = [ 34, 5 ]
147
148 # Make sure that all of the elements of the "expected" set are in
149 # the "actual" set and vice-versa.
150 assert_equal([], expected_formats - actual_formats)
151 assert_equal([], actual_formats - expected_formats)
152 end
153
154
155 def test_get_desired_format
156 # The get_desired_format() method should choose the best of the
157 # available formats.
158 yt = Youtube.new(nil)
159
160 page_data = nil
161
162 File.open('test/fixtures/youtube/BUer8Dv2HW8.html') do |f|
163 page_data = f.read
164 end
165
166 available_formats = yt.send('get_available_formats', page_data)
167 actual_result = yt.send('get_desired_format', available_formats)
168 expected_result = 37
169
170 assert_equal(expected_result, actual_result)
171 end
172
173
174 def test_get_video_filename
175 yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4')
176 expected_result = 'SudixyugiX4.flv'
177 actual_result = yt.get_video_filename()
178 assert_equal(expected_result, actual_result)
179 end
180
181
182 def test_get_video_filename_again
183 yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE')
184 expected_result = 'K9iDMcmm0tE.flv'
185 actual_result = yt.get_video_filename()
186 assert_equal(expected_result, actual_result)
187 end
188
189
190 def test_get_troublesome_video_filename_first_form
191 # This non-alphanumeric video id was causing
192 # get_video_filename to barf.
193 yt = Youtube.new('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir')
194 expected_result = '83-hlYMH1XE.flv'
195 actual_result = yt.get_video_filename()
196 assert_equal(expected_result, actual_result)
197 end
198
199
200 def test_get_troublesome_video_filename_second_form
201 yt = Youtube.new('http://www.youtube.com/v/83-hlYMH1XE&feature=dir')
202 expected_result = '83-hlYMH1XE.flv'
203 actual_result = yt.get_video_filename()
204 assert_equal(expected_result, actual_result)
205 end
206
207 end