mockets 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +1 -0
- data/lib/mocket.rb +55 -0
- data/lib/mockets.rb +5 -0
- data/lib/mockets/version.rb +3 -0
- data/lib/socket_adapter.rb +33 -0
- data/mockets.gemspec +25 -0
- data/spec/mocket_spec.rb +74 -0
- data/spec/socket_adapter_spec.rb +42 -0
- data/spec/spec_helper.rb +8 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2032e5f095374ff0cf02d3d39966e5d637ab34ef
|
4
|
+
data.tar.gz: 3c5c6e0fd5bddd76a116af072171a1f3dba56156
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 666cb05a8d9f83d261783a8ecbd10f92f3496fd3bce43306276c89d5c818a73f7e0d1c697963688307ba6d60db05a0714c317147aea757ab0fa74842b6f90fdf
|
7
|
+
data.tar.gz: 5992874366fcf3aa5ef6b2f5a37b09390567a792da60e13bec4790573e2edbeaeace4a8affdf481dcb827dd1283545751446d97a9d4fea5a828228befe8393e7
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Matthew Closson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
mockets
|
2
|
+
=======
|
3
|
+
|
4
|
+
Mock socket library for testing network applications
|
5
|
+
|
6
|
+
#### Reset all mock sockets to default of connection refused
|
7
|
+
```ruby
|
8
|
+
Mocket.reset!
|
9
|
+
Mocket.open('10.10.10.10', 25)
|
10
|
+
Exception: Errno:ECONNREFUSED
|
11
|
+
```
|
12
|
+
|
13
|
+
#### Set a host/port to host unreachable
|
14
|
+
```ruby
|
15
|
+
Mocket.hostunreachable('10.10.10.20', 25)
|
16
|
+
Mocket.open('10.10.10.20', 25)
|
17
|
+
Exception: Errno::EHOSTUNREACH
|
18
|
+
```
|
19
|
+
|
20
|
+
#### Set a host/port to connection timed out
|
21
|
+
```ruby
|
22
|
+
Mocket.timeout('10.10.20.20', 25)
|
23
|
+
Mocket.open('10.10.20.20', 25)
|
24
|
+
Exception: Errno::ETIMEDOUT
|
25
|
+
```
|
26
|
+
|
27
|
+
#### Set a host/port to return an open mocket
|
28
|
+
```ruby
|
29
|
+
Mocket.listen('10.10.10.30', 25)
|
30
|
+
mocket = Mocket.open('10.10.10.30', 25)
|
31
|
+
number_bytes_written = mocket.write('some message here')
|
32
|
+
mocket.close
|
33
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mocket.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
class Mocket
|
2
|
+
@@ports = Hash.new(Hash.new(:ECONNREFUSED))
|
3
|
+
LISTENING = :listening
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
self.state = :open
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.hostunreachable(host, port)
|
10
|
+
self.setport(host, port, :EHOSTUNREACH)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.listen(host, port)
|
14
|
+
self.setport(host, port, LISTENING)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.netunreachable(host, port)
|
18
|
+
self.setport(host, port, :ENETUNREACH)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.open(host, port)
|
22
|
+
state = self.portstate(host, port)
|
23
|
+
return Mocket.new if state == LISTENING
|
24
|
+
raise Errno.const_get(state)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.reset!
|
28
|
+
@@ports = Hash.new(Hash.new(:ECONNREFUSED))
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.timeout(host, port)
|
32
|
+
self.setport(host, port, :ETIMEDOUT)
|
33
|
+
end
|
34
|
+
|
35
|
+
def close
|
36
|
+
self.state = :closed
|
37
|
+
end
|
38
|
+
|
39
|
+
def write(bytes)
|
40
|
+
raise IOError, 'closed stream' if state == :closed
|
41
|
+
bytes.size
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
attr_accessor :state
|
47
|
+
|
48
|
+
def self.portstate(host, port)
|
49
|
+
@@ports[host][port]
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.setport(host, port, state)
|
53
|
+
@@ports[host][port] = state
|
54
|
+
end
|
55
|
+
end
|
data/lib/mockets.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'mocket'
|
2
|
+
require 'socket'
|
3
|
+
|
4
|
+
class SocketAdapter
|
5
|
+
def initialize
|
6
|
+
setstate(:live)
|
7
|
+
end
|
8
|
+
|
9
|
+
def setstate(state)
|
10
|
+
raise ArgumentError unless valid_state?(state)
|
11
|
+
self.state = state
|
12
|
+
end
|
13
|
+
|
14
|
+
def open(host, port)
|
15
|
+
socketlib.open(host, port)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
attr_accessor :state
|
21
|
+
|
22
|
+
def libraries
|
23
|
+
@libraries ||= { live: TCPSocket, test: Mocket }
|
24
|
+
end
|
25
|
+
|
26
|
+
def socketlib
|
27
|
+
libraries[state]
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_state?(state)
|
31
|
+
[:live, :test].include?(state)
|
32
|
+
end
|
33
|
+
end
|
data/mockets.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mockets/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mockets"
|
8
|
+
spec.version = Mockets::VERSION
|
9
|
+
spec.authors = ["Matthew Closson"]
|
10
|
+
spec.email = ["matthew.closson@gmail.com"]
|
11
|
+
spec.description = %q{Mock socket library for testing network applications}
|
12
|
+
spec.summary = %q{Mock socket library}
|
13
|
+
spec.homepage = "https://github.com/mclosson/mockets"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "simplecov"
|
25
|
+
end
|
data/spec/mocket_spec.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mocket do
|
4
|
+
let(:bytes) { '12345' }
|
5
|
+
let(:host) { '10.10.10.10' }
|
6
|
+
let(:port) { 2600 }
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
Mocket.reset!
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#reset!' do
|
13
|
+
it 'sets all previously set host/ports back to connection refused' do
|
14
|
+
Mocket.listen(host, port)
|
15
|
+
Mocket.reset!
|
16
|
+
expect { Mocket.open(host, port) }.to raise_error(Errno::ECONNREFUSED)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '.open' do
|
21
|
+
context 'host is not reachable' do
|
22
|
+
it 'raises a host unreachable error' do
|
23
|
+
Mocket.hostunreachable(host, port)
|
24
|
+
expect { Mocket.open(host, port) }.to raise_error(Errno::EHOSTUNREACH)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
context 'network is not reachable' do
|
28
|
+
it 'raises a net unreachable error' do
|
29
|
+
Mocket.netunreachable(host, port)
|
30
|
+
expect { Mocket.open(host, port) }.to raise_error(Errno::ENETUNREACH)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'times out before getting a response from host' do
|
35
|
+
it 'raises a timed out exception' do
|
36
|
+
Mocket.timeout(host, port)
|
37
|
+
expect { Mocket.open(host, port) }.to raise_error(Errno::ETIMEDOUT)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'the port is closed' do
|
42
|
+
it 'raises a connection refused error' do
|
43
|
+
expect { Mocket.open(host, port) }.to raise_error(Errno::ECONNREFUSED)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'host is reachable and port is open' do
|
48
|
+
it 'returns an open mocket' do
|
49
|
+
Mocket.listen(host, port)
|
50
|
+
expect(Mocket.open(host, port)).to be_instance_of(Mocket)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '.write' do
|
56
|
+
context 'mocket state is open' do
|
57
|
+
it 'returns the number of bytes written' do
|
58
|
+
Mocket.listen(host, port)
|
59
|
+
mocket = Mocket.open(host, port)
|
60
|
+
result = mocket.write(bytes)
|
61
|
+
expect(result).to eq(bytes.size)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'mocket state is closed' do
|
66
|
+
it 'raises IOError: closed stream' do
|
67
|
+
Mocket.listen(host, port)
|
68
|
+
mocket = Mocket.open(host, port)
|
69
|
+
mocket.close
|
70
|
+
expect { mocket.write(bytes) }.to raise_error(IOError, 'closed stream')
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'socket_adapter'
|
2
|
+
|
3
|
+
describe SocketAdapter do
|
4
|
+
let(:host) { '127.0.0.1' }
|
5
|
+
let(:port) { 40000 }
|
6
|
+
let(:socket_adapter) { SocketAdapter.new }
|
7
|
+
|
8
|
+
describe '.setstate' do
|
9
|
+
context 'valid input parameter' do
|
10
|
+
it 'does not raise an error' do
|
11
|
+
expect { socket_adapter.setstate(:live) }.not_to raise_error
|
12
|
+
expect { socket_adapter.setstate(:test) }.not_to raise_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'invalid input parameter' do
|
17
|
+
it 'raises ArgumentError' do
|
18
|
+
expect { socket_adapter.setstate(:other) }.to raise_error(ArgumentError)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '.open' do
|
24
|
+
context 'live mode' do
|
25
|
+
it 'calls TCPSocket#open' do
|
26
|
+
socket_adapter.setstate(:live)
|
27
|
+
TCPSocket = double('TCPSocket')
|
28
|
+
expect(TCPSocket).to receive(:open).with(host, port)
|
29
|
+
socket_adapter.open(host, port)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'test mode' do
|
34
|
+
it 'calls Mocket#open' do
|
35
|
+
socket_adapter.setstate(:test)
|
36
|
+
Mocket = double('Mocket')
|
37
|
+
expect(Mocket).to receive(:open).with(host, port)
|
38
|
+
socket_adapter.open(host, port)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mockets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Closson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Mock socket library for testing network applications
|
70
|
+
email:
|
71
|
+
- matthew.closson@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/mocket.rb
|
82
|
+
- lib/mockets.rb
|
83
|
+
- lib/mockets/version.rb
|
84
|
+
- lib/socket_adapter.rb
|
85
|
+
- mockets.gemspec
|
86
|
+
- spec/mocket_spec.rb
|
87
|
+
- spec/socket_adapter_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
homepage: https://github.com/mclosson/mockets
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.0.3
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Mock socket library
|
113
|
+
test_files:
|
114
|
+
- spec/mocket_spec.rb
|
115
|
+
- spec/socket_adapter_spec.rb
|
116
|
+
- spec/spec_helper.rb
|