spec/feedzirra/feed_spec.rb in feedzirra-0.2.1 vs spec/feedzirra/feed_spec.rb in feedzirra-0.2.2
- old
+ new
@@ -22,24 +22,24 @@
describe "#add_common_feed_entry_element" do
before(:all) do
Feedzirra::Feed.add_common_feed_entry_element("wfw:commentRss", :as => :comment_rss)
end
-
+
it "should parse the added element out of Atom feeds entries" do
Feedzirra::Feed.parse(sample_wfw_feed).entries.first.comment_rss.should == "this is the new val"
end
-
+
it "should parse the added element out of Atom Feedburner feeds entries" do
Feedzirra::Parser::AtomEntry.new.should respond_to(:comment_rss)
end
-
+
it "should parse the added element out of RSS feeds entries" do
Feedzirra::Parser::RSSEntry.new.should respond_to(:comment_rss)
end
end
-
+
describe "#parse" do # many of these tests are redundant with the specific feed type tests, but I put them here for completeness
context "when there's an available parser" do
it "should parse an rdf feed" do
feed = Feedzirra::Feed.parse(sample_rdf_feed)
feed.title.should == "HREF Considered Harmful"
@@ -64,11 +64,11 @@
it "should parse an feedburner atom feed" do
feed = Feedzirra::Feed.parse(sample_feedburner_atom_feed)
feed.title.should == "Paul Dix Explains Nothing"
feed.entries.first.published.should == Time.parse_safely("Thu Jan 22 15:50:22 UTC 2009")
feed.entries.size.should == 5
- end
+ end
it "should parse an itunes feed" do
feed = Feedzirra::Feed.parse(sample_itunes_feed)
feed.title.should == "All About Everything"
feed.entries.first.published.should == Time.parse_safely("Wed, 15 Jun 2005 19:00:00 GMT")
@@ -124,17 +124,17 @@
end
describe "when adding feed types" do
it "should prioritize added types over the built in ones" do
feed_text = "Atom asdf"
- Feedzirra::Parser::Atom.stub!(:able_to_parse?).and_return(true)
+ Feedzirra::Parser::Atom.stub(:able_to_parse?).and_return(true)
new_feed_type = Class.new do
def self.able_to_parse?(val)
true
end
end
-
+
new_feed_type.should be_able_to_parse(feed_text)
Feedzirra::Feed.add_feed_class(new_feed_type)
Feedzirra::Feed.determine_feed_parser_for_xml(feed_text).should == new_feed_type
# this is a hack so that this doesn't break the rest of the tests
@@ -177,18 +177,18 @@
@trotter_feed = { :xml => load_sample("TrotterCashionHome.xml"), :url => "http://feeds2.feedburner.com/trottercashion" }
end
describe "#fetch_raw" do
before(:each) do
- @cmock = stub('cmock', :header_str => '', :body_str => @paul_feed[:xml] )
- @multi = stub('curl_multi', :add => true, :perform => true)
- @curl_easy = stub('curl_easy')
- @curl = stub('curl', :headers => {}, :follow_location= => true, :on_failure => true)
- @curl.stub!(:on_success).and_yield(@cmock)
-
- Curl::Multi.stub!(:new).and_return(@multi)
- Curl::Easy.stub!(:new).and_yield(@curl).and_return(@curl_easy)
+ @cmock = double('cmock', :header_str => '', :body_str => @paul_feed[:xml] )
+ @multi = double('curl_multi', :add => true, :perform => true)
+ @curl_easy = double('curl_easy')
+ @curl = double('curl', :headers => {}, :follow_location= => true, :on_failure => true)
+ @curl.stub(:on_success).and_yield(@cmock)
+
+ Curl::Multi.stub(:new).and_return(@multi)
+ Curl::Easy.stub(:new).and_yield(@curl).and_return(@curl_easy)
end
it "should set user agent if it's passed as an option" do
Feedzirra::Feed.fetch_raw(@paul_feed[:url], :user_agent => 'Custom Useragent')
@curl.headers['User-Agent'].should == 'Custom Useragent'
@@ -196,21 +196,21 @@
it "should set user agent to default if it's not passed as an option" do
Feedzirra::Feed.fetch_raw(@paul_feed[:url])
@curl.headers['User-Agent'].should == Feedzirra::Feed::USER_AGENT
end
-
+
it "should set if modified since as an option if passed" do
Feedzirra::Feed.fetch_raw(@paul_feed[:url], :if_modified_since => Time.parse_safely("Wed, 28 Jan 2009 04:10:32 GMT"))
@curl.headers["If-Modified-Since"].should == 'Wed, 28 Jan 2009 04:10:32 GMT'
end
it "should set if none match as an option if passed" do
Feedzirra::Feed.fetch_raw(@paul_feed[:url], :if_none_match => 'ziEyTl4q9GH04BR4jgkImd0GvSE')
@curl.headers["If-None-Match"].should == 'ziEyTl4q9GH04BR4jgkImd0GvSE'
end
-
+
it 'should set userpwd for http basic authentication if :http_authentication is passed' do
@curl.should_receive(:userpwd=).with('username:password')
Feedzirra::Feed.fetch_raw(@paul_feed[:url], :http_authentication => ['username', 'password'])
end
@@ -222,25 +222,25 @@
it "should return raw xml" do
Feedzirra::Feed.fetch_raw(@paul_feed[:url]).should =~ /^#{Regexp.escape('<?xml version="1.0" encoding="UTF-8"?>')}/
end
it "should take multiple feed urls and return a hash of urls and response xml" do
- multi = stub('curl_multi', :add => true, :perform => true)
- Curl::Multi.stub!(:new).and_return(multi)
-
- paul_response = stub('paul_response', :header_str => '', :body_str => @paul_feed[:xml] )
- trotter_response = stub('trotter_response', :header_str => '', :body_str => @trotter_feed[:xml] )
+ multi = double('curl_multi', :add => true, :perform => true)
+ Curl::Multi.stub(:new).and_return(multi)
- paul_curl = stub('paul_curl', :headers => {}, :follow_location= => true, :on_failure => true)
- paul_curl.stub!(:on_success).and_yield(paul_response)
+ paul_response = double('paul_response', :header_str => '', :body_str => @paul_feed[:xml] )
+ trotter_response = double('trotter_response', :header_str => '', :body_str => @trotter_feed[:xml] )
- trotter_curl = stub('trotter_curl', :headers => {}, :follow_location= => true, :on_failure => true)
- trotter_curl.stub!(:on_success).and_yield(trotter_response)
-
+ paul_curl = double('paul_curl', :headers => {}, :follow_location= => true, :on_failure => true)
+ paul_curl.stub(:on_success).and_yield(paul_response)
+
+ trotter_curl = double('trotter_curl', :headers => {}, :follow_location= => true, :on_failure => true)
+ trotter_curl.stub(:on_success).and_yield(trotter_response)
+
Curl::Easy.should_receive(:new).with(@paul_feed[:url]).ordered.and_yield(paul_curl)
Curl::Easy.should_receive(:new).with(@trotter_feed[:url]).ordered.and_yield(trotter_curl)
-
+
results = Feedzirra::Feed.fetch_raw([@paul_feed[:url], @trotter_feed[:url]])
results.keys.should include(@paul_feed[:url])
results.keys.should include(@trotter_feed[:url])
results[@paul_feed[:url]].should =~ /Paul Dix/
results[@trotter_feed[:url]].should =~ /Trotter Cashion/
@@ -254,67 +254,67 @@
describe "#add_url_to_multi" do
before(:each) do
allow_message_expectations_on_nil
@multi = Curl::Multi.get([@paul_feed[:url]], {:follow_location => true}, {:pipeline => true})
- @multi.stub!(:add)
+ @multi.stub(:add)
@easy_curl = Curl::Easy.new(@paul_feed[:url])
-
+
Curl::Easy.should_receive(:new).and_yield(@easy_curl)
end
it "should set user agent if it's passed as an option" do
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, :user_agent => 'My cool application')
@easy_curl.headers["User-Agent"].should == 'My cool application'
end
-
+
it "should set user agent to default if it's not passed as an option" do
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
@easy_curl.headers["User-Agent"].should == Feedzirra::Feed::USER_AGENT
end
-
+
it "should set if modified since as an option if passed" do
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, :if_modified_since => Time.parse_safely("Jan 25 2009 04:10:32 GMT"))
@easy_curl.headers["If-Modified-Since"].should == 'Sun, 25 Jan 2009 04:10:32 GMT'
end
it 'should set follow location to true' do
@easy_curl.should_receive(:follow_location=).with(true)
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
end
-
+
it 'should set userpwd for http basic authentication if :http_authentication is passed' do
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, :http_authentication => ['myusername', 'mypassword'])
@easy_curl.userpwd.should == 'myusername:mypassword'
end
-
+
it 'should set accepted encodings' do
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {:compress => true})
@easy_curl.headers["Accept-encoding"].should == 'gzip, deflate'
end
it "should set if_none_match as an option if passed" do
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, :if_none_match => 'ziEyTl4q9GH04BR4jgkImd0GvSE')
@easy_curl.headers["If-None-Match"].should == 'ziEyTl4q9GH04BR4jgkImd0GvSE'
end
-
+
describe 'on success' do
before(:each) do
- @feed = mock('feed', :feed_url= => true, :etag= => true, :last_modified= => true)
- Feedzirra::Feed.stub!(:decode_content).and_return(@paul_feed[:xml])
- Feedzirra::Feed.stub!(:determine_feed_parser_for_xml).and_return(Feedzirra::Parser::AtomFeedBurner)
- Feedzirra::Parser::AtomFeedBurner.stub!(:parse).and_return(@feed)
- Feedzirra::Feed.stub!(:etag_from_header).and_return('ziEyTl4q9GH04BR4jgkImd0GvSE')
- Feedzirra::Feed.stub!(:last_modified_from_header).and_return('Wed, 28 Jan 2009 04:10:32 GMT')
+ @feed = double('feed', :feed_url= => true, :etag= => true, :last_modified= => true)
+ Feedzirra::Feed.stub(:decode_content).and_return(@paul_feed[:xml])
+ Feedzirra::Feed.stub(:determine_feed_parser_for_xml).and_return(Feedzirra::Parser::AtomFeedBurner)
+ Feedzirra::Parser::AtomFeedBurner.stub(:parse).and_return(@feed)
+ Feedzirra::Feed.stub(:etag_from_header).and_return('ziEyTl4q9GH04BR4jgkImd0GvSE')
+ Feedzirra::Feed.stub(:last_modified_from_header).and_return('Wed, 28 Jan 2009 04:10:32 GMT')
end
it 'should decode the response body' do
Feedzirra::Feed.should_receive(:decode_content).with(@easy_curl).and_return(@paul_feed[:xml])
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
@easy_curl.on_success.call(@easy_curl)
end
-
+
it 'should determine the xml parser class' do
Feedzirra::Feed.should_receive(:determine_feed_parser_for_xml).with(@paul_feed[:xml]).and_return(Feedzirra::Parser::AtomFeedBurner)
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
@easy_curl.on_success.call(@easy_curl)
end
@@ -322,11 +322,11 @@
it 'should parse the xml' do
Feedzirra::Parser::AtomFeedBurner.should_receive(:parse).with(@paul_feed[:xml], an_instance_of(Proc)).and_return(@feed)
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
@easy_curl.on_success.call(@easy_curl)
end
-
+
describe 'when a compatible xml parser class is found' do
it 'should set the last effective url to the feed url' do
@easy_curl.should_receive(:last_effective_url).and_return(@paul_feed[:url])
@feed.should_receive(:feed_url=).with(@paul_feed[:url])
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
@@ -347,15 +347,15 @@
it 'should add the feed to the responses' do
responses = {}
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], responses, {})
@easy_curl.on_success.call(@easy_curl)
-
+
responses.length.should == 1
responses['http://feeds.feedburner.com/PaulDixExplainsNothing'].should == @feed
end
-
+
it 'should call proc if :on_success option is passed' do
success = lambda { |url, feed| }
success.should_receive(:call).with(@paul_feed[:url], @feed)
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_success => success })
@easy_curl.on_success.call(@easy_curl)
@@ -370,22 +370,22 @@
describe 'on failure' do
before(:each) do
@headers = "HTTP/1.0 500 Something Bad\r\nDate: Thu, 29 Jan 2009 03:55:24 GMT\r\nServer: Apache\r\nX-FB-Host: chi-write6\r\nLast-Modified: Wed, 28 Jan 2009 04:10:32 GMT\r\n"
@body = 'Sorry, something broke'
- @easy_curl.stub!(:response_code).and_return(500)
- @easy_curl.stub!(:header_str).and_return(@headers)
- @easy_curl.stub!(:body_str).and_return(@body)
+ @easy_curl.stub(:response_code).and_return(500)
+ @easy_curl.stub(:header_str).and_return(@headers)
+ @easy_curl.stub(:body_str).and_return(@body)
end
it 'should call proc if :on_failure option is passed' do
failure = lambda { |url, feed| }
failure.should_receive(:call).with(@paul_feed[:url], 500, @headers, @body)
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_failure => failure })
@easy_curl.on_failure.call(@easy_curl)
end
-
+
it 'should return the http code in the responses' do
responses = {}
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], responses, {})
@easy_curl.on_failure.call(@easy_curl)
@@ -397,22 +397,22 @@
describe 'on complete for 404s' do
before(:each) do
@headers = "HTTP/1.0 404 Not Found\r\nDate: Thu, 29 Jan 2009 03:55:24 GMT\r\nServer: Apache\r\nX-FB-Host: chi-write6\r\nLast-Modified: Wed, 28 Jan 2009 04:10:32 GMT\r\n"
@body = 'Page could not be found.'
- @easy_curl.stub!(:response_code).and_return(404)
- @easy_curl.stub!(:header_str).and_return(@headers)
- @easy_curl.stub!(:body_str).and_return(@body)
+ @easy_curl.stub(:response_code).and_return(404)
+ @easy_curl.stub(:header_str).and_return(@headers)
+ @easy_curl.stub(:body_str).and_return(@body)
end
it 'should call proc if :on_failure option is passed' do
complete = lambda { |url| }
complete.should_receive(:call).with(@paul_feed[:url], 404, @headers, @body)
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_failure => complete })
@easy_curl.on_complete.call(@easy_curl)
end
-
+
it 'should return the http code in the responses' do
responses = {}
Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], responses, {})
@easy_curl.on_complete.call(@easy_curl)
@@ -424,35 +424,35 @@
describe "#add_feed_to_multi" do
before(:each) do
allow_message_expectations_on_nil
@multi = Curl::Multi.get([@paul_feed[:url]], {:follow_location => true}, {:pipeline => true})
- @multi.stub!(:add)
+ @multi.stub(:add)
@easy_curl = Curl::Easy.new(@paul_feed[:url])
@feed = Feedzirra::Feed.parse(sample_feedburner_atom_feed)
Curl::Easy.should_receive(:new).and_yield(@easy_curl)
end
it "should set user agent if it's passed as an option" do
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, :user_agent => 'My cool application')
@easy_curl.headers["User-Agent"].should == 'My cool application'
end
-
+
it "should set user agent to default if it's not passed as an option" do
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
@easy_curl.headers["User-Agent"].should == Feedzirra::Feed::USER_AGENT
end
it "should set if modified since as an option if passed" do
modified_time = Time.parse_safely("Wed, 28 Jan 2009 04:10:32 GMT")
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {:if_modified_since => modified_time})
modified_time.should be > @feed.last_modified
-
+
@easy_curl.headers["If-Modified-Since"].should == modified_time
end
-
+
it 'should set follow location to true' do
@easy_curl.should_receive(:follow_location=).with(true)
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
end
@@ -468,20 +468,20 @@
end
describe 'on success' do
before(:each) do
@new_feed = @feed.clone
- @feed.stub!(:update_from_feed)
- Feedzirra::Feed.stub!(:decode_content).and_return(@paul_feed[:xml])
- Feedzirra::Feed.stub!(:determine_feed_parser_for_xml).and_return(Feedzirra::Parser::AtomFeedBurner)
- Feedzirra::Parser::AtomFeedBurner.stub!(:parse).and_return(@new_feed)
- Feedzirra::Feed.stub!(:etag_from_header).and_return('ziEyTl4q9GH04BR4jgkImd0GvSE')
- Feedzirra::Feed.stub!(:last_modified_from_header).and_return('Wed, 28 Jan 2009 04:10:32 GMT')
+ @feed.stub(:update_from_feed)
+ Feedzirra::Feed.stub(:decode_content).and_return(@paul_feed[:xml])
+ Feedzirra::Feed.stub(:determine_feed_parser_for_xml).and_return(Feedzirra::Parser::AtomFeedBurner)
+ Feedzirra::Parser::AtomFeedBurner.stub(:parse).and_return(@new_feed)
+ Feedzirra::Feed.stub(:etag_from_header).and_return('ziEyTl4q9GH04BR4jgkImd0GvSE')
+ Feedzirra::Feed.stub(:last_modified_from_header).and_return('Wed, 28 Jan 2009 04:10:32 GMT')
end
it 'should process the next feed in the queue'
-
+
it 'should parse the updated feed' do
Feedzirra::Parser::AtomFeedBurner.should_receive(:parse).and_return(@new_feed)
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
@easy_curl.on_success.call(@easy_curl)
end
@@ -518,11 +518,11 @@
success = lambda { |feed| }
success.should_receive(:call).with(@feed)
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, { :on_success => success })
@easy_curl.on_success.call(@easy_curl)
end
-
+
it 'should call update from feed on the old feed with the updated feed' do
@feed.should_receive(:update_from_feed).with(@new_feed)
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
@easy_curl.on_success.call(@easy_curl)
end
@@ -531,23 +531,23 @@
describe 'on failure' do
before(:each) do
@headers = "HTTP/1.0 404 Not Found\r\nDate: Thu, 29 Jan 2009 03:55:24 GMT\r\nServer: Apache\r\nX-FB-Host: chi-write6\r\nLast-Modified: Wed, 28 Jan 2009 04:10:32 GMT\r\n"
@body = 'Page could not be found.'
- @easy_curl.stub!(:response_code).and_return(404)
- @easy_curl.stub!(:header_str).and_return(@headers)
- @easy_curl.stub!(:body_str).and_return(@body)
+ @easy_curl.stub(:response_code).and_return(404)
+ @easy_curl.stub(:header_str).and_return(@headers)
+ @easy_curl.stub(:body_str).and_return(@body)
end
it 'should call on success callback if the response code is 304' do
success = lambda { |feed| }
success.should_receive(:call).with(@feed)
@easy_curl.should_receive(:response_code).and_return(304)
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, { :on_success => success })
@easy_curl.on_failure.call(@easy_curl)
end
-
+
it 'should return the http code in the responses' do
responses = {}
Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], responses, {})
@easy_curl.on_failure.call(@easy_curl)
@@ -561,62 +561,62 @@
it 'should initiate the fetching and parsing using multicurl'
it "should pass any request options through to add_url_to_multi"
it 'should slice the feeds into groups of thirty for processing'
it "should return a feed object if a single feed is passed in"
it "should return an return an array of feed objects if multiple feeds are passed in"
-
+
it "should set if modified since as an option if passed" do
modified_time = Time.parse_safely("Wed, 28 Jan 2009 04:10:32 GMT")
- Feedzirra::Feed.should_receive(:add_url_to_multi).with(anything, anything, anything, anything, {:if_modified_since => modified_time}).any_number_of_times
-
+ Feedzirra::Feed.should_receive(:add_url_to_multi).with(anything, anything, anything, anything, {:if_modified_since => modified_time})
+
@feed = Feedzirra::Feed.fetch_and_parse(sample_feedburner_atom_feed, {:if_modified_since => modified_time})
end
-
+
end
describe "#decode_content" do
before(:each) do
- @curl_easy = mock('curl_easy', :body_str => '<xml></xml>')
+ @curl_easy = double('curl_easy', :body_str => '<xml></xml>')
end
it 'should decode the response body using gzip if the Content-Encoding: is gzip' do
- @curl_easy.stub!(:header_str).and_return('Content-Encoding: gzip')
- string_io = mock('stringio', :read => @curl_easy.body_str, :close => true)
+ @curl_easy.stub(:header_str).and_return('Content-Encoding: gzip')
+ string_io = double('stringio', :read => @curl_easy.body_str, :close => true)
StringIO.should_receive(:new).and_return(string_io)
Zlib::GzipReader.should_receive(:new).with(string_io).and_return(string_io)
Feedzirra::Feed.decode_content(@curl_easy)
end
-
+
it 'should decode the response body using gzip if the Content-Encoding: is gzip even when the case is wrong' do
- @curl_easy.stub!(:header_str).and_return('content-encoding: gzip')
- string_io = mock('stringio', :read => @curl_easy.body_str, :close => true)
+ @curl_easy.stub(:header_str).and_return('content-encoding: gzip')
+ string_io = double('stringio', :read => @curl_easy.body_str, :close => true)
StringIO.should_receive(:new).and_return(string_io)
Zlib::GzipReader.should_receive(:new).with(string_io).and_return(string_io)
Feedzirra::Feed.decode_content(@curl_easy)
end
it 'should deflate the response body using inflate if the Content-Encoding: is deflate' do
- @curl_easy.stub!(:header_str).and_return('Content-Encoding: deflate')
+ @curl_easy.stub(:header_str).and_return('Content-Encoding: deflate')
Zlib::Inflate.should_receive(:inflate).with(@curl_easy.body_str)
Feedzirra::Feed.decode_content(@curl_easy)
end
it 'should deflate the response body using inflate if the Content-Encoding: is deflate event if the case is wrong' do
- @curl_easy.stub!(:header_str).and_return('content-encoding: deflate')
+ @curl_easy.stub(:header_str).and_return('content-encoding: deflate')
Zlib::Inflate.should_receive(:inflate).with(@curl_easy.body_str)
Feedzirra::Feed.decode_content(@curl_easy)
end
it 'should return the response body if it is not encoded' do
- @curl_easy.stub!(:header_str).and_return('')
+ @curl_easy.stub(:header_str).and_return('')
Feedzirra::Feed.decode_content(@curl_easy).should == '<xml></xml>'
end
end
describe "#update" do
it 'should perform the updating using multicurl'
it "should pass any request options through to add_feed_to_multi"
it "should return a feed object if a single feed is passed in"
- it "should return an return an array of feed objects if multiple feeds are passed in"
+ it "should return an return an array of feed objects if multiple feeds are passed in"
end
end
end