Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make caching easier for non-memory caches. #345

Merged
merged 2 commits into from
Dec 27, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix Request#hash for different key orders.
  • Loading branch information
mschulkind committed Dec 27, 2013
commit 579249ccdd9b610c159b5c8834b74e6968374ef0
13 changes: 12 additions & 1 deletion lib/typhoeus/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def eql?(other)
#
# @api private
def hash
Zlib.crc32 "#{self.class.name}#{base_url}#{options}"
Zlib.crc32 "#{self.class.name}#{base_url}#{hashable_string_for(options)}"
end

# Mimics libcurls POST body generation. This is not accurate, but good
Expand Down Expand Up @@ -187,6 +187,17 @@ def fuzzy_hash_eql?(left, right)
end
end

def hashable_string_for(obj)
case obj
when Hash
hashable_string_for(obj.sort_by {|sub_obj| sub_obj.first.to_s})
when Array
obj.map {|sub_obj| hashable_string_for(sub_obj)}.to_s
else
obj.to_s
end
end

# Sets default header and verbose when turned on.
def set_defaults
if @options[:headers]
Expand Down
30 changes: 26 additions & 4 deletions spec/typhoeus/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,37 @@

describe "#hash" do
context "when request.eql?(other)" do
let(:other) { Typhoeus::Request.new(base_url, options) }
context "when different order" do
let(:other_options) {
{:headers => { 'User-Agent' => "Fubar" }, :verbose => true }
}
let(:other) { Typhoeus::Request.new(base_url, other_options)}

it "has same hashes" do
expect(request.hash).to eq(other.hash)
end
end

it "has same hashes" do
expect(request.hash).to eq(other.hash)
context "when same order" do
let(:other) { Typhoeus::Request.new(base_url, options) }

it "has same hashes" do
expect(request.hash).to eq(other.hash)
end
end

context "when hashes with different orders are contained in arrays" do
let(:request) { Typhoeus::Request.new(base_url, :params => [{:b => 2, :a => 1}]) }
let(:other) { Typhoeus::Request.new(base_url, :params => [{:a => 1, :b => 2}]) }
it "has different hashes" do
expect(request.hash).to eq(other.hash)
end
end
end

context "when not request.eql?(other)" do
let(:other) { Typhoeus::Request.new("base_url", {}) }
let(:request) { Typhoeus::Request.new(base_url, :params => {:foo => 'bar'}) }
let(:other) { Typhoeus::Request.new(base_url, :params => {:foo => 'baz'}) }

it "has different hashes" do
expect(request.hash).to_not eq(other.hash)
Expand Down