Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow setting headers_hash directly.

  • Loading branch information...
commit b3b74106e5a15d8dc2d025421d5a2bb6bb7c40b5 1 parent 2c9ed05
@dbalatero dbalatero authored
Showing with 22 additions and 13 deletions.
  1. +17 −13 lib/typhoeus/response.rb
  2. +5 −0 spec/typhoeus/response_spec.rb
View
30 lib/typhoeus/response.rb
@@ -5,6 +5,7 @@ class Response
:requested_url, :requested_remote_method,
:requested_http_method, :start_time,
:effective_url
+ attr_writer :headers_hash
def initialize(params = {})
@code = params[:code]
@@ -17,6 +18,7 @@ def initialize(params = {})
@request = params[:request]
@effective_url = params[:effective_url]
@mock = params[:mock] || false # default
+ @headers_hash = params[:headers_hash]
end
# Returns true if this is a mock response.
@@ -25,21 +27,23 @@ def mock?
end
def headers_hash
- headers.split("\n").map {|o| o.strip}.inject(Typhoeus::LowercaseHash.new) do |hash, o|
- if o.empty?
- hash
- else
- i = o.index(":") || o.size
- key = o.slice(0, i)
- value = o.slice(i + 1, o.size)
- value = value.strip unless value.nil?
- if hash.has_key? key
- hash[key] = [hash[key], value].flatten
+ @headers_hash ||= begin
+ headers.split("\n").map {|o| o.strip}.inject(Typhoeus::LowercaseHash.new) do |hash, o|
+ if o.empty?
+ hash
else
- hash[key] = value
- end
+ i = o.index(":") || o.size
+ key = o.slice(0, i)
+ value = o.slice(i + 1, o.size)
+ value = value.strip unless value.nil?
+ if hash.has_key? key
+ hash[key] = [hash[key], value].flatten
+ else
+ hash[key] = value
+ end
- hash
+ hash
+ end
end
end
end
View
5 spec/typhoeus/response_spec.rb
@@ -2,6 +2,11 @@
describe Typhoeus::Response do
describe "initialize" do
+ it "should store headers_hash" do
+ response = Typhoeus::Response.new(:headers_hash => {})
+ response.headers_hash.should == {}
+ end
+
it "should store response_code" do
Typhoeus::Response.new(:code => 200).code.should == 200
end
Please sign in to comment.
Something went wrong with that request. Please try again.