Browse files

beautify headers (:content_type => 'text/html')

  • Loading branch information...
1 parent 7d0161d commit 92a25a7a10c550403b40c0a07e70693789cba242 @adamwiggins committed Jan 24, 2009
Showing with 20 additions and 3 deletions.
  1. +8 −1 lib/rest_client.rb
  2. +12 −2 spec/rest_client_spec.rb
View
9 lib/rest_client.rb
@@ -270,7 +270,14 @@ def code
end
def headers
- @headers ||= @net_http_res.to_hash
+ @headers ||= self.class.beautify_headers(@net_http_res.to_hash)
+ end
+
+ def self.beautify_headers(headers)
+ headers.inject({}) do |out, (key, value)|
+ out[key.gsub(/-/, '_').to_sym] = value.first
+ out
+ end
end
end
end
View
14 spec/rest_client_spec.rb
@@ -356,9 +356,19 @@
@response.code.should == 200
end
+ it "beautifies the headers by turning the keys to symbols" do
+ h = RestClient::Response.beautify_headers('content-type' => [ 'x' ])
+ h.keys.first.should == :content_type
+ end
+
+ it "beautifies the headers by turning the values to strings instead of one-element arrays" do
+ h = RestClient::Response.beautify_headers('x' => [ 'text/html' ] )
+ h.values.first.should == 'text/html'
+ end
+
it "fetches the headers" do
- @net_http_res.should_receive(:to_hash).and_return('a' => ['b'])
- @response.headers['a'].should == ['b']
+ @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
+ @response.headers.should == { :content_type => 'text/html' }
end
it "can access the net http result directly" do

0 comments on commit 92a25a7

Please sign in to comment.