diff options
author | danielsdeleo <dan@opscode.com> | 2013-10-08 17:43:04 -0700 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2013-10-08 17:43:04 -0700 |
commit | 61dd5990429afa67800607533b9c306d5633e5b8 (patch) | |
tree | b10f2d88a5bdaba0d8de1bdbdd4584e5aa3fc31e | |
parent | 2479acaaa3485d494c5f27765f87ff5b37550567 (diff) | |
download | chef-61dd5990429afa67800607533b9c306d5633e5b8.tar.gz |
Fix Chef::HTTP dependence on subclass methods
-rw-r--r-- | lib/chef/http.rb | 22 | ||||
-rw-r--r-- | lib/chef/http/cookie_manager.rb | 8 | ||||
-rw-r--r-- | lib/chef/rest.rb | 2 | ||||
-rw-r--r-- | spec/unit/rest_spec.rb | 46 |
4 files changed, 39 insertions, 39 deletions
diff --git a/lib/chef/http.rb b/lib/chef/http.rb index 77faa686a0..50e0509b91 100644 --- a/lib/chef/http.rb +++ b/lib/chef/http.rb @@ -94,7 +94,7 @@ class Chef # === Parameters # path:: path part of the request URL def head(path, headers={}) - api_request(:HEAD, create_url(path), headers) + request(:HEAD, create_url(path), headers) end # Send an HTTP GET request to the path @@ -102,7 +102,7 @@ class Chef # === Parameters # path:: The path to GET def get(path, headers={}) - api_request(:GET, create_url(path), headers) + request(:GET, create_url(path), headers) end # Send an HTTP PUT request to the path @@ -110,7 +110,7 @@ class Chef # === Parameters # path:: path part of the request URL def put(path, json, headers={}) - api_request(:PUT, create_url(path), headers, json) + request(:PUT, create_url(path), headers, json) end # Send an HTTP POST request to the path @@ -118,7 +118,7 @@ class Chef # === Parameters # path:: path part of the request URL def post(path, json, headers={}) - api_request(:POST, create_url(path), headers, json) + request(:POST, create_url(path), headers, json) end # Send an HTTP DELETE request to the path @@ -126,13 +126,12 @@ class Chef # === Parameters # path:: path part of the request URL def delete(path, headers={}) - api_request(:DELETE, create_url(path), headers) + request(:DELETE, create_url(path), headers) end # Makes an HTTP request to +url+ with the given +method+, +headers+, and # +data+ (if applicable). def request(method, url, headers={}, data=false) - method, url, headers, data = apply_request_middleware(method, url, headers, data) response, rest_request, return_value = send_http_request(method, url, headers, data) @@ -185,8 +184,9 @@ class Chef raise end - def http_client - BasicClient.new(create_url(url)) + def http_client(base_url=nil) + base_url ||= url + BasicClient.new(base_url) end protected @@ -228,10 +228,11 @@ class Chef headers = build_headers(method, url, headers, body) retrying_http_errors(url) do + client = http_client(url) if block_given? - request, response = http_client.request(method, url, body, headers, &response_handler) + request, response = client.request(method, url, body, headers, &response_handler) else - request, response = http_client.request(method, url, body, headers) {|r| r.read_body } + request, response = client.request(method, url, body, headers) {|r| r.read_body } end @last_response = response @@ -320,7 +321,6 @@ class Chef yield ensure @redirects_followed = 0 - @authenticator.sign_request = true end private diff --git a/lib/chef/http/cookie_manager.rb b/lib/chef/http/cookie_manager.rb index 067dc3ed22..f6dcf9aa32 100644 --- a/lib/chef/http/cookie_manager.rb +++ b/lib/chef/http/cookie_manager.rb @@ -32,16 +32,16 @@ class Chef end def handle_request(method, url, headers={}, data=false) - host, port = url.host, url.port - if @cookies.has_key?("#{host}:#{port}") - headers['Cookie'] = @cookies["#{host}:#{port}"] + @host, @port = url.host, url.port + if @cookies.has_key?("#{@host}:#{@port}") + headers['Cookie'] = @cookies["#{@host}:#{@port}"] end [method, url, headers, data] end def handle_response(http_response, rest_request, return_value) if http_response['set-cookie'] - @cookies["#{host}:#{port}"] = http_response['set-cookie'] + @cookies["#{@host}:#{@port}"] = http_response['set-cookie'] end [http_response, rest_request, return_value] end diff --git a/lib/chef/rest.rb b/lib/chef/rest.rb index b264f96336..d27a139104 100644 --- a/lib/chef/rest.rb +++ b/lib/chef/rest.rb @@ -96,7 +96,7 @@ class Chef if raw streaming_request(create_url(path), headers) else - api_request(:GET, create_url(path), headers) + request(:GET, create_url(path), headers) end end diff --git a/spec/unit/rest_spec.rb b/spec/unit/rest_spec.rb index 073ff691d6..9352afad11 100644 --- a/spec/unit/rest_spec.rb +++ b/spec/unit/rest_spec.rb @@ -76,7 +76,7 @@ describe Chef::REST do end it "makes a :GET request with the composed url object" do - @rest.should_receive(:api_request).with(:GET, @monkey_uri, {}) + @rest.should_receive(:request).with(:GET, @monkey_uri, {}) @rest.get_rest("monkey") end @@ -86,17 +86,17 @@ describe Chef::REST do end it "makes a :DELETE request with the composed url object" do - @rest.should_receive(:api_request).with(:DELETE, @monkey_uri, {}) + @rest.should_receive(:request).with(:DELETE, @monkey_uri, {}) @rest.delete_rest("monkey") end it "makes a :POST request with the composed url object and data" do - @rest.should_receive(:api_request).with(:POST, @monkey_uri, {}, "data") + @rest.should_receive(:request).with(:POST, @monkey_uri, {}, "data") @rest.post_rest("monkey", "data") end it "makes a :PUT request with the composed url object and data" do - @rest.should_receive(:api_request).with(:PUT, @monkey_uri, {}, "data") + @rest.should_receive(:request).with(:PUT, @monkey_uri, {}, "data") @rest.put_rest("monkey", "data") end end @@ -235,13 +235,13 @@ describe Chef::REST do it "should always include the X-Chef-Version header" do Net::HTTP::Get.should_receive(:new).with("/?foo=bar", @base_headers).and_return(@request_mock) - @rest.api_request(:GET, @url, {}) + @rest.request(:GET, @url, {}) end it "sets the user agent to chef-client" do # must reset to default b/c knife changes the UA Chef::REST::RESTRequest.user_agent = Chef::REST::RESTRequest::DEFAULT_UA - @rest.api_request(:GET, @url, {}) + @rest.request(:GET, @url, {}) @request_mock['User-Agent'].should match(/^Chef Client\/#{Chef::VERSION}/) end @@ -260,7 +260,7 @@ describe Chef::REST do request = Net::HTTP::Get.new(@url.path) Net::HTTP::Get.should_receive(:new).and_return(request) # will raise a Zlib error if incorrect - @rest.api_request(:GET, @url, {}).should == "ninja" + @rest.request(:GET, @url, {}).should == "ninja" end end context "when configured with custom http headers" do @@ -280,19 +280,19 @@ describe Chef::REST do url_string = an_instance_of(String) header_hash = hash_including(@custom_headers) Net::HTTP::Get.should_receive(:new).with(url_string, header_hash) - @rest.api_request(:GET, @url, {}) + @rest.request(:GET, @url, {}) end 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", @base_headers.merge('Cookie' => "cookie monster")).and_return(@request_mock) - @rest.api_request(:GET, @url, {}) + @rest.request(:GET, @url, {}) end it "should build a new HTTP GET request" do Net::HTTP::Get.should_receive(:new).with("/?foo=bar", @base_headers).and_return(@request_mock) - @rest.api_request(:GET, @url, {}) + @rest.request(:GET, @url, {}) end it "should build a new HTTP POST request" do @@ -300,7 +300,7 @@ describe Chef::REST do expected_headers = @base_headers.merge("Content-Type" => 'application/json', 'Content-Length' => '13') Net::HTTP::Post.should_receive(:new).with("/?foo=bar", expected_headers).and_return(request) - @rest.api_request(:POST, @url, {}, {:one=>:two}) + @rest.request(:POST, @url, {}, {:one=>:two}) request.body.should == '{"one":"two"}' end @@ -308,31 +308,31 @@ describe Chef::REST do request = Net::HTTP::Put.new(@url.path) expected_headers = @base_headers.merge("Content-Type" => 'application/json', 'Content-Length' => '13') Net::HTTP::Put.should_receive(:new).with("/?foo=bar",expected_headers).and_return(request) - @rest.api_request(:PUT, @url, {}, {:one=>:two}) + @rest.request(:PUT, @url, {}, {:one=>:two}) request.body.should == '{"one":"two"}' end it "should build a new HTTP DELETE request" do Net::HTTP::Delete.should_receive(:new).with("/?foo=bar", @base_headers).and_return(@request_mock) - @rest.api_request(:DELETE, @url) + @rest.request(:DELETE, @url) end it "should raise an error if the method is not GET/PUT/POST/DELETE" do - lambda { @rest.api_request(:MONKEY, @url) }.should raise_error(ArgumentError) + lambda { @rest.request(:MONKEY, @url) }.should raise_error(ArgumentError) end it "returns nil when the response is successful but content-type is not JSON" do - @rest.api_request(:GET, @url).should == "ninja" + @rest.request(:GET, @url).should == "ninja" end it "should inflate the body as to an object if JSON is returned" do @http_response.add_field('content-type', "application/json") @http_response.stub(:body).and_return('{"ohai2u":"json_api"}') - @rest.api_request(:GET, @url, {}).should == {"ohai2u"=>"json_api"} + @rest.request(:GET, @url, {}).should == {"ohai2u"=>"json_api"} end %w[ HTTPFound HTTPMovedPermanently HTTPSeeOther HTTPUseProxy HTTPTemporaryRedirect HTTPMultipleChoice ].each do |resp_name| - it "should call api_request again on a #{resp_name} response" do + it "should call request again on a #{resp_name} response" do resp_cls = Net.const_get(resp_name) resp_code = Net::HTTPResponse::CODE_TO_OBJ.keys.detect { |k| Net::HTTPResponse::CODE_TO_OBJ[k] == resp_cls } http_response = Net::HTTPFound.new("1.1", resp_code, "bob is somewhere else again") @@ -341,10 +341,10 @@ describe Chef::REST do @http_client.stub(:request).and_yield(http_response).and_return(http_response) - lambda { @rest.api_request(:GET, @url) }.should raise_error(Chef::Exceptions::RedirectLimitExceeded) + lambda { @rest.request(:GET, @url) }.should raise_error(Chef::Exceptions::RedirectLimitExceeded) [:PUT, :POST, :DELETE].each do |method| - lambda { @rest.api_request(method, @url) }.should raise_error(Chef::Exceptions::InvalidRedirect) + lambda { @rest.request(method, @url) }.should raise_error(Chef::Exceptions::InvalidRedirect) end end end @@ -355,7 +355,7 @@ describe Chef::REST do @http_client.stub(:request).and_yield(http_response).and_return(http_response) - @rest.api_request(:GET, @url).should be_false + @rest.request(:GET, @url).should be_false end describe "when the request fails" do @@ -376,7 +376,7 @@ describe Chef::REST do @rest.stub(:sleep) @http_client.stub(:request).and_yield(http_response).and_return(http_response) - lambda {@rest.api_request(:GET, @url)}.should raise_error(Net::HTTPFatalError) + lambda {@rest.request(:GET, @url)}.should raise_error(Net::HTTPFatalError) @log_stringio.string.should match(Regexp.escape('INFO: HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four')) end @@ -394,7 +394,7 @@ describe Chef::REST do @rest.stub(:http_retry_count).and_return(0) @http_client.stub(:request).and_yield(http_response).and_return(http_response) - lambda {@rest.api_request(:GET, @url)}.should raise_error(Net::HTTPFatalError) + lambda {@rest.request(:GET, @url)}.should raise_error(Net::HTTPFatalError) @log_stringio.string.should match(Regexp.escape('INFO: HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four')) end @@ -404,7 +404,7 @@ describe Chef::REST do http_response.stub(:read_body) @rest.stub(:sleep) @http_client.stub(:request).and_yield(http_response).and_return(http_response) - lambda {@rest.api_request(:GET, @url)}.should raise_error(Net::HTTPFatalError) + lambda {@rest.request(:GET, @url)}.should raise_error(Net::HTTPFatalError) end end |