summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/provider/http_request.rb65
-rw-r--r--lib/chef/resource/http_request.rb3
-rw-r--r--spec/unit/provider/http_request_spec.rb97
3 files changed, 54 insertions, 111 deletions
diff --git a/lib/chef/provider/http_request.rb b/lib/chef/provider/http_request.rb
index 0ea5f8289f..184e17e409 100644
--- a/lib/chef/provider/http_request.rb
+++ b/lib/chef/provider/http_request.rb
@@ -6,9 +6,9 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -36,13 +36,9 @@ class Chef
def action_head
message = check_message(@new_resource.message)
# returns true from Chef::REST if returns 2XX (Net::HTTPSuccess)
- modified = @rest.run_request(
- :HEAD,
- @rest.create_url("#{@new_resource.url}?message=#{message}"),
+ modified = @rest.head(
+ "#{@new_resource.url}?message=#{message}",
@new_resource.headers,
- false,
- 10,
- false
)
Chef::Log.info("#{@new_resource} HEAD to #{@new_resource.url} successful")
Chef::Log.debug("#{@new_resource} HEAD request response: #{modified}")
@@ -53,76 +49,63 @@ class Chef
end
# Send a GET request to @new_resource.url, with ?message=@new_resource.message
- def action_get
+ def action_get
converge_by("#{@new_resource} GET to #{@new_resource.url}") do
message = check_message(@new_resource.message)
- body = @rest.run_request(
- :GET,
- @rest.create_url("#{@new_resource.url}?message=#{message}"),
- @new_resource.headers,
+ body = @rest.get(
+ "#{@new_resource.url}?message=#{message}",
false,
- 10,
- false
+ @new_resource.headers
)
Chef::Log.info("#{@new_resource} GET to #{@new_resource.url} successful")
Chef::Log.debug("#{@new_resource} GET request response: #{body}")
end
end
-
+
# Send a PUT request to @new_resource.url, with the message as the payload
- def action_put
+ def action_put
converge_by("#{@new_resource} PUT to #{@new_resource.url}") do
message = check_message(@new_resource.message)
- body = @rest.run_request(
- :PUT,
- @rest.create_url("#{@new_resource.url}"),
- @new_resource.headers,
+ body = @rest.put(
+ "#{@new_resource.url}",
message,
- 10,
- false
+ @new_resource.headers
)
Chef::Log.info("#{@new_resource} PUT to #{@new_resource.url} successful")
Chef::Log.debug("#{@new_resource} PUT request response: #{body}")
end
end
-
+
# Send a POST request to @new_resource.url, with the message as the payload
def action_post
converge_by("#{@new_resource} POST to #{@new_resource.url}") do
message = check_message(@new_resource.message)
- body = @rest.run_request(
- :POST,
- @rest.create_url("#{@new_resource.url}"),
- @new_resource.headers,
+ body = @rest.post(
+ "#{@new_resource.url}",
message,
- 10,
- false
+ @new_resource.headers
)
Chef::Log.info("#{@new_resource} POST to #{@new_resource.url} message: #{message.inspect} successful")
Chef::Log.debug("#{@new_resource} POST request response: #{body}")
end
end
-
+
# Send a DELETE request to @new_resource.url
def action_delete
converge_by("#{@new_resource} DELETE to #{@new_resource.url}") do
- body = @rest.run_request(
- :DELETE,
- @rest.create_url("#{@new_resource.url}"),
- @new_resource.headers,
- false,
- 10,
- false
+ body = @rest.delete(
+ "#{@new_resource.url}",
+ @new_resource.headers
)
@new_resource.updated_by_last_action(true)
Chef::Log.info("#{@new_resource} DELETE to #{@new_resource.url} successful")
Chef::Log.debug("#{@new_resource} DELETE request response: #{body}")
end
end
-
+
private
-
+
def check_message(message)
if message.kind_of?(Proc)
message.call
@@ -130,7 +113,7 @@ class Chef
message
end
end
-
+
end
end
end
diff --git a/lib/chef/resource/http_request.rb b/lib/chef/resource/http_request.rb
index d31ff0b1c9..fc64121f4e 100644
--- a/lib/chef/resource/http_request.rb
+++ b/lib/chef/resource/http_request.rb
@@ -43,7 +43,8 @@ class Chef
)
end
- def message(args=nil)
+ def message(args=nil, &block)
+ args = block if block_given?
set_or_return(
:message,
args,
diff --git a/spec/unit/provider/http_request_spec.rb b/spec/unit/provider/http_request_spec.rb
index 26d73cebb4..436eaec558 100644
--- a/spec/unit/provider/http_request_spec.rb
+++ b/spec/unit/provider/http_request_spec.rb
@@ -6,9 +6,9 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,13 +26,13 @@ describe Chef::Provider::HttpRequest do
@new_resource = Chef::Resource::HttpRequest.new('adam')
@new_resource.name "adam"
- @new_resource.url "http://www.opscode.com"
+ @new_resource.url "http://www.opscode.com/"
@new_resource.message "is cool"
@provider = Chef::Provider::HttpRequest.new(@new_resource, @run_context)
end
- describe "load_current_resource" do
+ describe "load_current_resource" do
it "should set up a Chef::REST client, with no authentication" do
Chef::REST.should_receive(:new).with(@new_resource.url, nil, nil)
@@ -45,91 +45,59 @@ describe Chef::Provider::HttpRequest do
# run_action(x) forces load_current_resource to run;
# that would overwrite our supplied mock Chef::Rest # object
@provider.stub!(:load_current_resource).and_return(true)
- @rest = mock("Chef::REST", :create_url => "http://www.opscode.com", :run_request => "you made it!" )
+ @rest = mock("Chef::REST")
@provider.rest = @rest
end
- describe "action_get" do
- it "should create the url with a message argument" do
- @rest.should_receive(:create_url).with("#{@new_resource.url}?message=#{@new_resource.message}")
- @provider.run_action(:get)
- end
+ describe "action_get" do
it "should inflate a message block at runtime" do
- @new_resource.stub!(:message).and_return(lambda { "return" })
- @rest.should_receive(:create_url).with("#{@new_resource.url}?message=return")
+ @new_resource.message { "return" }
+ @rest.should_receive(:get).with("http://www.opscode.com/?message=return", false, {})
@provider.run_action(:get)
+ @new_resource.should be_updated
end
it "should run a GET request" do
- @rest.should_receive(:run_request).with(:GET, @rest.create_url, {}, false, 10, false)
- @provider.run_action(:get)
- end
-
- it "should update the resource" do
+ @rest.should_receive(:get).with("http://www.opscode.com/?message=is cool", false, {})
@provider.run_action(:get)
@new_resource.should be_updated
end
end
- describe "action_put" do
- it "should create the url" do
- @rest.should_receive(:create_url).with("#{@new_resource.url}")
- @provider.run_action(:put)
- end
-
+ describe "action_put" do
it "should run a PUT request with the message as the payload" do
- @rest.should_receive(:run_request).with(:PUT, @rest.create_url, {}, @new_resource.message, 10, false)
+ @rest.should_receive(:put).with("http://www.opscode.com/", @new_resource.message, {})
@provider.run_action(:put)
+ @new_resource.should be_updated
end
it "should inflate a message block at runtime" do
@new_resource.stub!(:message).and_return(lambda { "return" })
- @rest.should_receive(:run_request).with(:PUT, @rest.create_url, {}, "return", 10, false)
- @provider.run_action(:put)
- end
-
- it "should update the resource" do
+ @rest.should_receive(:put).with("http://www.opscode.com/", "return", {})
@provider.run_action(:put)
@new_resource.should be_updated
end
end
- describe "action_post" do
- it "should create the url" do
- @rest.should_receive(:create_url).with("#{@new_resource.url}")
- @provider.run_action(:post)
- end
-
+ describe "action_post" do
it "should run a PUT request with the message as the payload" do
- @rest.should_receive(:run_request).with(:POST, @rest.create_url, {}, @new_resource.message, 10, false)
+ @rest.should_receive(:post).with("http://www.opscode.com/", @new_resource.message, {})
@provider.run_action(:post)
+ @new_resource.should be_updated
end
-
+
it "should inflate a message block at runtime" do
- @new_resource.stub!(:message).and_return(lambda { "return" })
- @rest.should_receive(:run_request).with(:POST, @rest.create_url, {}, "return", 10, false)
- @provider.run_action(:post)
- end
-
- it "should update the resource" do
+ @new_resource.message { "return" }
+ @rest.should_receive(:post).with("http://www.opscode.com/", "return", {})
@provider.run_action(:post)
@new_resource.should be_updated
end
end
- describe "action_delete" do
- it "should create the url" do
- @rest.should_receive(:create_url).with("#{@new_resource.url}")
- @provider.run_action(:delete)
- end
-
+ describe "action_delete" do
it "should run a DELETE request" do
- @rest.should_receive(:run_request).with(:DELETE, @rest.create_url, {}, false, 10, false)
- @provider.run_action(:delete)
- end
-
- it "should update the resource" do
+ @rest.should_receive(:delete).with("http://www.opscode.com/", {})
@provider.run_action(:delete)
@new_resource.should be_updated
end
@@ -137,39 +105,30 @@ describe Chef::Provider::HttpRequest do
describe "action_head" do
before do
- @rest = mock("Chef::REST", :create_url => "http://www.opscode.com", :run_request => true)
@provider.rest = @rest
end
- it "should create the url with a message argument" do
- @rest.should_receive(:create_url).with("#{@new_resource.url}?message=#{@new_resource.message}")
- @provider.run_action(:head)
- end
-
it "should inflate a message block at runtime" do
- @new_resource.stub!(:message).and_return(lambda { "return" })
- @rest.should_receive(:create_url).with("#{@new_resource.url}?message=return")
+ @new_resource.message { "return" }
+ @rest.should_receive(:head).with("http://www.opscode.com/?message=return", {}).and_return("")
@provider.run_action(:head)
+ @new_resource.should be_updated
end
it "should run a HEAD request" do
- @rest.should_receive(:run_request).with(:HEAD, @rest.create_url, {}, false, 10, false)
- @provider.run_action(:head)
- end
-
- it "should update the resource" do
+ @rest.should_receive(:head).with("http://www.opscode.com/?message=is cool", {}).and_return("")
@provider.run_action(:head)
@new_resource.should be_updated
end
it "should run a HEAD request with If-Modified-Since header" do
@new_resource.headers "If-Modified-Since" => File.mtime(__FILE__).httpdate
- @rest.should_receive(:run_request).with(:HEAD, @rest.create_url, @new_resource.headers, false, 10, false)
+ @rest.should_receive(:head).with("http://www.opscode.com/?message=is cool", @new_resource.headers)
@provider.run_action(:head)
end
it "doesn't call converge_by if HEAD does not return modified" do
- @rest.should_receive(:run_request).and_return(false)
+ @rest.should_receive(:head).and_return(false)
@provider.should_not_receive(:converge_by)
@provider.run_action(:head)
end