agiley-feedzirra 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,556 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Feedzirra::Feed do
4
+ describe "#add_common_feed_entry_element" do
5
+ before(:all) do
6
+ Feedzirra::Feed.add_common_feed_entry_element("wfw:commentRss", :as => :comment_rss)
7
+ end
8
+
9
+ it "should parse the added element out of Atom feeds" do
10
+ Feedzirra::Feed.parse(sample_wfw_feed).entries.first.comment_rss.should == "this is the new val"
11
+ end
12
+
13
+ it "should parse the added element out of Atom Feedburner feeds" do
14
+ Feedzirra::Parser::AtomEntry.new.should respond_to(:comment_rss)
15
+ end
16
+
17
+ it "should parse the added element out of RSS feeds" do
18
+ Feedzirra::Parser::RSSEntry.new.should respond_to(:comment_rss)
19
+ end
20
+ end
21
+
22
+ describe "#parse" do # many of these tests are redundant with the specific feed type tests, but I put them here for completeness
23
+ context "when there's an available parser" do
24
+ it "should parse an rdf feed" do
25
+ feed = Feedzirra::Feed.parse(sample_rdf_feed)
26
+ feed.title.should == "HREF Considered Harmful"
27
+ feed.entries.first.published.to_s.should == "Tue Sep 02 19:50:07 UTC 2008"
28
+ feed.entries.size.should == 10
29
+ end
30
+
31
+ it "should parse an rss feed" do
32
+ feed = Feedzirra::Feed.parse(sample_rss_feed)
33
+ feed.title.should == "Tender Lovemaking"
34
+ feed.entries.first.published.to_s.should == "Thu Dec 04 17:17:49 UTC 2008"
35
+ feed.entries.size.should == 10
36
+ end
37
+
38
+ it "should parse an atom feed" do
39
+ feed = Feedzirra::Feed.parse(sample_atom_feed)
40
+ feed.title.should == "Amazon Web Services Blog"
41
+ feed.entries.first.published.to_s.should == "Fri Jan 16 18:21:00 UTC 2009"
42
+ feed.entries.size.should == 10
43
+ end
44
+
45
+ it "should parse an feedburner atom feed" do
46
+ feed = Feedzirra::Feed.parse(sample_feedburner_atom_feed)
47
+ feed.title.should == "Paul Dix Explains Nothing"
48
+ feed.entries.first.published.to_s.should == "Thu Jan 22 15:50:22 UTC 2009"
49
+ feed.entries.size.should == 5
50
+ end
51
+
52
+ it "should parse an itunes feed as a standard RSS feed" do
53
+ feed = Feedzirra::Feed.parse(sample_itunes_feed)
54
+ feed.title.should == "All About Everything"
55
+ feed.entries.first.published.should == Time.parse("Wed, 15 Jun 2005 19:00:00 GMT")
56
+
57
+ # Since the commit 621957879, iTunes feeds will be parsed as standard RSS, so this
58
+ # entry should now not have a method for itunes_author.
59
+ feed.entries.first.should_not respond_to(:itunes_author)
60
+ feed.entries.size.should == 3
61
+ end
62
+ end
63
+
64
+ context "when there's no available parser" do
65
+ it "raises Feedzirra::NoParserAvailable" do
66
+ proc {
67
+ Feedzirra::Feed.parse("I'm an invalid feed")
68
+ }.should raise_error(Feedzirra::NoParserAvailable)
69
+ end
70
+ end
71
+
72
+ it "should parse an feedburner rss feed" do
73
+ feed = Feedzirra::Feed.parse(sample_rss_feed_burner_feed)
74
+ feed.title.should == "Sam Harris: Author, Philosopher, Essayist, Atheist"
75
+ feed.entries.first.published.to_s.should == "Tue Jan 13 17:20:28 UTC 2009"
76
+ feed.entries.size.should == 10
77
+ end
78
+ end
79
+
80
+ describe "#determine_feed_parser_for_xml" do
81
+ it "should return the Feedzirra::Parser::Atom class for an atom feed" do
82
+ Feedzirra::Feed.determine_feed_parser_for_xml(sample_atom_feed).should == Feedzirra::Parser::Atom
83
+ end
84
+
85
+ it "should return the Feedzirra::Parser::AtomFeedBurner class for an atom feedburner feed" do
86
+ Feedzirra::Feed.determine_feed_parser_for_xml(sample_feedburner_atom_feed).should == Feedzirra::Parser::AtomFeedBurner
87
+ end
88
+
89
+ it "should return the Feedzirra::Parser::RSS class for an rdf/rss 1.0 feed" do
90
+ Feedzirra::Feed.determine_feed_parser_for_xml(sample_rdf_feed).should == Feedzirra::Parser::RSS
91
+ end
92
+
93
+ it "should return the Feedzirra::Parser::RSS class for an rss feedburner feed" do
94
+ Feedzirra::Feed.determine_feed_parser_for_xml(sample_rss_feed_burner_feed).should == Feedzirra::Parser::RSS
95
+ end
96
+
97
+ it "should return the Feedzirra::Parser::RSS object for an rss 2.0 feed" do
98
+ Feedzirra::Feed.determine_feed_parser_for_xml(sample_rss_feed).should == Feedzirra::Parser::RSS
99
+ end
100
+
101
+ it "should return a Feedzirra::Parser::RSS object for an itunes feed" do
102
+ Feedzirra::Feed.determine_feed_parser_for_xml(sample_itunes_feed).should == Feedzirra::Parser::RSS
103
+ end
104
+
105
+ end
106
+
107
+ describe "when adding feed types" do
108
+ it "should prioritize added types over the built in ones" do
109
+ feed_text = "Atom asdf"
110
+ Feedzirra::Parser::Atom.should be_able_to_parse(feed_text)
111
+ new_feed_type = Class.new do
112
+ def self.able_to_parse?(val)
113
+ true
114
+ end
115
+ end
116
+
117
+ new_feed_type.should be_able_to_parse(feed_text)
118
+ Feedzirra::Feed.add_feed_class(new_feed_type)
119
+ Feedzirra::Feed.determine_feed_parser_for_xml(feed_text).should == new_feed_type
120
+
121
+ # this is a hack so that this doesn't break the rest of the tests
122
+ Feedzirra::Feed.feed_classes.reject! {|o| o == new_feed_type }
123
+ end
124
+ end
125
+
126
+ describe '#etag_from_header' do
127
+ before(:each) do
128
+ @header = "HTTP/1.0 200 OK\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\nETag: ziEyTl4q9GH04BR4jgkImd0GvSE\r\nP3P: CP=\"ALL DSP COR NID CUR OUR NOR\"\r\nConnection: close\r\nContent-Type: text/xml;charset=utf-8\r\n\r\n"
129
+ end
130
+
131
+ it "should return the etag from the header if it exists" do
132
+ Feedzirra::Feed.etag_from_header(@header).should == "ziEyTl4q9GH04BR4jgkImd0GvSE"
133
+ end
134
+
135
+ it "should return nil if there is no etag in the header" do
136
+ Feedzirra::Feed.etag_from_header("foo").should be_nil
137
+ end
138
+
139
+ end
140
+
141
+ describe '#last_modified_from_header' do
142
+ before(:each) do
143
+ @header = "HTTP/1.0 200 OK\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\nETag: ziEyTl4q9GH04BR4jgkImd0GvSE\r\nP3P: CP=\"ALL DSP COR NID CUR OUR NOR\"\r\nConnection: close\r\nContent-Type: text/xml;charset=utf-8\r\n\r\n"
144
+ end
145
+
146
+ it "should return the last modified date from the header if it exists" do
147
+ Feedzirra::Feed.last_modified_from_header(@header).should == Time.parse("Wed, 28 Jan 2009 04:10:32 GMT")
148
+ end
149
+
150
+ it "should return nil if there is no last modified date in the header" do
151
+ Feedzirra::Feed.last_modified_from_header("foo").should be_nil
152
+ end
153
+ end
154
+
155
+ describe "fetching feeds" do
156
+ before(:each) do
157
+ @paul_feed = { :xml => load_sample("PaulDixExplainsNothing.xml"), :url => "http://feeds.feedburner.com/PaulDixExplainsNothing" }
158
+ @trotter_feed = { :xml => load_sample("TrotterCashionHome.xml"), :url => "http://feeds2.feedburner.com/trottercashion" }
159
+ end
160
+
161
+ describe "#fetch_raw" do
162
+ before(:each) do
163
+ @cmock = stub('cmock', :header_str => '', :body_str => @paul_feed[:xml] )
164
+ @multi = stub('curl_multi', :add => true, :perform => true)
165
+ @curl_easy = stub('curl_easy')
166
+ @curl = stub('curl', :headers => {}, :follow_location= => true, :on_failure => true)
167
+ @curl.stub!(:on_success).and_yield(@cmock)
168
+
169
+ Curl::Multi.stub!(:new).and_return(@multi)
170
+ Curl::Easy.stub!(:new).and_yield(@curl).and_return(@curl_easy)
171
+ end
172
+
173
+ it "should set user agent if it's passed as an option" do
174
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url], :user_agent => 'Custom Useragent')
175
+ @curl.headers['User-Agent'].should == 'Custom Useragent'
176
+ end
177
+
178
+ it "should set user agent to default if it's not passed as an option" do
179
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url])
180
+ @curl.headers['User-Agent'].should == Feedzirra::Feed::USER_AGENT
181
+ end
182
+
183
+ it "should set if modified since as an option if passed" do
184
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url], :if_modified_since => Time.parse("Wed, 28 Jan 2009 04:10:32 GMT"))
185
+ @curl.headers["If-Modified-Since"].should == 'Wed, 28 Jan 2009 04:10:32 GMT'
186
+ end
187
+
188
+ it "should set if none match as an option if passed" do
189
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url], :if_none_match => 'ziEyTl4q9GH04BR4jgkImd0GvSE')
190
+ @curl.headers["If-None-Match"].should == 'ziEyTl4q9GH04BR4jgkImd0GvSE'
191
+ end
192
+
193
+ it 'should set userpwd for http basic authentication if :http_authentication is passed' do
194
+ @curl.should_receive(:userpwd=).with('username:password')
195
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url], :http_authentication => ['username', 'password'])
196
+ end
197
+
198
+ it 'should set proxy url for proxy if :proxy_url is passed' do
199
+ @curl.should_receive(:proxy_url=).with('10.1.0.1')
200
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url], :proxy_url => '10.1.0.1')
201
+ end
202
+
203
+ it 'should set proxy port for proxy if :proxy_port is passed' do
204
+ @curl.should_receive(:proxy_port=).with(5164)
205
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url], :proxy_port => 5164)
206
+ end
207
+
208
+ it 'should set accepted encodings' do
209
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url], :compress => true)
210
+ @curl.headers["Accept-encoding"].should == 'gzip, deflate'
211
+ end
212
+
213
+ it "should return raw xml" do
214
+ Feedzirra::Feed.fetch_raw(@paul_feed[:url]).should =~ /^#{Regexp.escape('<?xml version="1.0" encoding="UTF-8"?>')}/
215
+ end
216
+
217
+ it "should take multiple feed urls and return a hash of urls and response xml" do
218
+ multi = stub('curl_multi', :add => true, :perform => true)
219
+ Curl::Multi.stub!(:new).and_return(multi)
220
+
221
+ paul_response = stub('paul_response', :header_str => '', :body_str => @paul_feed[:xml] )
222
+ trotter_response = stub('trotter_response', :header_str => '', :body_str => @trotter_feed[:xml] )
223
+
224
+ paul_curl = stub('paul_curl', :headers => {}, :follow_location= => true, :on_failure => true)
225
+ paul_curl.stub!(:on_success).and_yield(paul_response)
226
+
227
+ trotter_curl = stub('trotter_curl', :headers => {}, :follow_location= => true, :on_failure => true)
228
+ trotter_curl.stub!(:on_success).and_yield(trotter_response)
229
+
230
+ Curl::Easy.should_receive(:new).with(@paul_feed[:url]).ordered.and_yield(paul_curl)
231
+ Curl::Easy.should_receive(:new).with(@trotter_feed[:url]).ordered.and_yield(trotter_curl)
232
+
233
+ results = Feedzirra::Feed.fetch_raw([@paul_feed[:url], @trotter_feed[:url]])
234
+ results.keys.should include(@paul_feed[:url])
235
+ results.keys.should include(@trotter_feed[:url])
236
+ results[@paul_feed[:url]].should =~ /Paul Dix/
237
+ results[@trotter_feed[:url]].should =~ /Trotter Cashion/
238
+ end
239
+
240
+ it "should always return a hash when passed an array" do
241
+ results = Feedzirra::Feed.fetch_raw([@paul_feed[:url]])
242
+ results.class.should == Hash
243
+ end
244
+ end
245
+
246
+ describe "#add_url_to_multi" do
247
+ before(:each) do
248
+ @multi = Curl::Multi.new
249
+ @multi.stub!(:add)
250
+ @easy_curl = Curl::Easy.new(@paul_feed[:url])
251
+
252
+ Curl::Easy.should_receive(:new).and_yield(@easy_curl)
253
+ end
254
+
255
+ it "should set user agent if it's passed as an option" do
256
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, :user_agent => 'My cool application')
257
+ @easy_curl.headers["User-Agent"].should == 'My cool application'
258
+ end
259
+
260
+ it "should set user agent to default if it's not passed as an option" do
261
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
262
+ @easy_curl.headers["User-Agent"].should == Feedzirra::Feed::USER_AGENT
263
+ end
264
+
265
+ it "should set if modified since as an option if passed" do
266
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, :if_modified_since => Time.parse("Jan 25 2009 04:10:32 GMT"))
267
+ @easy_curl.headers["If-Modified-Since"].should == 'Sun, 25 Jan 2009 04:10:32 GMT'
268
+ end
269
+
270
+ it 'should set follow location to true' do
271
+ @easy_curl.should_receive(:follow_location=).with(true)
272
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
273
+ end
274
+
275
+ it 'should set userpwd for http basic authentication if :http_authentication is passed' do
276
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, :http_authentication => ['myusername', 'mypassword'])
277
+ @easy_curl.userpwd.should == 'myusername:mypassword'
278
+ end
279
+
280
+ it 'should set accepted encodings' do
281
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {:compress => true})
282
+ @easy_curl.headers["Accept-encoding"].should == 'gzip, deflate'
283
+ end
284
+
285
+ it "should set if_none_match as an option if passed" do
286
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, :if_none_match => 'ziEyTl4q9GH04BR4jgkImd0GvSE')
287
+ @easy_curl.headers["If-None-Match"].should == 'ziEyTl4q9GH04BR4jgkImd0GvSE'
288
+ end
289
+
290
+ describe 'on success' do
291
+ before(:each) do
292
+ @feed = mock('feed', :feed_url= => true, :etag= => true, :last_modified= => true)
293
+ Feedzirra::Feed.stub!(:decode_content).and_return(@paul_feed[:xml])
294
+ Feedzirra::Feed.stub!(:determine_feed_parser_for_xml).and_return(Feedzirra::Parser::AtomFeedBurner)
295
+ Feedzirra::Parser::AtomFeedBurner.stub!(:parse).and_return(@feed)
296
+ Feedzirra::Feed.stub!(:etag_from_header).and_return('ziEyTl4q9GH04BR4jgkImd0GvSE')
297
+ Feedzirra::Feed.stub!(:last_modified_from_header).and_return('Wed, 28 Jan 2009 04:10:32 GMT')
298
+ end
299
+
300
+ it 'should decode the response body' do
301
+ Feedzirra::Feed.should_receive(:decode_content).with(@easy_curl).and_return(@paul_feed[:xml])
302
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
303
+ @easy_curl.on_success.call(@easy_curl)
304
+ end
305
+
306
+ it 'should determine the xml parser class' do
307
+ Feedzirra::Feed.should_receive(:determine_feed_parser_for_xml).with(@paul_feed[:xml]).and_return(Feedzirra::Parser::AtomFeedBurner)
308
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
309
+ @easy_curl.on_success.call(@easy_curl)
310
+ end
311
+
312
+ it 'should parse the xml' do
313
+ Feedzirra::Parser::AtomFeedBurner.should_receive(:parse).with(@paul_feed[:xml]).and_return(@feed)
314
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
315
+ @easy_curl.on_success.call(@easy_curl)
316
+ end
317
+
318
+ describe 'when a compatible xml parser class is found' do
319
+ it 'should set the last effective url to the feed url' do
320
+ @easy_curl.should_receive(:last_effective_url).and_return(@paul_feed[:url])
321
+ @feed.should_receive(:feed_url=).with(@paul_feed[:url])
322
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
323
+ @easy_curl.on_success.call(@easy_curl)
324
+ end
325
+
326
+ it 'should set the etags on the feed' do
327
+ @feed.should_receive(:etag=).with('ziEyTl4q9GH04BR4jgkImd0GvSE')
328
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
329
+ @easy_curl.on_success.call(@easy_curl)
330
+ end
331
+
332
+ it 'should set the last modified on the feed' do
333
+ @feed.should_receive(:last_modified=).with('Wed, 28 Jan 2009 04:10:32 GMT')
334
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, {})
335
+ @easy_curl.on_success.call(@easy_curl)
336
+ end
337
+
338
+ it 'should add the feed to the responses' do
339
+ responses = {}
340
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], responses, {})
341
+ @easy_curl.on_success.call(@easy_curl)
342
+
343
+ responses.length.should == 1
344
+ responses['http://feeds.feedburner.com/PaulDixExplainsNothing'].should == @feed
345
+ end
346
+
347
+ it 'should call proc if :on_success option is passed' do
348
+ success = lambda { |url, feed| }
349
+ success.should_receive(:call).with(@paul_feed[:url], @feed)
350
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_success => success })
351
+ @easy_curl.on_success.call(@easy_curl)
352
+ end
353
+ end
354
+
355
+ describe 'when no compatible xml parser class is found' do
356
+ it 'should raise a NoParserAvailable exception'
357
+ end
358
+ end
359
+
360
+ describe 'on failure' do
361
+ before(:each) do
362
+ @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"
363
+ @body = 'Page could not be found.'
364
+
365
+ @easy_curl.stub!(:response_code).and_return(404)
366
+ @easy_curl.stub!(:header_str).and_return(@headers)
367
+ @easy_curl.stub!(:body_str).and_return(@body)
368
+ end
369
+
370
+ it 'should call proc if :on_failure option is passed' do
371
+ failure = lambda { |url, feed| }
372
+ failure.should_receive(:call).with(@paul_feed[:url], 404, @headers, @body)
373
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], {}, { :on_failure => failure })
374
+ @easy_curl.on_failure.call(@easy_curl)
375
+ end
376
+
377
+ it 'should return the http code in the responses' do
378
+ responses = {}
379
+ Feedzirra::Feed.add_url_to_multi(@multi, @paul_feed[:url], [], responses, {})
380
+ @easy_curl.on_failure.call(@easy_curl)
381
+
382
+ responses.length.should == 1
383
+ responses[@paul_feed[:url]].should == 404
384
+ end
385
+ end
386
+ end
387
+
388
+ describe "#add_feed_to_multi" do
389
+ before(:each) do
390
+ @multi = Curl::Multi.new
391
+ @multi.stub!(:add)
392
+ @easy_curl = Curl::Easy.new(@paul_feed[:url])
393
+ @feed = Feedzirra::Feed.parse(sample_feedburner_atom_feed)
394
+
395
+ Curl::Easy.should_receive(:new).and_yield(@easy_curl)
396
+ end
397
+
398
+ it "should set user agent if it's passed as an option" do
399
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, :user_agent => 'My cool application')
400
+ @easy_curl.headers["User-Agent"].should == 'My cool application'
401
+ end
402
+
403
+ it "should set user agent to default if it's not passed as an option" do
404
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
405
+ @easy_curl.headers["User-Agent"].should == Feedzirra::Feed::USER_AGENT
406
+ end
407
+
408
+ it "should set if modified since as an option if passed"
409
+
410
+ it 'should set follow location to true' do
411
+ @easy_curl.should_receive(:follow_location=).with(true)
412
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
413
+ end
414
+
415
+ it 'should set userpwd for http basic authentication if :http_authentication is passed' do
416
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, :http_authentication => ['myusername', 'mypassword'])
417
+ @easy_curl.userpwd.should == 'myusername:mypassword'
418
+ end
419
+
420
+ it "should set if_none_match as an option if passed" do
421
+ @feed.etag = 'ziEyTl4q9GH04BR4jgkImd0GvSE'
422
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
423
+ @easy_curl.headers["If-None-Match"].should == 'ziEyTl4q9GH04BR4jgkImd0GvSE'
424
+ end
425
+
426
+ describe 'on success' do
427
+ before(:each) do
428
+ @new_feed = @feed.clone
429
+ @feed.stub!(:update_from_feed)
430
+ Feedzirra::Feed.stub!(:decode_content).and_return(@paul_feed[:xml])
431
+ Feedzirra::Feed.stub!(:determine_feed_parser_for_xml).and_return(Feedzirra::Parser::AtomFeedBurner)
432
+ Feedzirra::Parser::AtomFeedBurner.stub!(:parse).and_return(@new_feed)
433
+ Feedzirra::Feed.stub!(:etag_from_header).and_return('ziEyTl4q9GH04BR4jgkImd0GvSE')
434
+ Feedzirra::Feed.stub!(:last_modified_from_header).and_return('Wed, 28 Jan 2009 04:10:32 GMT')
435
+ end
436
+
437
+ it 'should process the next feed in the queue'
438
+
439
+ it 'should parse the updated feed' do
440
+ Feedzirra::Parser::AtomFeedBurner.should_receive(:parse).and_return(@new_feed)
441
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
442
+ @easy_curl.on_success.call(@easy_curl)
443
+ end
444
+
445
+ it 'should set the last effective url to the feed url' do
446
+ @easy_curl.should_receive(:last_effective_url).and_return(@paul_feed[:url])
447
+ @new_feed.should_receive(:feed_url=).with(@paul_feed[:url])
448
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
449
+ @easy_curl.on_success.call(@easy_curl)
450
+ end
451
+
452
+ it 'should set the etags on the feed' do
453
+ @new_feed.should_receive(:etag=).with('ziEyTl4q9GH04BR4jgkImd0GvSE')
454
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
455
+ @easy_curl.on_success.call(@easy_curl)
456
+ end
457
+
458
+ it 'should set the last modified on the feed' do
459
+ @new_feed.should_receive(:last_modified=).with('Wed, 28 Jan 2009 04:10:32 GMT')
460
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
461
+ @easy_curl.on_success.call(@easy_curl)
462
+ end
463
+
464
+ it 'should add the feed to the responses' do
465
+ responses = {}
466
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], responses, {})
467
+ @easy_curl.on_success.call(@easy_curl)
468
+
469
+ responses.length.should == 1
470
+ responses['http://feeds.feedburner.com/PaulDixExplainsNothing'].should == @feed
471
+ end
472
+
473
+ it 'should call proc if :on_success option is passed' do
474
+ success = lambda { |feed| }
475
+ success.should_receive(:call).with(@feed)
476
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, { :on_success => success })
477
+ @easy_curl.on_success.call(@easy_curl)
478
+ end
479
+
480
+ it 'should call update from feed on the old feed with the updated feed' do
481
+ @feed.should_receive(:update_from_feed).with(@new_feed)
482
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, {})
483
+ @easy_curl.on_success.call(@easy_curl)
484
+ end
485
+ end
486
+
487
+ describe 'on failure' do
488
+ before(:each) do
489
+ @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"
490
+ @body = 'Page could not be found.'
491
+
492
+ @easy_curl.stub!(:response_code).and_return(404)
493
+ @easy_curl.stub!(:header_str).and_return(@headers)
494
+ @easy_curl.stub!(:body_str).and_return(@body)
495
+ end
496
+
497
+ it 'should call on success callback if the response code is 304' do
498
+ success = lambda { |feed| }
499
+ success.should_receive(:call).with(@feed)
500
+ @easy_curl.should_receive(:response_code).and_return(304)
501
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], {}, { :on_success => success })
502
+ @easy_curl.on_failure.call(@easy_curl)
503
+ end
504
+
505
+ it 'should return the http code in the responses' do
506
+ responses = {}
507
+ Feedzirra::Feed.add_feed_to_multi(@multi, @feed, [], responses, {})
508
+ @easy_curl.on_failure.call(@easy_curl)
509
+
510
+ responses.length.should == 1
511
+ responses['http://www.pauldix.net/'].should == 404
512
+ end
513
+ end
514
+ end
515
+
516
+ describe "#fetch_and_parse" do
517
+ it 'should initiate the fetching and parsing using multicurl'
518
+ it "should pass any request options through to add_url_to_multi"
519
+ it 'should slice the feeds into groups of thirty for processing'
520
+ it "should return a feed object if a single feed is passed in"
521
+ it "should return an return an array of feed objects if multiple feeds are passed in"
522
+ end
523
+
524
+ describe "#decode_content" do
525
+ before(:each) do
526
+ @curl_easy = mock('curl_easy', :body_str => '<xml></xml>')
527
+ end
528
+
529
+ it 'should decode the response body using gzip if the Content-Encoding: is gzip' do
530
+ @curl_easy.stub!(:header_str).and_return('Content-Encoding: gzip')
531
+ string_io = mock('stringio', :read => @curl_easy.body_str, :close => true)
532
+ StringIO.should_receive(:new).and_return(string_io)
533
+ Zlib::GzipReader.should_receive(:new).with(string_io).and_return(string_io)
534
+ Feedzirra::Feed.decode_content(@curl_easy)
535
+ end
536
+
537
+ it 'should deflate the response body using inflate if the Content-Encoding: is deflate' do
538
+ @curl_easy.stub!(:header_str).and_return('Content-Encoding: deflate')
539
+ Zlib::Inflate.should_receive(:inflate).with(@curl_easy.body_str)
540
+ Feedzirra::Feed.decode_content(@curl_easy)
541
+ end
542
+
543
+ it 'should return the response body if it is not encoded' do
544
+ @curl_easy.stub!(:header_str).and_return('')
545
+ Feedzirra::Feed.decode_content(@curl_easy).should == '<xml></xml>'
546
+ end
547
+ end
548
+
549
+ describe "#update" do
550
+ it 'should perform the updating using multicurl'
551
+ it "should pass any request options through to add_feed_to_multi"
552
+ it "should return a feed object if a single feed is passed in"
553
+ it "should return an return an array of feed objects if multiple feeds are passed in"
554
+ end
555
+ end
556
+ end