mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Add support for a ":cookies" option to be used at the class level, or as
an option on any individual call. It should be passed a hash, which will be converted to the proper format and added to the request headers when the call is made. No special handling is in place for semi-colons, commas, or whitespace. The RFC doesn't specify an official method for escaping them. That's your problem. Signed-off-by: John Nunemaker <nunemaker@gmail.com>
This commit is contained in:
parent
26fd4eebfa
commit
6db6670994
6 changed files with 133 additions and 3 deletions
2
Manifest
2
Manifest
|
@ -20,6 +20,7 @@ features/supports_redirection.feature
|
|||
History
|
||||
httparty.gemspec
|
||||
lib/core_extensions.rb
|
||||
lib/httparty/cookie_hash.rb
|
||||
lib/httparty/exceptions.rb
|
||||
lib/httparty/request.rb
|
||||
lib/httparty/response.rb
|
||||
|
@ -38,6 +39,7 @@ spec/fixtures/google.html
|
|||
spec/fixtures/twitter.json
|
||||
spec/fixtures/twitter.xml
|
||||
spec/fixtures/undefined_method_add_node_for_nil.xml
|
||||
spec/httparty/cookie_hash_spec.rb
|
||||
spec/httparty/request_spec.rb
|
||||
spec/httparty_spec.rb
|
||||
spec/spec.opts
|
||||
|
|
|
@ -11,8 +11,8 @@ Gem::Specification.new do |s|
|
|||
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
||||
s.email = %q{nunemaker@gmail.com}
|
||||
s.executables = ["httparty"]
|
||||
s.extra_rdoc_files = ["bin/httparty", "lib/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "lib/module_level_inheritable_attributes.rb", "README"]
|
||||
s.files = ["bin/httparty", "examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "History", "httparty.gemspec", "lib/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "lib/module_level_inheritable_attributes.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "setup.rb", "spec/as_buggery_spec.rb", "spec/fixtures/delicious.xml", "spec/fixtures/empty.xml", "spec/fixtures/google.html", "spec/fixtures/twitter.json", "spec/fixtures/twitter.xml", "spec/fixtures/undefined_method_add_node_for_nil.xml", "spec/httparty/request_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "website/css/common.css", "website/index.html"]
|
||||
s.extra_rdoc_files = ["bin/httparty", "lib/core_extensions.rb", "lib/httparty/cookie_hash.rb", "lib/httparty/exceptions.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "lib/module_level_inheritable_attributes.rb", "README"]
|
||||
s.files = ["bin/httparty", "cucumber.yml", "examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "features/basic_authentication.feature", "features/command_line.feature", "features/deals_with_http_error_codes.feature", "features/handles_multiple_formats.feature", "features/steps/env.rb", "features/steps/httparty_response_steps.rb", "features/steps/httparty_steps.rb", "features/steps/mongrel_helper.rb", "features/steps/remote_service_steps.rb", "features/supports_redirection.feature", "History", "httparty.gemspec", "lib/core_extensions.rb", "lib/httparty/cookie_hash.rb", "lib/httparty/exceptions.rb", "lib/httparty/request.rb", "lib/httparty/response.rb", "lib/httparty/version.rb", "lib/httparty.rb", "lib/module_level_inheritable_attributes.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "setup.rb", "spec/as_buggery_spec.rb", "spec/fixtures/delicious.xml", "spec/fixtures/empty.xml", "spec/fixtures/google.html", "spec/fixtures/twitter.json", "spec/fixtures/twitter.xml", "spec/fixtures/undefined_method_add_node_for_nil.xml", "spec/httparty/cookie_hash_spec.rb", "spec/httparty/request_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "website/css/common.css", "website/index.html"]
|
||||
s.has_rdoc = true
|
||||
s.homepage = %q{http://httparty.rubyforge.org}
|
||||
s.post_install_message = %q{When you HTTParty, you must party hard!}
|
||||
|
|
|
@ -58,6 +58,12 @@ module HTTParty
|
|||
default_options[:headers] ||= {}
|
||||
default_options[:headers].merge!(h)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
def format(f)
|
||||
raise UnsupportedFormat, "Must be one of: #{AllowedFormats.values.join(', ')}" unless AllowedFormats.value?(f)
|
||||
|
@ -82,8 +88,18 @@ module HTTParty
|
|||
|
||||
private
|
||||
def perform_request(http_method, path, options) #:nodoc:
|
||||
process_cookies(options)
|
||||
Request.new(http_method, path, default_options.dup.merge(options)).perform
|
||||
end
|
||||
|
||||
def process_cookies(options) #:nodoc:
|
||||
return unless options[:cookies] || default_options[:cookies]
|
||||
options[:headers] ||= {}
|
||||
options[:headers]["cookie"] = cookies(options[:cookies] || {}).to_cookie_string
|
||||
|
||||
default_options.delete(:cookies)
|
||||
options.delete(:cookies)
|
||||
end
|
||||
end
|
||||
|
||||
def self.normalize_base_uri(url) #:nodoc:
|
||||
|
@ -119,4 +135,5 @@ end
|
|||
|
||||
require 'httparty/exceptions'
|
||||
require 'httparty/request'
|
||||
require 'httparty/response'
|
||||
require 'httparty/response'
|
||||
require 'httparty/cookie_hash'
|
||||
|
|
9
lib/httparty/cookie_hash.rb
Normal file
9
lib/httparty/cookie_hash.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class HTTParty::CookieHash < Hash
|
||||
def add_cookies(hash)
|
||||
merge!(hash)
|
||||
end
|
||||
|
||||
def to_cookie_string
|
||||
collect { |k, v| "#{k}=#{v}" }.join("; ")
|
||||
end
|
||||
end
|
38
spec/httparty/cookie_hash_spec.rb
Normal file
38
spec/httparty/cookie_hash_spec.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
require File.join(File.dirname(__FILE__), '../spec_helper')
|
||||
|
||||
describe HTTParty::CookieHash do
|
||||
before(:each) do
|
||||
@cookie_hash = HTTParty::CookieHash.new
|
||||
end
|
||||
|
||||
describe "#add_cookies" do
|
||||
it "should add new key/value pairs to the hash" do
|
||||
@cookie_hash.add_cookies(:foo => "bar")
|
||||
@cookie_hash.add_cookies(:rofl => "copter")
|
||||
@cookie_hash.length.should eql(2)
|
||||
end
|
||||
|
||||
it "should overwrite any existing key" do
|
||||
@cookie_hash.add_cookies(:foo => "bar")
|
||||
@cookie_hash.add_cookies(:foo => "copter")
|
||||
@cookie_hash.length.should eql(1)
|
||||
@cookie_hash[:foo].should eql("copter")
|
||||
end
|
||||
end
|
||||
|
||||
# The regexen are required because Hashes aren't ordered, so a test against
|
||||
# a hardcoded string was randomly failing.
|
||||
describe "#to_cookie_string" do
|
||||
before(:each) do
|
||||
@cookie_hash.add_cookies(:foo => "bar")
|
||||
@cookie_hash.add_cookies(:rofl => "copter")
|
||||
@s = @cookie_hash.to_cookie_string
|
||||
end
|
||||
|
||||
it "should format the key/value pairs, delimited by semi-colons" do
|
||||
@s.should match(/foo=bar/)
|
||||
@s.should match(/rofl=copter/)
|
||||
@s.should match(/^\w+=\w+; \w+=\w+$/)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -49,6 +49,70 @@ describe HTTParty do
|
|||
@klass.headers.should == init_headers
|
||||
end
|
||||
end
|
||||
|
||||
describe "cookies" do
|
||||
def expect_cookie_header(s)
|
||||
HTTParty::Request.should_receive(:new) \
|
||||
.with(anything, anything, hash_including({ :headers => { "cookie" => s } })) \
|
||||
.and_return(mock("mock response", :perform => nil))
|
||||
end
|
||||
|
||||
it "should not be in the headers by default" do
|
||||
HTTParty::Request.stub!(:new).and_return(stub(nil, :perform => nil))
|
||||
@klass.get("")
|
||||
@klass.headers.keys.should_not include("cookie")
|
||||
end
|
||||
|
||||
it "should raise an ArgumentError if passed a non-Hash" do
|
||||
lambda do
|
||||
@klass.cookies("nonsense")
|
||||
end.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "should allow a cookie to be specified with a one-off request" do
|
||||
expect_cookie_header "type=snickerdoodle"
|
||||
@klass.get("", :cookies => { :type => "snickerdoodle" })
|
||||
end
|
||||
|
||||
describe "when a cookie is set at the class level" do
|
||||
before(:each) do
|
||||
@klass.cookies({ :type => "snickerdoodle" })
|
||||
end
|
||||
|
||||
it "should include that cookie in the request" do
|
||||
expect_cookie_header "type=snickerdoodle"
|
||||
@klass.get("")
|
||||
end
|
||||
|
||||
it "should allow the class defaults to be overridden" do
|
||||
expect_cookie_header "type=chocolate_chip"
|
||||
|
||||
@klass.get("", :cookies => { :type => "chocolate_chip" })
|
||||
end
|
||||
end
|
||||
|
||||
describe "in a class with multiple methods that use different cookies" do
|
||||
before(:each) do
|
||||
@klass.instance_eval do
|
||||
def first_method
|
||||
get("first_method", :cookies => { :first_method_cookie => "foo" })
|
||||
end
|
||||
|
||||
def second_method
|
||||
get("second_method", :cookies => { :second_method_cookie => "foo" })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "should not allow cookies used in one method to carry over into other methods" do
|
||||
expect_cookie_header "first_method_cookie=foo"
|
||||
@klass.first_method
|
||||
|
||||
expect_cookie_header "second_method_cookie=foo"
|
||||
@klass.second_method
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "default params" do
|
||||
it "should default to empty hash" do
|
||||
|
|
Loading…
Reference in a new issue