alovak-network 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/network/connection.rb +95 -0
- data/lib/network.rb +12 -0
- data/test/network/test_connection.rb +117 -0
- data/test/network/test_network.rb +8 -0
- data/test/test_helper.rb +5 -0
- metadata +76 -0
@@ -0,0 +1,95 @@
|
|
1
|
+
module Network
|
2
|
+
class Connection
|
3
|
+
attr_reader :uri, :pem
|
4
|
+
|
5
|
+
attr_accessor :read_timeout, :open_timeout, :headers,
|
6
|
+
:verify_peer, :ca_file,
|
7
|
+
:debugger_stream
|
8
|
+
|
9
|
+
READ_TIMEOUT = 60
|
10
|
+
OPEN_TIMEOUT = 30
|
11
|
+
VERIFY_NONE = OpenSSL::SSL::VERIFY_NONE
|
12
|
+
VERIFY_PEER = OpenSSL::SSL::VERIFY_PEER
|
13
|
+
|
14
|
+
def initialize(uri)
|
15
|
+
@uri = URI.parse(uri)
|
16
|
+
@read_timeout = READ_TIMEOUT
|
17
|
+
@open_timeout = OPEN_TIMEOUT
|
18
|
+
@verify_peer = false
|
19
|
+
@debugger_stream = nil
|
20
|
+
@headers = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
def post(data)
|
24
|
+
begin
|
25
|
+
http.post(uri.path, data, post_headers(data))
|
26
|
+
rescue Timeout::Error, Errno::ETIMEDOUT, Timeout::ExitException
|
27
|
+
raise Error, "The connection to the remote server is timed out"
|
28
|
+
rescue EOFError
|
29
|
+
raise Error, "The connection to the remote server was dropped"
|
30
|
+
rescue Errno::ECONNRESET, Errno::ECONNABORTED
|
31
|
+
raise Error, "The remote server reset the connection"
|
32
|
+
rescue Errno::ECONNREFUSED
|
33
|
+
raise Error, "The connection was refused by the remote server"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def use_ssl?
|
38
|
+
@uri.scheme == "https"
|
39
|
+
end
|
40
|
+
|
41
|
+
def pem_file(file)
|
42
|
+
@pem = File.read(file)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def http
|
48
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
49
|
+
configure_timeouts(http)
|
50
|
+
configure_debugging(http)
|
51
|
+
|
52
|
+
configure_ssl(http) if use_ssl?
|
53
|
+
http
|
54
|
+
end
|
55
|
+
|
56
|
+
def post_headers(data)
|
57
|
+
@headers['Content-Type'] ||= 'application/x-www-form-urlencoded'
|
58
|
+
@headers['Content-Length'] ||= data.size.to_s
|
59
|
+
@headers
|
60
|
+
end
|
61
|
+
|
62
|
+
def configure_ssl(http)
|
63
|
+
http.use_ssl = true
|
64
|
+
|
65
|
+
if pem
|
66
|
+
http.cert = cert
|
67
|
+
http.key = key
|
68
|
+
end
|
69
|
+
|
70
|
+
if verify_peer
|
71
|
+
http.verify_mode = VERIFY_PEER
|
72
|
+
http.ca_file = ca_file
|
73
|
+
else
|
74
|
+
http.verify_mode = VERIFY_NONE
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def configure_timeouts(http)
|
79
|
+
http.read_timeout = read_timeout
|
80
|
+
http.open_timeout = open_timeout
|
81
|
+
end
|
82
|
+
|
83
|
+
def configure_debugging(http)
|
84
|
+
http.set_debug_output(debugger_stream)
|
85
|
+
end
|
86
|
+
|
87
|
+
def key
|
88
|
+
OpenSSL::PKey::RSA.new(pem)
|
89
|
+
end
|
90
|
+
|
91
|
+
def cert
|
92
|
+
OpenSSL::X509::Certificate.new(pem)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/network.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestConnection < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@connection = Network::Connection.new("http://example.com/path")
|
6
|
+
@http = mock('http')
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_network_exceptions
|
10
|
+
[ { :class => Timeout::Error, :message => "The connection to the remote server is timed out" },
|
11
|
+
{ :class => EOFError, :message => "The connection to the remote server was dropped"},
|
12
|
+
{ :class => Errno::ECONNRESET, :message => "The remote server reset the connection"},
|
13
|
+
{ :class => Errno::ECONNABORTED, :message => "The remote server reset the connection"},
|
14
|
+
{ :class => Errno::ECONNREFUSED, :message => "The connection was refused by the remote server"},
|
15
|
+
].each do |exception|
|
16
|
+
Net::HTTP.any_instance.expects(:request).raises exception[:class]
|
17
|
+
e = assert_raise Network::Error do
|
18
|
+
@connection.post("some data")
|
19
|
+
end
|
20
|
+
assert_equal exception[:message], e.message
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_default_timeouts
|
25
|
+
assert_equal Network::Connection::READ_TIMEOUT, @connection.read_timeout
|
26
|
+
assert_equal Network::Connection::OPEN_TIMEOUT, @connection.open_timeout
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_change_timeouts
|
30
|
+
@connection.read_timeout = 10
|
31
|
+
@connection.open_timeout = 20
|
32
|
+
assert_equal 10, @connection.read_timeout
|
33
|
+
assert_equal 20, @connection.open_timeout
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_default_headers_for_post_request
|
37
|
+
Net::HTTP.any_instance.expects(:post).with("/path", "data",
|
38
|
+
{ 'Content-Type' => 'application/x-www-form-urlencoded',
|
39
|
+
'Content-Length' => '4' })
|
40
|
+
@connection.post("data")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_user_headers_are_not_overwrited_by_default_headers
|
44
|
+
excepted_headers = {
|
45
|
+
'Content-Type' => 'application/xml',
|
46
|
+
'Accept' => 'text/plain'
|
47
|
+
}
|
48
|
+
|
49
|
+
Net::HTTP.any_instance.expects(:post).with("/path",
|
50
|
+
"data",
|
51
|
+
excepted_headers.update('Content-Length' => '4'))
|
52
|
+
|
53
|
+
@connection.headers = excepted_headers
|
54
|
+
@connection.post("data")
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_configure_ssl_if_scheme_is_https
|
58
|
+
assert Network::Connection.new("https://example.com").use_ssl?
|
59
|
+
assert !Network::Connection.new("http://example.com").use_ssl?
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_default_debug
|
63
|
+
assert_equal nil, @connection.debugger_stream
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_change_debug
|
67
|
+
@connection.debugger_stream = STDOUT
|
68
|
+
assert_equal STDOUT, @connection.debugger_stream
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_default_ssl_verify_mode
|
72
|
+
assert_equal false, @connection.verify_peer
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_ssl_verify_peer_mode
|
76
|
+
@connection.verify_peer = true
|
77
|
+
assert_equal true, @connection.verify_peer
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_timeouts_configuration
|
81
|
+
@http.expects(:read_timeout=)
|
82
|
+
@http.expects(:open_timeout=)
|
83
|
+
@connection.send(:configure_timeouts, @http)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_debugging_configuration
|
87
|
+
@http.expects(:set_debug_output)
|
88
|
+
@connection.send(:configure_debugging, @http)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_ssl_configuration_without_server_certification_verification
|
92
|
+
@http.expects(:use_ssl=)
|
93
|
+
@http.expects(:verify_mode=).with(Network::Connection::VERIFY_NONE)
|
94
|
+
@http.expects(:ca_file=).never
|
95
|
+
|
96
|
+
@connection.send(:configure_ssl, @http)
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_ssl_configuration_with_server_certification_verification
|
100
|
+
@connection.verify_peer = true
|
101
|
+
@http.expects(:use_ssl=)
|
102
|
+
@http.expects(:verify_mode=).with(Network::Connection::VERIFY_PEER)
|
103
|
+
@http.expects(:ca_file=)
|
104
|
+
|
105
|
+
@connection.send(:configure_ssl, @http)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_ssl_configuration_with_client_pem_file
|
109
|
+
@connection.pem_file('test/test.pem')
|
110
|
+
@http.expects(:use_ssl=)
|
111
|
+
@http.expects(:cert=)
|
112
|
+
@http.expects(:key=)
|
113
|
+
@http.expects(:verify_mode=).with(Network::Connection::VERIFY_NONE)
|
114
|
+
|
115
|
+
@connection.send(:configure_ssl, @http)
|
116
|
+
end
|
117
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alovak-network
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavel Gabriel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mocha
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.7
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: turn
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.6.0
|
34
|
+
version:
|
35
|
+
description: HTTP/HTTPS communication module based on ruby net/http, net/https modules
|
36
|
+
email: alovak@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/network/connection.rb
|
45
|
+
- lib/network.rb
|
46
|
+
- test/network/test_connection.rb
|
47
|
+
has_rdoc: false
|
48
|
+
homepage: http://github.com/alovak/network/
|
49
|
+
licenses:
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 2
|
73
|
+
summary: HTTP/HTTPS communication module based on ruby net/http, net/https modules
|
74
|
+
test_files:
|
75
|
+
- test/network/test_network.rb
|
76
|
+
- test/test_helper.rb
|