|
104 | 104 | @http_response_mock.stub!(:kind_of?).with(Net::HTTPSuccess).and_return(true) |
105 | 105 | @http_response_mock.stub!(:body).and_return("ninja") |
106 | 106 | @http_response_mock.stub!(:error!).and_return(true) |
| 107 | + @http_response_mock.stub!(:header).and_return({ 'Content-Length' => "5" }) |
107 | 108 | @http_mock = mock("Net::HTTP", :null_object => true) |
108 | 109 | @http_mock.stub!(:verify_mode=).and_return(true) |
109 | 110 | @http_mock.stub!(:read_timeout=).and_return(true) |
|
115 | 116 | @request_mock.stub!(:method).and_return(true) |
116 | 117 | @request_mock.stub!(:path).and_return(true) |
117 | 118 | @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 }) |
119 | 120 | Tempfile.stub!(:new).with("chef-rest").and_return(@tf_mock) |
120 | 121 | end |
121 | 122 |
|
@@ -145,6 +146,14 @@ def do_run_request(method=:GET, data=false, limit=10, raw=false) |
145 | 146 | do_run_request |
146 | 147 | end |
147 | 148 |
|
| 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 | + |
148 | 157 | it "should build a new HTTP GET request" do |
149 | 158 | Net::HTTP::Get.should_receive(:new).with("/?foo=bar", |
150 | 159 | { 'Accept' => 'application/json' } |
@@ -211,31 +220,49 @@ def do_run_request(method=:GET, data=false, limit=10, raw=false) |
211 | 220 | do_run_request(:GET, false, 10, true) |
212 | 221 | end |
213 | 222 |
|
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 |
240 | 267 |
|
241 | 268 | end |
0 commit comments