summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjamesc <james@opscode.com>2013-11-20 15:45:18 -0800
committerjamesc <james@opscode.com>2013-11-20 15:45:18 -0800
commit3ef7c26d428253c07f706f257ce005b689c81877 (patch)
tree1da121b2309a9415afa4a445ec6d0d42d40ec3e9
parent31c695b0dad707a172780bc80cb12a31de45a415 (diff)
parent5c7cd1eaa71954156e3e40dc17918a4dae86a4ce (diff)
downloadchef-3ef7c26d428253c07f706f257ce005b689c81877.tar.gz
Merge branch 'jc/OC-10471/raw_http_request'
-rw-r--r--lib/chef/rest.rb21
-rw-r--r--spec/unit/rest_spec.rb45
2 files changed, 56 insertions, 10 deletions
diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb
index d8c2a005d7..04ee0b0cb2 100644
--- a/lib/chef/rest.rb
+++ b/lib/chef/rest.rb
@@ -124,9 +124,24 @@ class Chef
alias :api_request :request
- alias :raw_http_request :send_http_request
-
- public :raw_http_request
+ # Do a HTTP request where no middleware is loaded (e.g. JSON input/output
+ # conversion) but the standard Chef Authentication headers are added to the
+ # request.
+ def raw_http_request(method, path, headers, data)
+ url = create_url(path)
+ method, url, headers, data = @authenticator.handle_request(method, url, headers, data)
+
+ response, rest_request, return_value = send_http_request(method, url, headers, data)
+ response.error! unless success_response?(response)
+ return_value
+ rescue Exception => exception
+ log_failed_request(response, return_value) unless response.nil?
+
+ if exception.respond_to?(:chef_rest_request=)
+ exception.chef_rest_request = rest_request
+ end
+ raise
+ end
# Deprecated:
# Responsibilities of this method have been split up. The #http_client is
diff --git a/spec/unit/rest_spec.rb b/spec/unit/rest_spec.rb
index 4cf58767de..0281bd22ec 100644
--- a/spec/unit/rest_spec.rb
+++ b/spec/unit/rest_spec.rb
@@ -65,13 +65,6 @@ describe Chef::REST do
Chef::REST::CookieJar.instance.clear
end
- describe "legacy API" do
- it 'responds to raw_http_request as a public method' do
- @rest.public_methods.map(&:to_s).should include("raw_http_request")
- end
-
- end
-
describe "calling an HTTP verb on a path or absolute URL" do
it "adds a relative URL to the base url it was initialized with" do
@@ -127,6 +120,44 @@ describe Chef::REST do
end
end
+ describe "legacy API" do
+ before(:each) do
+ Chef::Config[:node_name] = "webmonkey.example.com"
+ Chef::Config[:client_key] = CHEF_SPEC_DATA + "/ssl/private_key.pem"
+ @rest = Chef::REST.new(@base_url)
+ end
+
+ it 'responds to raw_http_request as a public method' do
+ @rest.public_methods.map(&:to_s).should include("raw_http_request")
+ end
+
+ it 'calls the authn middleware' do
+ data = "\"secure data\""
+
+ auth_headers = STANDARD_WRITE_HEADERS.merge({"auth_done"=>"yep"})
+
+ @rest.authenticator.should_receive(:handle_request).
+ with(:POST, @monkey_uri, STANDARD_WRITE_HEADERS, data).
+ and_return([:POST, @monkey_uri, auth_headers, data])
+ @rest.should_receive(:send_http_request).
+ with(:POST, @monkey_uri, auth_headers, data).
+ and_return([1,2,3])
+ @rest.should_receive('success_response?'.to_sym).with(1).and_return(true)
+ @rest.raw_http_request(:POST, @monkey_uri, STANDARD_WRITE_HEADERS, data)
+ end
+
+ it 'sets correct authn headers' do
+ data = "\"secure data\""
+ method, uri, auth_headers, d = @rest.authenticator.handle_request(:POST, @monkey_uri, STANDARD_WRITE_HEADERS, data)
+
+ @rest.should_receive(:send_http_request).
+ with(:POST, @monkey_uri, auth_headers, data).
+ and_return([1,2,3])
+ @rest.should_receive('success_response?'.to_sym).with(1).and_return(true)
+ @rest.raw_http_request(:POST, @monkey_uri, STANDARD_WRITE_HEADERS, data)
+ end
+ end
+
describe "when configured to authenticate to the Chef server" do
before do