Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dpetersen/134933 to your computer and use it in GitHub Desktop.
Save dpetersen/134933 to your computer and use it in GitHub Desktop.
From e83c6d9e3ea21c3c55f20831bef5aa4390ef2e91 Mon Sep 17 00:00:00 2001
From: Don Petersen <don@donpetersen.net>
Date: Tue, 23 Jun 2009 19:28:42 -0500
Subject: [PATCH] Fixed a bug in the cookies class method that caused cookies to be
forgotten after the first request. Reported by David Turnbull.
---
lib/httparty.rb | 13 +++++++------
spec/httparty_spec.rb | 7 +++++++
2 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/lib/httparty.rb b/lib/httparty.rb
index 362df86..b705f3d 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 07c042d..4996b0f 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"
--
1.6.3.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment