summaryrefslogtreecommitdiff
path: root/chef/spec/unit/rest_spec.rb
diff options
context:
space:
mode:
authorAdam Jacob <adam@hjksolutions.com>2008-10-21 08:55:42 -0700
committerAdam Jacob <adam@hjksolutions.com>2008-10-21 08:55:42 -0700
commitfca99a946647283f9a6b38189c241954d255a231 (patch)
treefd4b7f5581b68f55a5ec51c59be85f3ffc1d978c /chef/spec/unit/rest_spec.rb
parentceeab2205bb495ed88537aeb634ed823a96b740e (diff)
downloadchef-fca99a946647283f9a6b38189c241954d255a231.tar.gz
Adding behaviors for Chef::REST with chunky downloading, a few changes to Chef::REST for readability, adding behaviors for Chef::Resource and supports[]
Diffstat (limited to 'chef/spec/unit/rest_spec.rb')
-rw-r--r--chef/spec/unit/rest_spec.rb81
1 files changed, 54 insertions, 27 deletions
diff --git a/chef/spec/unit/rest_spec.rb b/chef/spec/unit/rest_spec.rb
index 471330189c..a4171cd666 100644
--- a/chef/spec/unit/rest_spec.rb
+++ b/chef/spec/unit/rest_spec.rb
@@ -104,6 +104,7 @@ describe Chef::REST, "run_request method" do
@http_response_mock.stub!(:kind_of?).with(Net::HTTPSuccess).and_return(true)
@http_response_mock.stub!(:body).and_return("ninja")
@http_response_mock.stub!(:error!).and_return(true)
+ @http_response_mock.stub!(:header).and_return({ 'Content-Length' => "5" })
@http_mock = mock("Net::HTTP", :null_object => true)
@http_mock.stub!(:verify_mode=).and_return(true)
@http_mock.stub!(:read_timeout=).and_return(true)
@@ -115,7 +116,7 @@ describe Chef::REST, "run_request method" do
@request_mock.stub!(:method).and_return(true)
@request_mock.stub!(:path).and_return(true)
@http_mock.stub!(:request).and_return(@http_response_mock)
- @tf_mock = mock(Tempfile, { :print => true, :close => true })
+ @tf_mock = mock(Tempfile, { :print => true, :close => true, :write => true })
Tempfile.stub!(:new).with("chef-rest").and_return(@tf_mock)
end
@@ -145,6 +146,14 @@ describe Chef::REST, "run_request method" do
do_run_request
end
+ it "should set the cookie for this request if one exists for the given host:port" do
+ @r.cookies = { "#{@url_mock.host}:#{@url_mock.port}" => "cookie monster" }
+ Net::HTTP::Get.should_receive(:new).with("/?foo=bar",
+ { 'Accept' => 'application/json', 'Cookie' => 'cookie monster' }
+ ).and_return(@request_mock)
+ do_run_request
+ end
+
it "should build a new HTTP GET request" do
Net::HTTP::Get.should_receive(:new).with("/?foo=bar",
{ 'Accept' => 'application/json' }
@@ -211,31 +220,49 @@ describe Chef::REST, "run_request method" do
do_run_request(:GET, false, 10, true)
end
- ###
- # TODO - Figure out how to test the http.request(foo) do |response| block
- ###
- # it "should create a tempfile for the output of a raw request" do
- # fake_http = FakeHTTP.new
- # fake_http.response_object = @http_response_mock
- # Net::HTTP.stub!(:new).and_return(fake_http)
- # Tempfile.should_receive(:new).with("chef-rest").and_return(@tf_mock)
- # do_run_request(:GET, false, 10, true).should eql(@tf_mock)
- # end
- #
- # it "should populate the tempfile with the value of the raw request" do
- # fake_http = FakeHTTP.new
- # fake_http.response_object = @http_response_mock
- # Net::HTTP.stub!(:new).and_return(fake_http)
- # @tf_mock.should_receive(:write, "ninja").once.and_return(true)
- # do_run_request(:GET, false, 10, true)
- # end
- #
- # it "should close the tempfile if we're doing a raw request" do
- # fake_http = FakeHTTP.new
- # fake_http.response_object = @http_response_mock
- # Net::HTTP.stub!(:new).and_return(fake_http)
- # @tf_mock.should_receive(:close).once.and_return(true)
- # do_run_request(:GET, false, 10, true)
- # end
+ it "should create a tempfile for the output of a raw request" do
+ @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
+ Tempfile.should_receive(:new).with("chef-rest").and_return(@tf_mock)
+ do_run_request(:GET, false, 10, true).should eql(@tf_mock)
+ end
+
+ it "should read the body of the response in chunks on a raw request" do
+ @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
+ @http_response_mock.should_receive(:read_body).and_return(true)
+ do_run_request(:GET, false, 10, true)
+ end
+
+ it "should populate the tempfile with the value of the raw request" do
+ @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
+ @http_response_mock.stub!(:read_body).and_yield("ninja")
+ @tf_mock.should_receive(:write, "ninja").once.and_return(true)
+ do_run_request(:GET, false, 10, true)
+ end
+
+ it "should close the tempfile if we're doing a raw request" do
+ @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
+ @tf_mock.should_receive(:close).once.and_return(true)
+ do_run_request(:GET, false, 10, true)
+ end
+
+ it "should not raise a divide by zero exception if the size is 0" do
+ @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
+ @http_response_mock.stub!(:header).and_return({ 'Content-Length' => "5" })
+ @http_response_mock.stub!(:read_body).and_yield('')
+ lambda { do_run_request(:GET, false, 10, true) }.should_not raise_error(ZeroDivisionError)
+ end
+
+ it "should not raise a divide by zero exception if the Content-Length is 0" do
+ @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
+ @http_response_mock.stub!(:header).and_return({ 'Content-Length' => "0" })
+ @http_response_mock.stub!(:read_body).and_yield("ninja")
+ lambda { do_run_request(:GET, false, 10, true) }.should_not raise_error(ZeroDivisionError)
+ end
+
+ it "should call read_body without a block if the request is not raw" do
+ @http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
+ @http_response_mock.should_receive(:read_body)
+ do_run_request(:GET, false, 10, false)
+ end
end