1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

Bugfix for issue #15:correctly takes into account user headers whose keys are strings. Better generation of logs. Updated tests.

This commit is contained in:
Cyril Rohr 2010-03-28 10:58:21 +02:00 committed by Julien Kirch
parent 6079fb070d
commit 4da24e9c7c
3 changed files with 41 additions and 40 deletions

View file

@ -2,6 +2,7 @@
- added Response.to_str and AbstractResponse.to_i to improve semantic and compatibility
- multipart Payloads ignores the name attribute if it's not set (patch provided by Tekin Suleyman)
- correctly takes into account user headers whose keys are strings (path provided by Cyril Rohr)
# 1.4.2

View file

@ -59,26 +59,7 @@ module RestClient
unless @cookies.empty?
user_headers[:cookie] = @cookies.map {|(key, val)| "#{key.to_s}=#{val}" }.sort.join(",")
end
headers = default_headers.merge(user_headers).inject({}) do |final, (key, value)|
target_key = key.to_s.gsub(/_/, '-').capitalize
if 'CONTENT-TYPE' == target_key.upcase
target_value = value.to_s
final[target_key] = MIME::Types.type_for_extension target_value
elsif 'ACCEPT' == target_key.upcase
# Accept can be composed of several comma-separated values
if value.is_a? Array
target_values = value
else
target_values = value.to_s.split ','
end
final[target_key] = target_values.map{ |ext| MIME::Types.type_for_extension(ext.to_s.strip)}.join(', ')
else
final[target_key] = value.to_s
end
final
end
headers = stringify_headers(default_headers).merge(stringify_headers(user_headers))
headers.merge!(@payload.headers) if @payload
headers
end
@ -223,7 +204,7 @@ module RestClient
out = []
out << "RestClient.#{method} #{url.inspect}"
out << payload.short_inspect if payload
out << processed_headers.inspect.gsub(/^\{/, '').gsub(/\}$/, '')
out << processed_headers.to_a.sort.map{|(k,v)| [k.inspect, v.inspect].join("=>")}.join(", ")
RestClient.log << out.join(', ') + "\n"
end
end
@ -235,9 +216,32 @@ module RestClient
end
end
# Return a hash of headers whose keys are capitalized strings
def stringify_headers headers
headers.inject({}) do |result, (key, value)|
target_key = key.to_s.split(/_/).map{|w| w.capitalize}.join('-')
if 'CONTENT-TYPE' == target_key.upcase
target_value = value.to_s
result[target_key] = MIME::Types.type_for_extension target_value
elsif 'ACCEPT' == target_key.upcase
# Accept can be composed of several comma-separated values
if value.is_a? Array
target_values = value
else
target_values = value.to_s.split ','
end
result[target_key] = target_values.map{ |ext| MIME::Types.type_for_extension(ext.to_s.strip)}.join(', ')
else
result[target_key] = value.to_s
end
result
end
end
def default_headers
{ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' }
end
end
end

View file

@ -108,12 +108,12 @@ describe RestClient::Request do
describe "user headers" do
it "merges user headers with the default headers" do
@request.should_receive(:default_headers).and_return({ '1' => '2' })
headers = @request.make_headers('3' => '4')
headers.should have_key('1')
headers['1'].should == '2'
headers.should have_key('3')
headers['3'].should == '4'
@request.should_receive(:default_headers).and_return({ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' })
headers = @request.make_headers("Accept" => "application/json", :accept_encoding => 'gzip')
headers.should have_key "Accept-Encoding"
headers["Accept-Encoding"].should == "gzip"
headers.should have_key "Accept"
headers["Accept"].should == "application/json"
end
it "prefers the user header when the same header exists in the defaults" do
@ -126,18 +126,18 @@ describe RestClient::Request do
describe "header symbols" do
it "converts header symbols from :content_type to 'Content-type'" do
it "converts header symbols from :content_type to 'Content-Type'" do
@request.should_receive(:default_headers).and_return({})
headers = @request.make_headers(:content_type => 'abc')
headers.should have_key('Content-type')
headers['Content-type'].should == 'abc'
headers.should have_key('Content-Type')
headers['Content-Type'].should == 'abc'
end
it "converts content-type from extension to real content-type" do
@request.should_receive(:default_headers).and_return({})
headers = @request.make_headers(:content_type => 'json')
headers.should have_key('Content-type')
headers['Content-type'].should == 'application/json'
headers.should have_key('Content-Type')
headers['Content-Type'].should == 'application/json'
end
it "converts accept from extension(s) to real content-type(s)" do
@ -282,29 +282,25 @@ describe RestClient::Request do
it "logs a get request" do
log = RestClient.log = []
RestClient::Request.new(:method => :get, :url => 'http://url').log_request
['RestClient.get "http://url", "Accept-encoding"=>"gzip, deflate", "Accept"=>"*/*; q=0.5, application/xml"' + "\n",
'RestClient.get "http://url", "Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate"' + "\n"].should include(log[0])
log[0].should == %Q{RestClient.get "http://url", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate"\n}
end
it "logs a post request with a small payload" do
log = RestClient.log = []
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log_request
['RestClient.post "http://url", "foo", "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"3", "Accept"=>"*/*; q=0.5, application/xml"' + "\n",
'RestClient.post "http://url", "foo", "Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"3"' + "\n"].should include(log[0])
log[0].should == %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3"\n}
end
it "logs a post request with a large payload" do
log = RestClient.log = []
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log_request
['RestClient.post "http://url", 1000 byte(s) length, "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"1000", "Accept"=>"*/*; q=0.5, application/xml"' + "\n",
'RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*; q=0.5, application/xml", "Accept-encoding"=>"gzip, deflate", "Content-Length"=>"1000"' + "\n"].should include(log[0])
log[0].should == %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000"\n}
end
it "logs input headers as a hash" do
log = RestClient.log = []
RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).log_request
['RestClient.get "http://url", "Accept-encoding"=>"gzip, deflate", "Accept"=>"text/plain"' + "\n",
'RestClient.get "http://url", "Accept"=>"text/plain", "Accept-encoding"=>"gzip, deflate"' + "\n"].should include(log[0])
log[0].should == %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate"\n}
end
it "logs a response including the status code, content type, and result body size in bytes" do