Skip to content

Commit fca99a9

Browse files
committed
Adding behaviors for Chef::REST with chunky downloading, a few changes to Chef::REST for readability, adding behaviors for Chef::Resource and supports[]
1 parent ceeab22 commit fca99a9

4 files changed

Lines changed: 69 additions & 30 deletions

File tree

chef/lib/chef/rest.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
class Chef
2626
class REST
2727

28+
attr_accessor :url, :cookies
29+
2830
def initialize(url)
2931
@url = url
3032
@cookies = Hash.new
@@ -89,7 +91,7 @@ def run_request(method, url, data=false, limit=10, raw=false)
8991
'Accept' => "application/json",
9092
}
9193
end
92-
if @cookies["#{url.host}:#{url.port}"]
94+
if @cookies.has_key?("#{url.host}:#{url.port}")
9395
headers['Cookie'] = @cookies["#{url.host}:#{url.port}"]
9496
end
9597
req = nil
@@ -124,7 +126,8 @@ def run_request(method, url, data=false, limit=10, raw=false)
124126
tf = Tempfile.new("chef-rest")
125127
# Stolen from http://www.ruby-forum.com/topic/166423
126128
# Kudos to _why!
127-
size, total = 0, response.header['Content-Length'].to_i
129+
size = 0
130+
total = response.header['Content-Length'].to_i
128131
response.read_body do |chunk|
129132
tf.write(chunk)
130133
size += chunk.size

chef/spec/unit/resource_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,13 @@
141141
serialized_node.name.should eql(@resource.name)
142142
end
143143

144+
it "should allow you to set what features this resource supports" do
145+
support_hash = { :one => :two }
146+
@resource.supports(support_hash)
147+
@resource.supports.should eql(support_hash)
148+
end
149+
150+
it "should return the current value of supports" do
151+
@resource.supports.should == {}
152+
end
144153
end

chef/spec/unit/rest_spec.rb

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
@http_response_mock.stub!(:kind_of?).with(Net::HTTPSuccess).and_return(true)
105105
@http_response_mock.stub!(:body).and_return("ninja")
106106
@http_response_mock.stub!(:error!).and_return(true)
107+
@http_response_mock.stub!(:header).and_return({ 'Content-Length' => "5" })
107108
@http_mock = mock("Net::HTTP", :null_object => true)
108109
@http_mock.stub!(:verify_mode=).and_return(true)
109110
@http_mock.stub!(:read_timeout=).and_return(true)
@@ -115,7 +116,7 @@
115116
@request_mock.stub!(:method).and_return(true)
116117
@request_mock.stub!(:path).and_return(true)
117118
@http_mock.stub!(:request).and_return(@http_response_mock)
118-
@tf_mock = mock(Tempfile, { :print => true, :close => true })
119+
@tf_mock = mock(Tempfile, { :print => true, :close => true, :write => true })
119120
Tempfile.stub!(:new).with("chef-rest").and_return(@tf_mock)
120121
end
121122

@@ -145,6 +146,14 @@ def do_run_request(method=:GET, data=false, limit=10, raw=false)
145146
do_run_request
146147
end
147148

149+
it "should set the cookie for this request if one exists for the given host:port" do
150+
@r.cookies = { "#{@url_mock.host}:#{@url_mock.port}" => "cookie monster" }
151+
Net::HTTP::Get.should_receive(:new).with("/?foo=bar",
152+
{ 'Accept' => 'application/json', 'Cookie' => 'cookie monster' }
153+
).and_return(@request_mock)
154+
do_run_request
155+
end
156+
148157
it "should build a new HTTP GET request" do
149158
Net::HTTP::Get.should_receive(:new).with("/?foo=bar",
150159
{ 'Accept' => 'application/json' }
@@ -211,31 +220,49 @@ def do_run_request(method=:GET, data=false, limit=10, raw=false)
211220
do_run_request(:GET, false, 10, true)
212221
end
213222

214-
###
215-
# TODO - Figure out how to test the http.request(foo) do |response| block
216-
###
217-
# it "should create a tempfile for the output of a raw request" do
218-
# fake_http = FakeHTTP.new
219-
# fake_http.response_object = @http_response_mock
220-
# Net::HTTP.stub!(:new).and_return(fake_http)
221-
# Tempfile.should_receive(:new).with("chef-rest").and_return(@tf_mock)
222-
# do_run_request(:GET, false, 10, true).should eql(@tf_mock)
223-
# end
224-
#
225-
# it "should populate the tempfile with the value of the raw request" do
226-
# fake_http = FakeHTTP.new
227-
# fake_http.response_object = @http_response_mock
228-
# Net::HTTP.stub!(:new).and_return(fake_http)
229-
# @tf_mock.should_receive(:write, "ninja").once.and_return(true)
230-
# do_run_request(:GET, false, 10, true)
231-
# end
232-
#
233-
# it "should close the tempfile if we're doing a raw request" do
234-
# fake_http = FakeHTTP.new
235-
# fake_http.response_object = @http_response_mock
236-
# Net::HTTP.stub!(:new).and_return(fake_http)
237-
# @tf_mock.should_receive(:close).once.and_return(true)
238-
# do_run_request(:GET, false, 10, true)
239-
# end
223+
it "should create a tempfile for the output of a raw request" do
224+
@http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
225+
Tempfile.should_receive(:new).with("chef-rest").and_return(@tf_mock)
226+
do_run_request(:GET, false, 10, true).should eql(@tf_mock)
227+
end
228+
229+
it "should read the body of the response in chunks on a raw request" do
230+
@http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
231+
@http_response_mock.should_receive(:read_body).and_return(true)
232+
do_run_request(:GET, false, 10, true)
233+
end
234+
235+
it "should populate the tempfile with the value of the raw request" do
236+
@http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
237+
@http_response_mock.stub!(:read_body).and_yield("ninja")
238+
@tf_mock.should_receive(:write, "ninja").once.and_return(true)
239+
do_run_request(:GET, false, 10, true)
240+
end
241+
242+
it "should close the tempfile if we're doing a raw request" do
243+
@http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
244+
@tf_mock.should_receive(:close).once.and_return(true)
245+
do_run_request(:GET, false, 10, true)
246+
end
247+
248+
it "should not raise a divide by zero exception if the size is 0" do
249+
@http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
250+
@http_response_mock.stub!(:header).and_return({ 'Content-Length' => "5" })
251+
@http_response_mock.stub!(:read_body).and_yield('')
252+
lambda { do_run_request(:GET, false, 10, true) }.should_not raise_error(ZeroDivisionError)
253+
end
254+
255+
it "should not raise a divide by zero exception if the Content-Length is 0" do
256+
@http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
257+
@http_response_mock.stub!(:header).and_return({ 'Content-Length' => "0" })
258+
@http_response_mock.stub!(:read_body).and_yield("ninja")
259+
lambda { do_run_request(:GET, false, 10, true) }.should_not raise_error(ZeroDivisionError)
260+
end
261+
262+
it "should call read_body without a block if the request is not raw" do
263+
@http_mock.stub!(:request).and_yield(@http_response_mock).and_return(@http_response_mock)
264+
@http_response_mock.should_receive(:read_body)
265+
do_run_request(:GET, false, 10, false)
266+
end
240267

241268
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Chef::Log.debug("You are the first of fakefile's attributes")
2-
friends("you said we wuz visiting em")
2+
friends("you said we wuz visiting em")

0 commit comments

Comments
 (0)