# # Copyright Michael Orlitzky # # http://michael.orlitzky.com/ # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # http://www.fsf.org/licensing/licenses/gpl.html # require 'test/unit' require 'src/websites/youtube' class YoutubeTest < Test::Unit::TestCase def test_owns_youtube_urls assert(Youtube.owns_url?('http://www.youtube.com/watch?v=SudixyugiX4')) assert(Youtube.owns_url?('http://www.youtube.com/watch?v=SudixyugiX4&hl=en')) assert(Youtube.owns_url?('http://youtube.com/watch?v=SudixyugiX4&hl=en')) assert(Youtube.owns_url?('http://www.youtube.com/v/SudixyugiX4')) assert(Youtube.owns_url?('http://www.youtube.com/v/SudixyugiX4&hl=en')) assert(Youtube.owns_url?('http://youtube.com/v/SudixyugiX4&hl=en')) assert(Youtube.owns_url?('http://www.youtube.com/watch?v=K9iDMcmm0tE')) assert(Youtube.owns_url?('http://www.youtube.com/watch?v=K9iDMcmm0tE#')) assert(Youtube.owns_url?('http://www.youtube.com/v/K9iDMcmm0tE')) assert(Youtube.owns_url?('http://www.youtube.com/watch?v=83-hlYMH1XE')) assert(Youtube.owns_url?('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir')) assert(Youtube.owns_url?('http://in.youtube.com/watch?v=VcydqSpYN00&feature=channel_page')) assert(Youtube.owns_url?('http://uk.youtube.com/watch?v=LN4Ov6ZLcrI')) assert(Youtube.owns_url?('http://www.youtube.com/meetlocalbiz#p/u/0/rJVWV4aA6Jk')) end def test_doesnt_own_redtube_urls assert(!Youtube.owns_url?('http://www.redtube.com/6807')) assert(!Youtube.owns_url?('www.redtube.com/6807')) assert(!Youtube.owns_url?('http://redtube.com/6807')) assert(!Youtube.owns_url?('redtube.com/6807')) end def test_parse_video_id yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4') expected_result = 'SudixyugiX4' actual_result = yt.send('parse_video_id') assert_equal(expected_result, actual_result) end def test_parse_video_id_again yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE') expected_result = 'K9iDMcmm0tE' actual_result = yt.send('parse_video_id') assert_equal(expected_result, actual_result) end def test_parse_t_parameter yt = Youtube.new(nil) page_data = nil File.open('test/fixtures/youtube/SudixyugiX4.html') do |f| page_data = f.read end expected_result = 'OEgsToPDskLQUAntWWpzhEMhBMlgqHdo' actual_result = yt.send('parse_t_parameter', page_data) assert_equal(expected_result, actual_result) end def test_parse_t_parameter_again # This was failing once because my regex missed a hyphen. # I modified the regex to match anything between the pair # of quotes, so it should now catch the hyphen and underscore. yt = Youtube.new(nil) page_data = nil File.open('test/fixtures/youtube/K9iDMcmm0tE.html') do |f| page_data = f.read end expected_result = 'O_EgsToPDskJsXVvAXpAct1zug-lBJBz' actual_result = yt.send('parse_t_parameter', page_data) assert_equal(expected_result, actual_result) end def test_parse_t_parameter_from_url # Tests a different regex than the other 't' parameter tests. yt = Youtube.new(nil) page_data = nil File.open('test/fixtures/youtube/cpU27Xp8zdM.html') do |f| page_data = f.read end expected_result = 'vjVQa1PpcFNyKz8nP3yZ3MHcwOvLXsOWpCJHkgJVShI=' actual_result = yt.send('parse_t_parameter', page_data) assert_equal(expected_result, actual_result) end def test_get_available_formats # Make sure that we can parse the available formats from a page. yt = Youtube.new(nil) page_data = nil File.open('test/fixtures/youtube/BUer8Dv2HW8.html') do |f| page_data = f.read end actual_formats = yt.send('get_available_formats', page_data) expected_formats = [ 37, 22, 35, 18, 34, 5 ] # Make sure that all of the elements of the "expected" set are in # the "actual" set and vice-versa. assert_equal([], expected_formats - actual_formats) assert_equal([], actual_formats - expected_formats) end def test_get_available_formats_from_url # Tests a different regex than the other available formats test. yt = Youtube.new(nil) page_data = nil File.open('test/fixtures/youtube/cpU27Xp8zdM.html') do |f| page_data = f.read end actual_formats = yt.send('get_available_formats', page_data) expected_formats = [ 34, 5 ] # Make sure that all of the elements of the "expected" set are in # the "actual" set and vice-versa. assert_equal([], expected_formats - actual_formats) assert_equal([], actual_formats - expected_formats) end def test_get_desired_format # The get_desired_format() method should choose the best of the # available formats. yt = Youtube.new(nil) page_data = nil File.open('test/fixtures/youtube/BUer8Dv2HW8.html') do |f| page_data = f.read end available_formats = yt.send('get_available_formats', page_data) actual_result = yt.send('get_desired_format', available_formats) expected_result = 37 assert_equal(expected_result, actual_result) end def test_get_video_filename yt = Youtube.new('http://www.youtube.com/watch?v=SudixyugiX4') expected_result = 'SudixyugiX4.flv' actual_result = yt.get_video_filename() assert_equal(expected_result, actual_result) end def test_get_video_filename_again yt = Youtube.new('http://www.youtube.com/watch?v=K9iDMcmm0tE') expected_result = 'K9iDMcmm0tE.flv' actual_result = yt.get_video_filename() assert_equal(expected_result, actual_result) end def test_get_troublesome_video_filename_first_form # This non-alphanumeric video id was causing # get_video_filename to barf. yt = Youtube.new('http://www.youtube.com/watch?v=83-hlYMH1XE&feature=dir') expected_result = '83-hlYMH1XE.flv' actual_result = yt.get_video_filename() assert_equal(expected_result, actual_result) end def test_get_troublesome_video_filename_second_form yt = Youtube.new('http://www.youtube.com/v/83-hlYMH1XE&feature=dir') expected_result = '83-hlYMH1XE.flv' actual_result = yt.get_video_filename() assert_equal(expected_result, actual_result) end end