]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - test/youtube_test.rb
ec91d357112a2c1b6e24e6cbd41bb15653d4a16f
[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 end
39
40
41 def test_doesnt_own_redtube_urls
42 assert(!Youtube.owns_url?('http://www.redtube.com/6807'))
43 assert(!Youtube.owns_url?('www.redtube.com/6807'))
44 assert(!Youtube.owns_url?('http://redtube.com/6807'))
45 assert(!Youtube.owns_url?('redtube.com/6807'))
46 end
47
48
49 def test_parse_video_id
50 yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4')
51 expected_result = 'SudixyugiX4'
52 actual_result = yt.send('parse_video_id')
53 assert_equal(expected_result, actual_result)
54 end
55
56
57 def test_parse_video_id_again
58 yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE')
59 expected_result = 'K9iDMcmm0tE'
60 actual_result = yt.send('parse_video_id')
61 assert_equal(expected_result, actual_result)
62 end
63
64
65 def test_parse_t_parameter
66 yt = Youtube.new(nil)
67
68 page_data = nil
69
70 File.open('test/fixtures/youtube/SudixyugiX4.html') do |f|
71 page_data = f.read
72 end
73
74 expected_result = 'OEgsToPDskLQUAntWWpzhEMhBMlgqHdo'
75 actual_result = yt.send('parse_t_parameter', page_data)
76 assert_equal(expected_result, actual_result)
77 end
78
79
80 def test_parse_t_parameter_again
81 # This was failing once because my regex missed a hyphen.
82 # I modified the regex to match anything between the pair
83 # of quotes, so it should now catch the hyphen and underscore.
84 yt = Youtube.new(nil)
85
86 page_data = nil
87
88 File.open('test/fixtures/youtube/K9iDMcmm0tE.html') do |f|
89 page_data = f.read
90 end
91
92 expected_result = 'O_EgsToPDskJsXVvAXpAct1zug-lBJBz'
93 actual_result = yt.send('parse_t_parameter', page_data)
94 assert_equal(expected_result, actual_result)
95 end
96
97
98 def test_get_available_formats
99 # Make sure that we can parse the available formats from a page.
100 yt = Youtube.new(nil)
101
102 page_data = nil
103
104 File.open('test/fixtures/youtube/BUer8Dv2HW8.html') do |f|
105 page_data = f.read
106 end
107
108 actual_formats = yt.send('get_available_formats', page_data)
109 expected_formats = [ 37, 22, 35, 18, 34, 5 ]
110
111 # Make sure that all of the elements of the "expected" set are in
112 # the "actual" set and vice-versa.
113 assert_equal([], expected_formats - actual_formats)
114 assert_equal([], actual_formats - expected_formats)
115 end
116
117
118 def test_get_desired_format
119 # The get_desired_format() method should choose the best of the
120 # available formats.
121 yt = Youtube.new(nil)
122
123 page_data = nil
124
125 File.open('test/fixtures/youtube/BUer8Dv2HW8.html') do |f|
126 page_data = f.read
127 end
128
129 available_formats = yt.send('get_available_formats', page_data)
130 actual_result = yt.send('get_desired_format', available_formats)
131 expected_result = 37
132
133 assert_equal(expected_result, actual_result)
134 end
135
136
137 def test_get_video_filename
138 yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4')
139 expected_result = 'SudixyugiX4.flv'
140 actual_result = yt.get_video_filename()
141 assert_equal(expected_result, actual_result)
142 end
143
144
145 def test_get_video_filename_again
146 yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE')
147 expected_result = 'K9iDMcmm0tE.flv'
148 actual_result = yt.get_video_filename()
149 assert_equal(expected_result, actual_result)
150 end
151
152
153 def test_get_troublesome_video_filename_first_form
154 # This non-alphanumeric video id was causing
155 # get_video_filename to barf.
156 yt = Youtube.new('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir')
157 expected_result = '83-hlYMH1XE.flv'
158 actual_result = yt.get_video_filename()
159 assert_equal(expected_result, actual_result)
160 end
161
162
163 def test_get_troublesome_video_filename_second_form
164 yt = Youtube.new('http://www.youtube.com/v/83-hlYMH1XE&feature=dir')
165 expected_result = '83-hlYMH1XE.flv'
166 actual_result = yt.get_video_filename()
167 assert_equal(expected_result, actual_result)
168 end
169
170 end