diff --git a/lib/httparty.rb b/lib/httparty.rb index a6b4512..4a5a74a 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -7,6 +7,8 @@ require 'rubygems' gem 'crack' require 'crack' +require 'httparty/cookie_hash' + module HTTParty AllowedFormats = { @@ -26,7 +28,9 @@ module HTTParty base.extend ClassMethods base.send :include, HTTParty::ModuleInheritableAttributes base.send(:mattr_inheritable, :default_options) + base.send(:mattr_inheritable, :default_cookies) base.instance_variable_set("@default_options", {}) + base.instance_variable_set("@default_cookies", CookieHash.new) end module ClassMethods @@ -90,8 +94,7 @@ module HTTParty def cookies(h={}) raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash) - default_options[:cookies] ||= CookieHash.new - default_options[:cookies].add_cookies(h) + default_cookies.add_cookies(h) end # Allows setting the format with which to parse. @@ -157,11 +160,10 @@ module HTTParty end def process_cookies(options) #:nodoc: - return unless options[:cookies] || default_options[:cookies] + return unless options[:cookies] || default_cookies options[:headers] ||= {} - options[:headers]["cookie"] = cookies(options[:cookies] || {}).to_cookie_string + options[:headers]["cookie"] = cookies.merge(options[:cookies] || {}).to_cookie_string - default_options.delete(:cookies) options.delete(:cookies) end end @@ -198,7 +200,6 @@ module HTTParty end end -require 'httparty/cookie_hash' require 'httparty/core_extensions' require 'httparty/exceptions' require 'httparty/request' diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 9679f5c..83fc558 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -96,6 +96,13 @@ describe HTTParty do @klass.get("") end + it "should pass the proper cookies when requested multiple times" do + 2.times do + expect_cookie_header "type=snickerdoodle" + @klass.get("") + end + end + it "should allow the class defaults to be overridden" do expect_cookie_header "type=chocolate_chip"