Skip to content

Commit

Permalink
New method Bio::Command#start_http_uri(uri) with tests
Browse files Browse the repository at this point in the history
 * lib/bio/command.rb: New method Bio::Command#start_http_uri(uri)
   that supports HTTPS. Note that this method is intended to be
   called only from BioRuby internals.
 * lib/bio/command.rb: Bio::Command#post and #post_form are changed
   to use the start_http_uri().
 * test/network/bio/test_command.rb: tests for start_http_uri().
  • Loading branch information
ngoto committed Aug 12, 2016
1 parent 11c680f commit 7abd46f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
42 changes: 40 additions & 2 deletions lib/bio/command.rb
Expand Up @@ -13,6 +13,7 @@
require 'open-uri'
require 'cgi'
require 'net/http'
require 'net/https'
require 'tmpdir'
require 'fileutils'

Expand Down Expand Up @@ -706,6 +707,43 @@ def read_uri(uri)
OpenURI.open_uri(uri).read
end

# Same as:
# Net::HTTP.start(uri.address, uri.port)
# and
# it uses proxy if an environment variable (same as OpenURI.open_uri)
# is set.
# It supports https.
#
# Note: This method ignores uri.path.
# It only uses uri.address and uri.port.
#
# ---
# *Arguments*:
# * (required) _uri_: URI object or String containing URI
# *Returns*:: (same as Net::HTTP::start except for proxy and https support)
def start_http_uri(uri, &block)
unless uri.is_a?(URI)
uri = URI.parse(uri)
end

# Note: URI#find_proxy is an unofficial method defined in open-uri.rb.
# If the spec of open-uri.rb would be changed, we should change below.
if proxyuri = uri.find_proxy then
raise 'Non-HTTP proxy' if proxyuri.class != URI::HTTP
klass = Net::HTTP.Proxy(proxyuri.host, proxyuri.port)
else
klass = Net::HTTP
end

http = klass.new(uri.host, uri.port)
case uri.scheme
when 'https'
http.use_ssl = true
end

http.start(&block)
end

# Same as:
# Net::HTTP.start(address, port)
# and
Expand Down Expand Up @@ -813,7 +851,7 @@ def post_form(uri, params = nil, header = {})
}
hash.update(header)

start_http(uri.host, uri.port) do |http|
start_http_uri(uri) do |http|
http.post(uri.path, data, hash)
end
end
Expand Down Expand Up @@ -937,7 +975,7 @@ def post(uri, data, header = {})
}
hash.update(header)

start_http(uri.host, uri.port) do |http|
start_http_uri(uri) do |http|
http.post(uri.path, data, hash)
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/network/bio/test_command.rb
Expand Up @@ -13,6 +13,7 @@

# libraries needed for the tests
require 'test/unit'
require 'uri'
require 'bio/command'

module Bio
Expand All @@ -22,6 +23,7 @@ def setup
@port = 80
@path = "/"
@url = "http://bioruby.open-bio.org:80/"
@uri = URI.parse(@url)
end

def test_read_uri
Expand All @@ -32,6 +34,21 @@ def test_read_uri
assert(!str.to_s.empty?)
end

def test_start_http_uri
ht = Bio::Command.start_http_uri(@uri)
assert_kind_of(Net::HTTP, ht)
res = ht.get(@path)
assert_kind_of(Net::HTTPResponse, res)
end

def test_start_http_uri_with_block
res = Bio::Command.start_http_uri(@uri) do |ht|
assert_kind_of(Net::HTTP, ht)
ht.get(@path)
end
assert_kind_of(Net::HTTPResponse, res)
end

def test_start_http
ht = Bio::Command.start_http(@host, @port)
assert_kind_of(Net::HTTP, ht)
Expand Down

0 comments on commit 7abd46f

Please sign in to comment.