Skip to content

Commit

Permalink
log response with http status, content type, and output body size
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Wiggins committed Aug 14, 2008
1 parent fdd9010 commit d3d5a72
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
12 changes: 9 additions & 3 deletions lib/rest_client.rb
Expand Up @@ -148,10 +148,12 @@ def transmit(uri, req, payload)
net = Net::HTTP.new(uri.host, uri.port)
net.use_ssl = uri.is_a?(URI::HTTPS)

display_log(log)
display_log request_log

net.start do |http|
process_result http.request(req, payload || "")
res = http.request(req, payload || "")
display_log response_log(res)
process_result res
end
rescue EOFError
raise RestClient::ServerBrokeConnection
Expand Down Expand Up @@ -185,7 +187,7 @@ def process_result(res)
end
end

def log
def request_log
if @payload
if @payload.size > 100
"RestClient.#{method} '#{url}', '(#{payload.size} byte payload)'"
Expand All @@ -197,6 +199,10 @@ def log
end
end

def response_log(res)
"# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{res['Content-type'].gsub(/;.*$/, '')} #{res.body.size} bytes"
end

def display_log(msg)
return unless log_to = RestClient.log

Expand Down
28 changes: 22 additions & 6 deletions spec/rest_client_spec.rb
Expand Up @@ -129,6 +129,7 @@
it "transmits the request with Net::HTTP" do
@http.should_receive(:request).with('req', 'payload')
@request.should_receive(:process_result)
@request.should_receive(:response_log)
@request.transmit(@uri, 'req', 'payload')
end

Expand All @@ -137,12 +138,14 @@
@net.should_receive(:use_ssl=).with(true)
@http.stub!(:request)
@request.stub!(:process_result)
@request.stub!(:response_log)
@request.transmit(@uri, 'req', 'payload')
end

it "doesn't send nil payloads" do
@http.should_receive(:request).with('req', '')
@request.should_receive(:process_result)
@request.stub!(:response_log)
@request.transmit(@uri, 'req', nil)
end

Expand All @@ -169,6 +172,7 @@
it "sets up the credentials prior to the request" do
@http.stub!(:request)
@request.stub!(:process_result)
@request.stub!(:response_log)

@request.stub!(:user).and_return('joe')
@request.stub!(:password).and_return('mypass')
Expand Down Expand Up @@ -240,21 +244,33 @@
lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
end

it "logs a get" do
RestClient::Request.new(:method => :get, :url => 'http://url').log.should ==
it "logs a get request" do
RestClient::Request.new(:method => :get, :url => 'http://url').request_log.should ==
"RestClient.get 'http://url'"
end

it "logs a post with a small payload" do
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log.should ==
it "logs a post request with a small payload" do
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log.should ==
"RestClient.post 'http://url', 'foo'"
end

it "logs a post with a long payload" do
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log.should ==
it "logs a post request with a large payload" do
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log.should ==
"RestClient.post 'http://url', '(1000 byte payload)'"
end

it "logs a response including the status code, content type, and result body size in bytes" do
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub!(:[]).with('Content-type').and_return('text/html')
@request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
end

it "strips the charset from the response content type" do
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
@request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
end

it "displays the log to stdout" do
RestClient.stub!(:log).and_return('stdout')
STDOUT.should_receive(:puts).with('xyz')
Expand Down

0 comments on commit d3d5a72

Please sign in to comment.