mocking-server 0.0.1
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.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +77 -0
- data/lib/mocking_server.rb +75 -0
- data/spec/mocking_server_spec.rb +45 -0
- metadata +61 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c9aef0d56c7fba6c2ed66e05acff6f5a6716a491
|
|
4
|
+
data.tar.gz: 301532b3ae35c6612493ca2d3b6fcace6e19f8ce
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1e1a2a93363d10ce1ac039cd5948a11836ddb4654ef9de4b934f1ef9139066786a9ebdef35830231eb1be1538e46245977250881c1b5940e4cdca531584e5f20
|
|
7
|
+
data.tar.gz: f6a5aa78f6038f343b0df6cf76f99923c3b7f68b4ce70825f892d9f1fbfb48195287b09133060d45788af89a97b6578609a1b6201480c6b1df35ec3a7de3b7bc
|
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 Nicholas Ren
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Mockery #
|
|
2
|
+
A quick way of mocking an external web service you want to consume.
|
|
3
|
+
Inspired by [moco](https://github.com/dreamhead/moco), [mock-server](https://github.com/djanowski/mock-server) and [sinatra](http://www.sinatrarb.com/).
|
|
4
|
+
|
|
5
|
+
# Why #
|
|
6
|
+
Integration, especially based on HTTP, e.g. web service, REST etc, is widlly used in most web development.
|
|
7
|
+
When you write a feature that need to connect to an external web service
|
|
8
|
+
You wonder how to test that. One option is to stub methods in Net::HTTP and equivalents, but by doing that you
|
|
9
|
+
are tying yourself to an implementation detail. The ideal thing is to have a server running locally, Then serve different request for different response.
|
|
10
|
+
|
|
11
|
+
One thing I want to highlight is, rather than set up a global server which respond to all coming request, With `mockery`, you can set up different server for different `testcase`, `spec` or `scenario`.
|
|
12
|
+
|
|
13
|
+
## Feature ##
|
|
14
|
+
+ Standalone server, test the real interaction.
|
|
15
|
+
+ Setup your mock server with sinatra's elegant DSL.
|
|
16
|
+
|
|
17
|
+
## Get it ##
|
|
18
|
+
(haven't published to rubygems)
|
|
19
|
+
`gem install mockery` or add `gem 'mockery'` in your `Gemfile`
|
|
20
|
+
|
|
21
|
+
## With rspec ##
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
# in rspec helper
|
|
25
|
+
require 'mockery'
|
|
26
|
+
RSpec.configure do |config|
|
|
27
|
+
config.include Mockery::Methods
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# in spec
|
|
31
|
+
describe ApiCilent do
|
|
32
|
+
subject { ApiCilent.new }
|
|
33
|
+
|
|
34
|
+
it "should return hello" do
|
|
35
|
+
server = mock_server do
|
|
36
|
+
get '/greeting' do
|
|
37
|
+
status 200
|
|
38
|
+
body 'hello'
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
server.run do
|
|
43
|
+
expect(subject.greeting).to eq('hello')
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### With cucumber ##
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
#in env.rb
|
|
53
|
+
require 'mockery'
|
|
54
|
+
World(Mockery::Methods)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# in steps
|
|
58
|
+
server = mock_server do
|
|
59
|
+
get '/greeting' do
|
|
60
|
+
status 200
|
|
61
|
+
body 'hello'
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
server.run do
|
|
66
|
+
# steps that send request to the local mock sever
|
|
67
|
+
end
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## License ##
|
|
71
|
+
|
|
72
|
+
MIT.
|
|
73
|
+
|
|
74
|
+
## Contributor
|
|
75
|
+
|
|
76
|
+
+ Niu Yameng
|
|
77
|
+
+ Nicholas Ren
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require "sinatra/base"
|
|
2
|
+
require "logger"
|
|
3
|
+
|
|
4
|
+
class MockingServer
|
|
5
|
+
class App < Sinatra::Base
|
|
6
|
+
use Rack::ShowExceptions
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def run
|
|
10
|
+
start
|
|
11
|
+
yield if block_given?
|
|
12
|
+
ensure
|
|
13
|
+
shutdown
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module Methods
|
|
17
|
+
def mock_server(*args, &block)
|
|
18
|
+
app = Class.new(Sinatra::Base)
|
|
19
|
+
app.class_eval(&block)
|
|
20
|
+
MockingServer.new(app, *args, &block)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
protected
|
|
25
|
+
|
|
26
|
+
def listening?(host, port)
|
|
27
|
+
begin
|
|
28
|
+
socket = TCPSocket.new(host, port)
|
|
29
|
+
socket.close unless socket.nil?
|
|
30
|
+
true
|
|
31
|
+
rescue Errno::ECONNREFUSED,
|
|
32
|
+
Errno::EBADF, # Windows
|
|
33
|
+
Errno::EADDRNOTAVAIL # Windows
|
|
34
|
+
false
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def wait_for_service(host, port, timeout = 5)
|
|
39
|
+
start_time = Time.now
|
|
40
|
+
|
|
41
|
+
until listening?(host, port)
|
|
42
|
+
if timeout && (Time.now > (start_time + timeout))
|
|
43
|
+
raise SocketError.new("Socket did not open within #{timeout} seconds")
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
true
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def initialize(app, port = 4000, &block)
|
|
50
|
+
@app = app
|
|
51
|
+
@port = port
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def start
|
|
55
|
+
@pid_server = fork do
|
|
56
|
+
|
|
57
|
+
server_options = {
|
|
58
|
+
:app => @app,
|
|
59
|
+
:server => 'webrick',
|
|
60
|
+
:environment => :none,
|
|
61
|
+
:daemonize => false,
|
|
62
|
+
:Port => @port
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
Rack::Server.start(server_options)
|
|
66
|
+
end
|
|
67
|
+
wait_for_service("0.0.0.0", @port)
|
|
68
|
+
self
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def shutdown
|
|
72
|
+
Process.kill(:INT, @pid_server)
|
|
73
|
+
Process.waitpid(@pid_server)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'net/http'
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
describe 'mock server' do
|
|
6
|
+
let( :http ) { Net::HTTP.new('127.0.0.1', 3000) }
|
|
7
|
+
let( :http2 ) { Net::HTTP.new('127.0.0.1', 4000) }
|
|
8
|
+
|
|
9
|
+
it "should work" do
|
|
10
|
+
server = mock_server(3000) do
|
|
11
|
+
get '/user' do
|
|
12
|
+
'hello'
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
server.run do
|
|
17
|
+
http.request_get('/user').body.should eq("hello")
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "should work as well" do
|
|
22
|
+
server = mock_server(3000) do
|
|
23
|
+
get '/category' do
|
|
24
|
+
'hello'
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
server.run do
|
|
29
|
+
http.request_get('/category').body.should eq("hello")
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "should work as well 2" do
|
|
34
|
+
server = mock_server(4000) do
|
|
35
|
+
get '/category' do
|
|
36
|
+
'hello'
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
server.run do
|
|
41
|
+
http2.request_get('/category').body.should eq("hello")
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mocking-server
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Nicholas Ren
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-12-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: sinatra
|
|
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:
|
|
29
|
+
- nicholas85211@gmail.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- lib/mocking_server.rb
|
|
35
|
+
- README.md
|
|
36
|
+
- LICENSE
|
|
37
|
+
- spec/mocking_server_spec.rb
|
|
38
|
+
homepage: http://github.com/nicholasren/mocking-server
|
|
39
|
+
licenses: []
|
|
40
|
+
metadata: {}
|
|
41
|
+
post_install_message:
|
|
42
|
+
rdoc_options: []
|
|
43
|
+
require_paths:
|
|
44
|
+
- lib
|
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - '>='
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
requirements: []
|
|
56
|
+
rubyforge_project:
|
|
57
|
+
rubygems_version: 2.0.5
|
|
58
|
+
signing_key:
|
|
59
|
+
specification_version: 4
|
|
60
|
+
summary: A quick way of mocking an external web service you want to consume.
|
|
61
|
+
test_files: []
|