Commit 629a1b42 authored by Daniel DeLeo's avatar Daniel DeLeo
Browse files

[CHEF-2159] Set a custom user agent for chef

parent 23be489f
......@@ -28,6 +28,17 @@ require 'chef/version'
class Chef
class REST
class RESTRequest
DEFAULT_UA = "chef-client #{::Chef::VERSION}"
def self.user_agent=(ua)
@user_agent = ua
end
def self.user_agent
@user_agent ||= DEFAULT_UA
end
attr_reader :method, :url, :headers, :http_client, :http_request
def initialize(method, url, req_body, base_headers={})
......@@ -181,6 +192,7 @@ class Chef
@http_request.body = request_body if (request_body && @http_request.request_body_permitted?)
# Optionally handle HTTP Basic Authentication
@http_request.basic_auth(url.user, url.password) if url.user
@http_request['User-Agent'] = self.class.user_agent
end
end
......
......@@ -250,6 +250,11 @@ describe Chef::REST do
@tempfile = Tempfile.open("chef-rspec-rest_spec-line-#{__LINE__}--")
Tempfile.stub!(:new).with("chef-rest").and_return(@tempfile)
Tempfile.stub!(:open).and_return(@tempfile)
@request_mock = {}
Net::HTTP::Get.stub!(:new).and_return(@request_mock)
@http_response_mock = mock("Net::HTTP Response mock")
end
after do
......@@ -299,6 +304,11 @@ describe Chef::REST do
end
describe "as JSON API requests" do
before do
@request_mock = {}
Net::HTTP::Get.stub!(:new).and_return(@request_mock)
end
it "should always include the X-Chef-Version header" do
Net::HTTP::Get.should_receive(:new).with("/?foo=bar",
{ 'Accept' => 'application/json', 'X-Chef-Version' => Chef::VERSION }
......@@ -327,6 +337,11 @@ describe Chef::REST do
end
end
it "sets the user agent to chef-client" do
@rest.api_request(:GET, @url, {})
@request_mock['User-Agent'].should == "chef-client #{Chef::VERSION}"
end
it "should set the cookie for this request if one exists for the given host:port" do
Chef::REST::CookieJar.instance["#{@url.host}:#{@url.port}"] = "cookie monster"
Net::HTTP::Get.should_receive(:new).with("/?foo=bar",
......@@ -427,6 +442,9 @@ describe Chef::REST do
before do
@tempfile = Tempfile.open("chef-rspec-rest_spec-line-#{__LINE__}--")
Tempfile.stub!(:new).with("chef-rest").and_return(@tempfile)
@request_mock = {}
Net::HTTP::Get.stub!(:new).and_return(@request_mock)
@http_response = Net::HTTPSuccess.new("1.1",200, "it-works")
@http_response.stub!(:read_body)
@http_client.stub!(:request).and_yield(@http_response).and_return(@http_response)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment