Skip to content

Commit

Permalink
Request/response logging. #9.
Browse files Browse the repository at this point in the history
To do: write the test client log to a file and archive the result.
  • Loading branch information
hannahwhy committed Oct 25, 2012
1 parent 18b37ab commit cea3c98
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
22 changes: 22 additions & 0 deletions lib/castanet/client.rb
@@ -1,3 +1,4 @@
require 'logger'
require 'net/http'
require 'net/https'
require 'uri'
Expand Down Expand Up @@ -66,6 +67,27 @@ def https_required
true
end

##
# Logs messages from ticket operations.
#
# By default, this is a memoized instance of Ruby's Logger class with
# log level DEBUG and log device set to standard error.
#
# | Log level | Data logged |
# +-----------+------------------------------------------------+
# | DEBUG | Request URLs, response code/body, SSL context |
#
# In production, you may want to silence Castanet output. To do so, set
# the log level > DEBUG or log device to nil.
def logger
@logger ||= Logger.new($stderr).tap do |l|
formatter = Logger::Formatter.new

l.level = Logger::DEBUG
l.formatter = lambda { |*args| "[#{Module.nesting.first}] #{formatter.call(*args)}" }
end
end

##
# Returns a hash of SSL options.
#
Expand Down
10 changes: 10 additions & 0 deletions lib/castanet/service_ticket.rb
Expand Up @@ -17,6 +17,7 @@ class ServiceTicket

def_delegators :client,
:https_required,
:logger,
:proxy_callback_url,
:proxy_retrieval_url,
:service_validate_url,
Expand Down Expand Up @@ -94,6 +95,7 @@ def present!

net_http(uri).start do |h|
cas_response = h.get(uri.request_uri)
log_response(cas_response, uri)

self.response = parsed_ticket_validate_response(cas_response.body)
end
Expand Down Expand Up @@ -132,6 +134,8 @@ def retrieve_pgt!

net_http(uri).start do |h|
response = h.get(uri.request_uri)
log_response(response, uri)

body = response.body

case response
Expand Down Expand Up @@ -162,11 +166,17 @@ def validation_url
#
# @return [Net::HTTP]
def net_http(uri)
logger.debug { "Request to #{uri}: ssl_context=#{ssl_context.inspect}" }

Net::HTTP.new(uri.host, uri.port).tap do |h|
configure_ssl(h, ssl_context) if use_ssl?(uri.scheme)
end
end

def log_response(resp, uri)
logger.debug { "Response from #{uri}: status=#{resp.code}, body=#{resp.body}" }
end

private

def configure_ssl(h, ssl)
Expand Down
8 changes: 4 additions & 4 deletions spec/support/test_client.rb
Expand Up @@ -8,14 +8,14 @@

class TestClient < OpenStruct
include Castanet::Client
end

let(:client) do
TestClient.new.tap do |tc|
tc.cas_url = https_cas_url
def logger
Logger.new(nil)
end
end

let(:client) { TestClient.new }

def use_https_urls
client.cas_url = https_cas_url
client.proxy_retrieval_url = https_proxy_retrieval_url
Expand Down

0 comments on commit cea3c98

Please sign in to comment.