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

Merge pull request #324 from rest-client/user-agent

Add a more descriptive rest-client user agent.
This commit is contained in:
Andy Brody 2014-11-25 18:03:28 -05:00
commit 9c3c296c1b
3 changed files with 32 additions and 9 deletions

View file

@ -1,3 +1,5 @@
require 'rbconfig'
module RestClient module RestClient
module Platform module Platform
# Return true if we are running on a darwin-based Ruby platform. This will # Return true if we are running on a darwin-based Ruby platform. This will
@ -26,5 +28,22 @@ module RestClient
# defined on mri >= 1.9 # defined on mri >= 1.9
RUBY_ENGINE == 'jruby' RUBY_ENGINE == 'jruby'
end end
def self.architecture
"#{RbConfig::CONFIG['host_os']} #{RbConfig::CONFIG['host_cpu']}"
end
def self.ruby_agent_version
case RUBY_ENGINE
when 'jruby'
"jruby/#{JRUBY_VERSION} (#{RUBY_VERSION}p#{RUBY_PATCHLEVEL})"
else
"#{RUBY_ENGINE}/#{RUBY_VERSION}p#{RUBY_PATCHLEVEL}"
end
end
def self.default_user_agent
"rest-client/#{VERSION} (#{architecture}) #{ruby_agent_version}"
end
end end
end end

View file

@ -562,7 +562,11 @@ module RestClient
end end
def default_headers def default_headers
{:accept => '*/*', :accept_encoding => 'gzip, deflate'} {
:accept => '*/*',
:accept_encoding => 'gzip, deflate',
:user_agent => RestClient::Platform.default_user_agent,
}
end end
private private

View file

@ -369,26 +369,26 @@ describe RestClient::Request do
describe "logging" do describe "logging" do
it "logs a get request" do it "logs a get request" do
log = RestClient.log = [] log = RestClient.log = []
RestClient::Request.new(:method => :get, :url => 'http://url').log_request RestClient::Request.new(:method => :get, :url => 'http://url', :headers => {:user_agent => 'rest-client'}).log_request
log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate"\n} log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n}
end end
it "logs a post request with a small payload" do it "logs a post request with a small payload" do
log = RestClient.log = [] log = RestClient.log = []
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log_request RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo', :headers => {:user_agent => 'rest-client'}).log_request
log[0].should eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3"\n} log[0].should eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3", "User-Agent"=>"rest-client"\n}
end end
it "logs a post request with a large payload" do it "logs a post request with a large payload" do
log = RestClient.log = [] log = RestClient.log = []
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log_request RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000), :headers => {:user_agent => 'rest-client'}).log_request
log[0].should eq %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000"\n} log[0].should eq %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000", "User-Agent"=>"rest-client"\n}
end end
it "logs input headers as a hash" do it "logs input headers as a hash" do
log = RestClient.log = [] log = RestClient.log = []
RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).log_request RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain', :user_agent => 'rest-client' }).log_request
log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate"\n} log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n}
end end
it "logs a response including the status code, content type, and result body size in bytes" do it "logs a response including the status code, content type, and result body size in bytes" do