From 3fb87cc744d1e1134476496dedc9125a25add859 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Thu, 4 Sep 2014 21:38:07 -0700 Subject: Print out request and response body on non-2xx response --- spec/functional/http/simple_spec.rb | 58 ++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'spec/functional') diff --git a/spec/functional/http/simple_spec.rb b/spec/functional/http/simple_spec.rb index 092d164342..fec71351df 100644 --- a/spec/functional/http/simple_spec.rb +++ b/spec/functional/http/simple_spec.rb @@ -80,5 +80,61 @@ describe Chef::HTTP::Simple do end it_behaves_like "downloading all the things" -end + context "when Chef::Log.level = :debug" do + before do + Chef::Log.level = :debug + @debug_log = '' + Chef::Log.stub(:debug) { |str| @debug_log << str } + end + + let(:source) { 'http://localhost:9000' } + + it "Logs the request and response for 200's but not the body" do + http_client.get('http://localhost:9000/nyan_cat.png') + expect(@debug_log).to match(/200/) + expect(@debug_log).to match(/HTTP Request Header Data/) + expect(@debug_log).to match(/HTTP Status and Header Data/) + expect(@debug_log).not_to match(/HTTP Request Body/) + expect(@debug_log).not_to match(/HTTP Response Body/) + expect(@debug_log).not_to match(/Your request is just terrible./) + end + + it "Logs the request and response for 200 POST, but not the body" do + http_client.post('http://localhost:9000/posty', 'hithere') + expect(@debug_log).to match(/200/) + expect(@debug_log).to match(/HTTP Request Header Data/) + expect(@debug_log).to match(/HTTP Status and Header Data/) + expect(@debug_log).not_to match(/HTTP Request Body/) + expect(@debug_log).not_to match(/hithere/) + expect(@debug_log).not_to match(/HTTP Response Body/) + expect(@debug_log).not_to match(/Your request is just terrible./) + end + + it "Logs the request and response and bodies for 400 response" do + expect do + http_client.get('http://localhost:9000/bad_request') + end.to raise_error(Net::HTTPServerException) + expect(@debug_log).to match(/400/) + expect(@debug_log).to match(/HTTP Request Header Data/) + expect(@debug_log).to match(/HTTP Status and Header Data/) + expect(@debug_log).not_to match(/HTTP Request Body/) + expect(@debug_log).not_to match(/hithere/) + expect(@debug_log).to match(/HTTP Response Body/) + expect(@debug_log).to match(/Your request is just terrible./) + end + + it "Logs the request and response and bodies for 400 POST response" do + expect do + http_client.post('http://localhost:9000/bad_request', 'hithere') + end.to raise_error(Net::HTTPServerException) + expect(@debug_log).to match(/400/) + expect(@debug_log).to match(/HTTP Request Header Data/) + expect(@debug_log).to match(/HTTP Status and Header Data/) + expect(@debug_log).to match(/HTTP Request Body/) + expect(@debug_log).to match(/hithere/) + expect(@debug_log).to match(/HTTP Response Body/) + expect(@debug_log).to match(/Your request is just terrible./) + end + end +end -- cgit v1.2.1