summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2014-01-22 16:16:55 -0800
committerClaire McQuin <claire@getchef.com>2014-01-23 16:57:29 -0800
commit91306695d946656157112e7303bd2945d4f31aeb (patch)
tree4c7f959fc19d72cf3156a6c4ca198944e139872c
parent4bef6b984bce5ea20fecf7b7827b8b1b31ac3fd2 (diff)
downloadchef-91306695d946656157112e7303bd2945d4f31aeb.tar.gz
add specs for CHEF-4502 behavior
-rw-r--r--spec/unit/rest_spec.rb38
1 files changed, 32 insertions, 6 deletions
diff --git a/spec/unit/rest_spec.rb b/spec/unit/rest_spec.rb
index e512c09b2e..a94f026d33 100644
--- a/spec/unit/rest_spec.rb
+++ b/spec/unit/rest_spec.rb
@@ -225,7 +225,7 @@ describe Chef::REST do
http_response = Net::HTTPSuccess.new("1.1", "200", "successful rest req")
http_response.stub(:read_body)
http_response.stub(:body).and_return(body)
- http_response.add_field("Content-Length", body.bytesize)
+ http_response["Content-Length"] = body.bytesize.to_s
http_response
end
@@ -310,17 +310,22 @@ describe Chef::REST do
end
it "should not raise a divide by zero exception if the size is 0" do
- http_response.stub(:header).and_return({ 'Content-Length' => "5" })
+ http_response['Content-Length'] = "5"
http_response.stub(:read_body).and_yield('')
expect { rest.streaming_request(url, {}) }.not_to raise_error
end
it "should not raise a divide by zero exception if the Content-Length is 0" do
- http_response.stub(:header).and_return({ 'Content-Length' => "0" })
+ http_response['Content-Length'] = "0"
http_response.stub(:read_body).and_yield("ninja")
expect { rest.streaming_request(url, {}) }.not_to raise_error
end
+ it "should fail if the response is truncated" do
+ http_response["Content-Length"] = (body.bytesize + 99).to_s
+ expect { rest.streaming_request(url, {}) }.not_to raise_error(RuntimeError)
+ end
+
end
describe "as JSON API requests" do
@@ -444,12 +449,23 @@ describe Chef::REST do
expect(rest.request(:GET, url)).to eq("ninja")
end
+ it "should fail if the response is truncated" do
+ http_response["Content-Length"] = (body.bytesize + 99).to_s
+ expect { rest.request(:GET, url) }.to raise_error(RuntimeError)
+ end
+
context "when JSON is returned" do
let(:body) { '{"ohai2u":"json_api"}' }
it "should inflate the body as to an object" do
http_response.add_field('content-type', "application/json")
expect(rest.request(:GET, url, {})).to eq({"ohai2u"=>"json_api"})
end
+
+ it "should fail if the response is truncated" do
+ http_response.add_field('content-type', "application/json")
+ http_response["Content-Length"] = (body.bytesize + 99).to_s
+ expect { rest.request(:GET, url, {}) }.to raise_error(RuntimeError)
+ end
end
%w[ HTTPFound HTTPMovedPermanently HTTPSeeOther HTTPUseProxy HTTPTemporaryRedirect HTTPMultipleChoice ].each do |resp_name|
@@ -532,6 +548,10 @@ describe Chef::REST do
expect {rest.request(:GET, url)}.to raise_error(Net::HTTPFatalError)
expect(log_stringio.string).to match(Regexp.escape('INFO: HTTP Request Returned 500 drooling from inside of mouth: Ears get sore!, Not even four'))
end
+ it "fails when the compressed body is truncated" do
+ http_response["Content-Length"] = (body.bytesize + 99).to_s
+ expect {rest.request(:GET, url)}.to raise_error(RuntimeError)
+ end
end
context "on a generic unsuccessful request" do
@@ -559,7 +579,7 @@ describe Chef::REST do
http_response.stub(:read_body)
http_response.should_not_receive(:body)
- http_response.add_field("Content-Length", body.bytesize)
+ http_response["Content-Length"] = body.bytesize.to_s
http_response
end
@@ -608,17 +628,23 @@ describe Chef::REST do
end
it "does not raise a divide by zero exception if the content's actual size is 0" do
- http_response.add_field('Content-Length', "5")
+ http_response['Content-Length'] = "5"
http_response.stub(:read_body).and_yield('')
expect { rest.streaming_request(url, {}) }.not_to raise_error
end
it "does not raise a divide by zero exception when the Content-Length is 0" do
- http_response.add_field('Content-Length', "0")
+ http_response['Content-Length'] = "0"
http_response.stub(:read_body).and_yield("ninja")
expect { rest.streaming_request(url, {}) }.not_to raise_error
end
+ it "it raises an exception when the download is truncated" do
+ http_response["Content-Length"] = (body.bytesize + 99).to_s
+ http_response.stub(:read_body).and_yield("ninja")
+ expect { rest.streaming_request(url, {}) }.to raise_error(RuntimeError)
+ end
+
it "fetches a file and yields the tempfile it is streamed to" do
http_response.stub(:read_body).and_yield("real").and_yield("ultimate").and_yield("power")
tempfile_path = nil