Permalink
Browse files

fix handling of redirect_to in to_hash and from_hash

  • Loading branch information...
1 parent 0657c75 commit 31a1515e9d4ac45813fb83527421749aae17fc8d @chriskite committed Jan 20, 2012
Showing with 54 additions and 17 deletions.
  1. +1 −1 lib/anemone/http.rb
  2. +1 −1 lib/anemone/page.rb
  3. +46 −15 spec/page_spec.rb
  4. +6 −0 spec/storage_spec.rb
View
@@ -112,7 +112,7 @@ def get(url, referer = nil)
response, response_time = get_response(loc, referer)
code = Integer(response.code)
- redirect_to = response.is_a?(Net::HTTPRedirection) ? URI(response['location']).normalize : nil
+ redirect_to = response.is_a?(Net::HTTPRedirection) ? URI(response['location']).normalize : nil
yield response, code, loc, redirect_to, response_time
limit -= 1
end while (loc = redirect_to) && allowed?(redirect_to, url) && limit > 0
View
@@ -190,7 +190,7 @@ def self.from_hash(hash)
'@visited' => hash['visited'],
'@depth' => hash['depth'].to_i,
'@referer' => hash['referer'],
- '@redirect_to' => URI(hash['redirect_to'] || ''),
+ '@redirect_to' => (!!hash['redirect_to'] && !hash['redirect_to'].empty?) ? URI(hash['redirect_to']) : nil,
'@response_time' => hash['response_time'].to_i,
'@fetched' => hash['fetched']
}.each do |var, value|
View
@@ -74,25 +74,56 @@ module Anemone
@page.cookies.should == []
end
- it "should have a to_hash method that converts the page to a hash" do
- hash = @page.to_hash
- hash['url'].should == @page.url.to_s
- hash['referer'].should == @page.referer.to_s
- hash['links'].should == @page.links.map(&:to_s)
+ describe "#to_hash" do
+ it "converts the page to a hash" do
+ hash = @page.to_hash
+ hash['url'].should == @page.url.to_s
+ hash['referer'].should == @page.referer.to_s
+ hash['links'].should == @page.links.map(&:to_s)
+ end
+
+ context "when redirect_to is nil" do
+ it "sets 'redirect_to' to nil in the hash" do
+ @page.redirect_to.should be_nil
+ @page.to_hash[:redirect_to].should be_nil
+ end
+ end
+
+ context "when redirect_to is a non-nil URI" do
+ it "sets 'redirect_to' to the URI string" do
+ new_page = Page.new(URI(SPEC_DOMAIN), {:redirect_to => URI(SPEC_DOMAIN + '1')})
+ new_page.redirect_to.to_s.should == SPEC_DOMAIN + '1'
+ new_page.to_hash['redirect_to'].should == SPEC_DOMAIN + '1'
+ end
+ end
end
- it "should have a from_hash method to convert from a hash to a Page" do
- page = @page.dup
- page.depth = 1
- converted = Page.from_hash(page.to_hash)
- converted.links.should == page.links
- converted.depth.should == page.depth
+ describe "#from_hash" do
+ it "converts from a hash to a Page" do
+ page = @page.dup
+ page.depth = 1
+ converted = Page.from_hash(page.to_hash)
+ converted.links.should == page.links
+ converted.depth.should == page.depth
+ end
+
+ it 'handles a from_hash with a nil redirect_to' do
+ page_hash = @page.to_hash
+ page_hash['redirect_to'] = nil
+ lambda{Page.from_hash(page_hash)}.should_not raise_error(URI::InvalidURIError)
+ Page.from_hash(page_hash).redirect_to.should be_nil
+ end
end
- it 'should handle a from_hash with a nil redirect_to' do
- page_hash = @page.to_hash
- page_hash['redirect_to'] = nil
- lambda{Page.from_hash(page_hash)}.should_not raise_error(URI::InvalidURIError)
+ describe "#redirect_to" do
+ context "when the page was a redirect" do
+ it "returns a URI of the page it redirects to" do
+ new_page = Page.new(URI(SPEC_DOMAIN), {:redirect_to => URI(SPEC_DOMAIN + '1')})
+ redirect = new_page.redirect_to
+ redirect.should be_a(URI)
+ redirect.to_s.should == SPEC_DOMAIN + '1'
+ end
+ end
end
end
View
@@ -113,6 +113,12 @@ module Storage
merged.should === @store
end
+
+ it "should correctly deserialize nil redirect_to when loading" do
+ @page.redirect_to.should be_nil
+ @store[@url] = @page
+ @store[@url].redirect_to.should be_nil
+ end
end
describe PStore do

0 comments on commit 31a1515

Please sign in to comment.