Permalink
Browse files

* test coverage checked. see #189.

  • Loading branch information...
1 parent 0ebdcc1 commit 2253f959c20dd5bca01fa6a3ecc26715348a5279 nahi committed Dec 19, 2008
Showing with 416 additions and 279 deletions.
  1. +61 −87 lib/httpclient.rb
  2. +25 −32 lib/httpclient/cookie.rb
  3. +153 −108 lib/httpclient/http.rb
  4. +37 −45 lib/httpclient/session.rb
  5. +8 −0 lib/httpclient/util.rb
  6. +132 −7 test/test_httpclient.rb
View
@@ -55,6 +55,8 @@ class HTTPClient
/: (\S+) (\S+)/ =~ %q$Id$
LIB_NAME = "(#{$1}/#{$2}, #{RUBY_VERSION_STRING})"
+ include Util
+
class ConfigurationError < StandardError
end
@@ -84,8 +86,30 @@ class Session
BadResponse = ::HTTPClient::BadResponseError
end
- attr_reader :agent_name
- attr_reader :from
+ class << self
+ def attr_proxy(symbol, assignable = false)
+ name = symbol.to_s
+ define_method(name) {
+ @session_manager.__send__(name)
+ }
+ if assignable
+ aname = name + '='
+ define_method(aname) { |rhs|
+ reset_all
+ @session_manager.__send__(aname, rhs)
+ }
+ end
+ end
+
+ %w(get_content post_content head get post put delete options propfind proppatch trace).each do |name|
+ eval <<-EOD
+ def #{name}(*arg)
+ new.#{name}(*arg)
+ end
+ EOD
+ end
+ end
+
attr_reader :ssl_config
attr_accessor :cookie_manager
attr_reader :test_loopback_response
@@ -96,17 +120,17 @@ class Session
attr_accessor :follow_redirect_count
attr_accessor :protocol_retry_count
- include Util
+ attr_proxy(:protocol_version, true)
+ attr_proxy(:connect_timeout, true)
+ attr_proxy(:send_timeout, true)
+ attr_proxy(:receive_timeout, true)
+ # if your ruby is older than 2005-09-06, do not set socket_sync = false to
+ # avoid an SSL socket blocking bug in openssl/buffering.rb.
+ attr_proxy(:socket_sync, true)
+ attr_proxy(:agent_name, true)
+ attr_proxy(:from, true)
- class << self
- %w(get_content post_content head get post put delete options propfind proppatch trace).each do |name|
- eval <<-EOD
- def #{name}(*arg)
- new.#{name}(*arg)
- end
- EOD
- end
- end
+ attr_proxy(:test_loopback_http_response)
PROPFIND_DEFAULT_EXTHEADER = { 'Depth' => '0' }
@@ -122,20 +146,19 @@ def #{name}(*arg)
# Create an instance.
# SSLConfig cannot be re-initialized. Create new client.
#
- def initialize(proxy = nil, agent_name = nil, from = nil)
+ def initialize(*args)
+ proxy, agent_name, from = keyword_argument(args, :proxy, :agent_name, :from)
@proxy = nil # assigned later.
@no_proxy = nil
- @agent_name = agent_name
- @from = from
@www_auth = WWWAuth.new
@proxy_auth = ProxyAuth.new
@request_filter = [@proxy_auth, @www_auth]
@debug_dev = nil
@redirect_uri_callback = method(:default_redirect_uri_callback)
@test_loopback_response = []
@session_manager = SessionManager.new(self)
- @session_manager.agent_name = @agent_name
- @session_manager.from = @from
+ @session_manager.agent_name = agent_name
+ @session_manager.from = from
@session_manager.ssl_config = @ssl_config = SSLConfig.new(self)
@cookie_manager = WebAgent::CookieManager.new
@follow_redirect_count = 10
@@ -154,42 +177,6 @@ def debug_dev=(dev)
@session_manager.debug_dev = dev
end
- def protocol_version
- @session_manager.protocol_version
- end
-
- def protocol_version=(protocol_version)
- reset_all
- @session_manager.protocol_version = protocol_version
- end
-
- def connect_timeout
- @session_manager.connect_timeout
- end
-
- def connect_timeout=(connect_timeout)
- reset_all
- @session_manager.connect_timeout = connect_timeout
- end
-
- def send_timeout
- @session_manager.send_timeout
- end
-
- def send_timeout=(send_timeout)
- reset_all
- @session_manager.send_timeout = send_timeout
- end
-
- def receive_timeout
- @session_manager.receive_timeout
- end
-
- def receive_timeout=(receive_timeout)
- reset_all
- @session_manager.receive_timeout = receive_timeout
- end
-
def proxy
@proxy
end
@@ -223,12 +210,6 @@ def no_proxy=(no_proxy)
reset_all
end
- # if your ruby is older than 2005-09-06, do not set socket_sync = false to
- # avoid an SSL socket blocking bug in openssl/buffering.rb.
- def socket_sync=(socket_sync)
- @session_manager.socket_sync = socket_sync
- end
-
def set_auth(uri, user, passwd)
uri = urify(uri)
@www_auth.set_auth(uri, user, passwd)
@@ -261,10 +242,6 @@ def redirect_uri_callback=(redirect_uri_callback)
@redirect_uri_callback = redirect_uri_callback
end
- def test_loopback_http_response
- @session_manager.test_loopback_http_response
- end
-
# SYNOPSIS
# Client#get_content(uri, query = nil, extheader = {}, &block = nil)
#
@@ -329,35 +306,35 @@ def default_redirect_uri_callback(uri, res)
end
def head(uri, query = nil, extheader = {})
- request('HEAD', uri, query, nil, extheader)
+ request(:head, uri, query, nil, extheader)
end
def get(uri, query = nil, extheader = {}, &block)
- request('GET', uri, query, nil, extheader, &block)
+ request(:get, uri, query, nil, extheader, &block)
end
def post(uri, body = nil, extheader = {}, &block)
- request('POST', uri, nil, body, extheader, &block)
+ request(:post, uri, nil, body, extheader, &block)
end
def put(uri, body = nil, extheader = {}, &block)
- request('PUT', uri, nil, body, extheader, &block)
+ request(:put, uri, nil, body, extheader, &block)
end
def delete(uri, extheader = {}, &block)
- request('DELETE', uri, nil, nil, extheader, &block)
+ request(:delete, uri, nil, nil, extheader, &block)
end
def options(uri, extheader = {}, &block)
- request('OPTIONS', uri, nil, nil, extheader, &block)
+ request(:options, uri, nil, nil, extheader, &block)
end
def propfind(uri, extheader = PROPFIND_DEFAULT_EXTHEADER, &block)
- request('PROPFIND', uri, nil, nil, extheader, &block)
+ request(:propfind, uri, nil, nil, extheader, &block)
end
def proppatch(uri, body = nil, extheader = {}, &block)
- request('PROPPATCH', uri, nil, body, extheader, &block)
+ request(:proppatch, uri, nil, body, extheader, &block)
end
def trace(uri, query = nil, body = nil, extheader = {}, &block)
@@ -367,7 +344,7 @@ def trace(uri, query = nil, body = nil, extheader = {}, &block)
def request(method, uri, query = nil, body = nil, extheader = {}, &block)
uri = urify(uri)
proxy = no_proxy?(uri) ? nil : @proxy
- req = create_request(method, uri, query, body, extheader)
+ req = create_request(method.to_s.upcase, uri, query, body, extheader)
if block
filtered_block = proc { |res, str|
block.call(str)
@@ -379,45 +356,45 @@ def request(method, uri, query = nil, body = nil, extheader = {}, &block)
# Async interface.
def head_async(uri, query = nil, extheader = {})
- request_async('HEAD', uri, query, nil, extheader)
+ request_async(:head, uri, query, nil, extheader)
end
def get_async(uri, query = nil, extheader = {})
- request_async('GET', uri, query, nil, extheader)
+ request_async(:get, uri, query, nil, extheader)
end
def post_async(uri, body = nil, extheader = {})
- request_async('POST', uri, nil, body, extheader)
+ request_async(:post, uri, nil, body, extheader)
end
def put_async(uri, body = nil, extheader = {})
- request_async('PUT', uri, nil, body, extheader)
+ request_async(:put, uri, nil, body, extheader)
end
def delete_async(uri, extheader = {})
- request_async('DELETE', uri, nil, nil, extheader)
+ request_async(:delete, uri, nil, nil, extheader)
end
def options_async(uri, extheader = {})
- request_async('OPTIONS', uri, nil, nil, extheader)
+ request_async(:options, uri, nil, nil, extheader)
end
def propfind_async(uri, extheader = PROPFIND_DEFAULT_EXTHEADER)
- request_async('PROPFIND', uri, nil, nil, extheader)
+ request_async(:propfind, uri, nil, nil, extheader)
end
def proppatch_async(uri, body = nil, extheader = {})
- request_async('PROPPATCH', uri, nil, body, extheader)
+ request_async(:proppatch, uri, nil, body, extheader)
end
def trace_async(uri, query = nil, body = nil, extheader = {})
- request_async('TRACE', uri, query, body, extheader)
+ request_async(:trace, uri, query, body, extheader)
end
def request_async(method, uri, query = nil, body = nil, extheader = {})
uri = urify(uri)
proxy = no_proxy?(uri) ? nil : @proxy
- req = create_request(method, uri, query, body, extheader)
+ req = create_request(method.to_s.upcase, uri, query, body, extheader)
do_request_async(req, proxy)
end
@@ -573,11 +550,8 @@ def create_boundary
end
def file_in_form_data?(body)
- if body.is_a?(Array) or body.is_a?(Hash)
- body.any? { |k, v| v.is_a?(File) }
- else
- false
- end
+ HTTP::Message.multiparam_query?(body) &&
+ body.any? { |k, v| HTTP::Message.file?(v) }
end
def override_header(extheader, key, value)
View
@@ -228,17 +228,16 @@ def initialize(file=nil)
end
def cookies=(cookies)
- @cookies.synchronize do
- @cookies = cookies
- @cookies.extend(MonitorMixin)
- end
+ @cookies = cookies
+ @cookies.extend(MonitorMixin)
end
def save_all_cookies(force = nil, save_unused = true, save_discarded = true)
- if @is_saved and !force
- return
- end
@cookies.synchronize do
+ check_expired_cookies()
+ if @is_saved and !force
+ return
+ end
File.open(@cookies_file, 'w') do |f|
@cookies.each do |cookie|
if (cookie.use? or save_unused) and
@@ -262,15 +261,13 @@ def save_cookies(force = nil)
end
def check_expired_cookies()
- @cookies.synchronize do
- @cookies.reject!{|cookie|
- is_expired = (cookie.expires && (cookie.expires < Time.now.gmtime))
- if is_expired && !cookie.discard?
- @is_saved = false
- end
- is_expired
- }
- end
+ @cookies.reject!{|cookie|
+ is_expired = (cookie.expires && (cookie.expires < Time.now.gmtime))
+ if is_expired && !cookie.discard?
+ @is_saved = false
+ end
+ is_expired
+ }
end
def parse(str, url)
@@ -296,20 +293,18 @@ def make_cookie_str(cookie_list)
def find(url)
- @cookies.synchronize do
- check_expired_cookies()
- return nil if @cookies.empty?
-
- cookie_list = Array.new()
- @cookies.each{|cookie|
- if cookie.use? && cookie.match?(url)
- if cookie_list.select{|c1| c1.name == cookie.name}.empty?
- cookie_list << cookie
- end
+ return nil if @cookies.empty?
+
+ cookie_list = Array.new()
+ @cookies.each{|cookie|
+ is_expired = (cookie.expires && (cookie.expires < Time.now.gmtime))
+ if cookie.use? && !is_expired && cookie.match?(url)
+ if cookie_list.select{|c1| c1.name == cookie.name}.empty?
+ cookie_list << cookie
end
- }
- return make_cookie_str(cookie_list)
- end
+ end
+ }
+ return make_cookie_str(cookie_list)
end
def find_cookie_info(domain, path, name)
@@ -383,6 +378,7 @@ def add(cookie)
cookie.use = true
@cookies << cookie
end
+ check_expired_cookies()
end
cookie.url = url
@@ -402,9 +398,6 @@ def add(cookie)
cookie.discard = false
@is_saved = false
end
-
- check_expired_cookies()
- return false
end
def load_cookies()
Oops, something went wrong.

0 comments on commit 2253f95

Please sign in to comment.