Browse files

the options[:cookies] can be set with the HTTPResponse object which g…

…enerated by the HttpRequest.method...
  • Loading branch information...
1 parent 36fe8ec commit bc23cf2471f9191305d25b48ac0c118f51227050 xianhua.zhou@gmail.com committed Oct 5, 2009
Showing with 14 additions and 6 deletions.
  1. +8 −6 lib/http_request.rb
  2. +6 −0 test/test_http_request.rb
View
14 lib/http_request.rb
@@ -78,10 +78,10 @@ def data(response, &block)
end
# send request by some given parameters
- def request(method, opt, &block)
+ def request(method, options, &block)
# parse the @options
- parse_options(method, opt)
+ parse_options(method, options)
# parse and merge for the options[:parameters]
parse_parameters
@@ -96,11 +96,13 @@ def request(method, opt, &block)
end
# catch all available http requests
- def self.method_missing(method_name, args, &block)
+ def self.method_missing(method_name, options, &block)
+ # we need to retrieve the cookies from last http response before reset cookies if it's a Net::HTTPResponse
+ options[:cookies] = options[:cookies].cookies if options[:cookies].is_a? Net::HTTPResponse
@@__cookies = {}
method_name = method_name.to_s.downcase
raise NoHttpMethodException, "No such http method can be called: #{method_name}" unless self.http_methods.include?(method_name)
- self.instance.request(method_name, args, &block)
+ self.instance.request(method_name, options, &block)
end
# for ftp, no plan to add new features to this method except bug fixing
@@ -250,7 +252,7 @@ def parse_options(method, options)
# support gzip
begin; require 'zlib'; rescue LoadError; end
- @headers['Accept-Encoding'] = 'gzip,deflate' if defined? Zlib
+ @headers['Accept-Encoding'] = 'gzip,deflate' if defined? ::Zlib
# ajax calls?
@headers['X_REQUESTED_WITH'] = 'XMLHttpRequest' if @options[:ajax] or @options[:xhr]
@@ -428,7 +430,7 @@ def cookies
def body
bd = read_body()
return bd unless bd
- if (self['content-encoding'] == 'gzip') and defined?(Zlib)
+ if (self['content-encoding'] == 'gzip') and defined?(::Zlib)
::Zlib::GzipReader.new(StringIO.new(bd)).read
else
bd
View
6 test/test_http_request.rb
@@ -223,6 +223,12 @@
h1 = hr.get(:url => URL + "/session", :cookies => h1.cookies)
h1.body.should.equal "2"
+ h2 = hr.get(URL + "/session")
+ h2.body.should.equal "1"
+
+ h2 = hr.get(:url => URL + "/session", :cookies => h2)
+ h2.body.should.equal "2"
+
h = hr.get(URL + "/session/1")
h.body.should.equal "/session/1:/session/2"

0 comments on commit bc23cf2

Please sign in to comment.