Added spec for YOUTUBE_ID_REGEX, we can easily add more youtube url sample here later to avoid polluting application_helper spec

This commit is contained in:
Arzumy MD 2011-05-06 00:47:40 +08:00
parent 6ba2b00fb8
commit fad9635022
2 changed files with 32 additions and 2 deletions

View file

@ -20,5 +20,5 @@ module YoutubeTitles
end
end
YOUTUBE_ID_REGEX = /(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})(?:\S*(#[^ ]+))?/im unless defined? YOUTUBE_ID_REGEX
YOUTUBE_ID_REGEX = /(?:https?:\/\/)(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#!|.+&|.+&)v=)([\w-]{11})(?:\S*(#[^ ]+)|\S+)?/im unless defined? YOUTUBE_ID_REGEX
end

View file

@ -1,29 +1,35 @@
require 'spec_helper'
require 'youtube_titles'
describe YoutubeTitles do
include YoutubeTitles
before do
@video_id = "ABYnqp-bxvg"
@url="http://www.youtube.com/watch?v=#{@video_id}&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
@api_path = "/feeds/api/videos/#{@video_id}?v=2"
end
include YoutubeTitles
describe '#youtube_title_for' do
before do
@expected_title = "UP & down & UP & down &"
@mock_http = mock("http")
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(@mock_http)
end
it 'gets a youtube title corresponding to an id' do
@mock_http.should_receive(:get).with(@api_path, nil).and_return(
[nil, "Foobar <title>#{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"])
youtube_title_for(@video_id).should == @expected_title
end
it 'returns a fallback for videos with no title' do
@mock_http.should_receive(:get).with(@api_path, nil).and_return(
[nil, "Foobar #{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"])
youtube_title_for(@video_id).should == I18n.t('application.helper.video_title.unknown')
end
end
describe 'serialization and marshalling' do
before do
@expected_title = '""Procrastination"" Tales Of Mere Existence'
@ -33,10 +39,34 @@ describe YoutubeTitles do
[nil, "Foobar <title>#{@expected_title}</title> hallo welt <asd><dasdd><a>dsd</a>"])
@post = Factory.create(:status_message, :text => @url)
end
it 'can be re-marshalled' do
lambda {
StatusMessage.find(@post.id).youtube_titles
}.should_not raise_error
end
end
describe "YOUTUBE_ID_REGEX" do
specify "normal url" do
url = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX)
matched_data[0].should == url
matched_data[1].should == "dQw4w9WgXcQ"
end
specify "https url" do
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX)
matched_data[0].should == url
matched_data[1].should == "dQw4w9WgXcQ"
end
specify "url with extra query params" do
url = "http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=related"
matched_data = url.match(YoutubeTitles::YOUTUBE_ID_REGEX)
matched_data[0].should == url
matched_data[1].should == "dQw4w9WgXcQ"
end
end
end