stub_server 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/lib/stub_server.rb +78 -0
- data/lib/stub_server/version.rb +4 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 955f6568b93ddc0eb9c8176802b6d6b3656f0efa
|
4
|
+
data.tar.gz: b74b997143e42a544d44b845eaba4e8e168ceb33
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78883018277395e88f966079b2c36ffb1bdce72a89d56f6f74ff7c1b39e54f8e7282322afbaa95b7114c9b25bc2f5d7740c401b044e0397ac6aadf229d412981
|
7
|
+
data.tar.gz: 5c4a74865266f6bc4ddfd9581d1e79609e004c6f4093b66fd994a056fa752450c5ae99b8ef27c33603c582cbe250e621e285dddfd6c01656a2c528a79a44b019
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (C) 2013 Michael Grosser <michael@grosser.it>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/stub_server.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'rack'
|
3
|
+
require 'webrick'
|
4
|
+
|
5
|
+
class StubServer
|
6
|
+
def self.open(port, replies, **options)
|
7
|
+
server = new(port, replies, **options)
|
8
|
+
server.boot
|
9
|
+
yield server
|
10
|
+
ensure
|
11
|
+
server.shutdown
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(port, replies, ssl: false, json: false, webrick: {})
|
15
|
+
@port = port
|
16
|
+
@replies = replies
|
17
|
+
@ssl = ssl
|
18
|
+
@json = json
|
19
|
+
@webrick = webrick
|
20
|
+
end
|
21
|
+
|
22
|
+
def boot
|
23
|
+
Thread.new do
|
24
|
+
options = {
|
25
|
+
Port: @port,
|
26
|
+
Logger: WEBrick::Log.new("/dev/null"),
|
27
|
+
AccessLog: []
|
28
|
+
}
|
29
|
+
|
30
|
+
if @ssl
|
31
|
+
require 'webrick/https'
|
32
|
+
require 'openssl' # can be slow / break ... so keeping it nested
|
33
|
+
|
34
|
+
options.merge!(
|
35
|
+
SSLEnable: true,
|
36
|
+
SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE,
|
37
|
+
SSLCertificate: OpenSSL::X509::Certificate.new(@ssl.fetch(:cert)),
|
38
|
+
SSLPrivateKey: OpenSSL::PKey::RSA.new(@ssl.fetch(:key)),
|
39
|
+
SSLCertName: [["CN", 'not-a-valid-cert']]
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
options.merge!(@webrick)
|
44
|
+
|
45
|
+
Rack::Handler::WEBrick.run(self, options) { |s| @server = s }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def wait
|
50
|
+
Timeout.timeout(10) do
|
51
|
+
loop do
|
52
|
+
begin
|
53
|
+
socket = TCPSocket.new('localhost', @port)
|
54
|
+
socket.close if socket
|
55
|
+
sleep 0.1 if ENV['CI']
|
56
|
+
return
|
57
|
+
rescue Errno::ECONNREFUSED
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def call(env)
|
65
|
+
path = env.fetch("PATH_INFO")
|
66
|
+
code, headers, body = @replies[path]
|
67
|
+
unless code
|
68
|
+
warn "StubServer: Missing reply for path #{path}" # some clients does not show current url when failing
|
69
|
+
raise
|
70
|
+
end
|
71
|
+
body = [body.to_json] if @json
|
72
|
+
[code, headers, body]
|
73
|
+
end
|
74
|
+
|
75
|
+
def shutdown
|
76
|
+
@server.shutdown if @server
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stub_server
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Grosser
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email: michael@grosser.it
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- MIT-LICENSE
|
34
|
+
- lib/stub_server.rb
|
35
|
+
- lib/stub_server/version.rb
|
36
|
+
homepage: https://github.com/grosser/stub_server
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.0.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.4.5.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Boot up a real server to serve testing replies
|
60
|
+
test_files: []
|