mock_server 0.1.5 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mock_server/playback.rb +21 -15
- data/lib/mock_server/record.rb +12 -8
- data/lib/mock_server/spec/helpers.rb +44 -92
- data/lib/mock_server/state.rb +31 -0
- data/lib/mock_server/store/global.rb +22 -0
- data/lib/mock_server/utils.rb +3 -5
- metadata +9 -6
data/lib/mock_server/playback.rb
CHANGED
@@ -1,45 +1,52 @@
|
|
1
|
-
require 'mock_server/utils'
|
2
1
|
require 'erb'
|
3
2
|
require 'hashie'
|
4
3
|
|
4
|
+
require_relative 'utils'
|
5
|
+
require_relative 'state'
|
6
|
+
|
7
|
+
unless defined? MockServer::Store
|
8
|
+
require_relative 'store/global'
|
9
|
+
end
|
10
|
+
|
5
11
|
module MockServer
|
6
12
|
class Playback
|
7
13
|
include MockServer::Utils
|
14
|
+
include MockServer::Store
|
8
15
|
|
9
|
-
def initialize(app,
|
10
|
-
@options = options
|
16
|
+
def initialize(app, opt = {})
|
11
17
|
@app = app
|
12
|
-
|
18
|
+
@options = mock_server_options_merge(opt)
|
13
19
|
end
|
14
20
|
|
15
21
|
def call(env)
|
16
|
-
@options.
|
22
|
+
@options = self.mock_server_options_read
|
17
23
|
|
18
|
-
verbose(env
|
24
|
+
verbose(env) if @options[:verbose]
|
19
25
|
return @app.call(env) unless @options[:routes] and
|
20
26
|
lazy_match @options[:routes], env["PATH_INFO"]
|
21
27
|
|
22
28
|
@request = Rack::Request.new(env)
|
23
29
|
|
24
|
-
|
25
|
-
$mock_server_options[:requests_stack] << @request.path
|
30
|
+
@options[:requests_stack] << @request.path
|
26
31
|
|
27
32
|
@data = load_data
|
28
33
|
|
29
34
|
record = match_request
|
30
35
|
|
31
|
-
if record
|
32
|
-
|
33
|
-
|
36
|
+
response = if record
|
37
|
+
@options[:success_stack] << @request.path
|
38
|
+
@options[:matcher_exceptions].clear
|
34
39
|
|
35
40
|
response = record[:response]
|
36
41
|
[response[:status], response[:headers], [response[:body]]]
|
37
42
|
else
|
38
|
-
$mock_server_options[:errors_stack] ||= []
|
39
43
|
error = { @request.path => "Couldn't match #{@request.request_method} #{@request.path}" }
|
40
|
-
|
44
|
+
@options[:errors_stack] << error
|
41
45
|
[404, {}, ['RECORD NOT FOUND!']]
|
42
46
|
end
|
47
|
+
|
48
|
+
self.mock_server_options_write(@options)
|
49
|
+
response
|
43
50
|
end
|
44
51
|
|
45
52
|
private
|
@@ -80,8 +87,7 @@ module MockServer
|
|
80
87
|
end
|
81
88
|
|
82
89
|
def store_matcher_exception(exception)
|
83
|
-
|
84
|
-
$mock_server_options[:matcher_exceptions] << exception
|
90
|
+
@options[:matcher_exceptions] << exception
|
85
91
|
end
|
86
92
|
|
87
93
|
def load_data
|
data/lib/mock_server/record.rb
CHANGED
@@ -1,29 +1,33 @@
|
|
1
|
-
|
1
|
+
require_relative 'utils'
|
2
|
+
require_relative 'state'
|
3
|
+
|
4
|
+
unless defined? MockServer::Store
|
5
|
+
require_relative 'store/global'
|
6
|
+
end
|
2
7
|
|
3
8
|
module MockServer
|
4
9
|
class Record
|
5
10
|
include MockServer::Utils
|
11
|
+
include MockServer::Store
|
6
12
|
|
7
|
-
def initialize(app,
|
8
|
-
@options = options
|
13
|
+
def initialize(app, opt = {})
|
9
14
|
@app = app
|
10
|
-
|
15
|
+
@options = mock_server_options_merge(opt)
|
11
16
|
end
|
12
17
|
|
13
18
|
def call(env)
|
14
|
-
@options.
|
19
|
+
@options = self.mock_server_options_read
|
15
20
|
|
16
|
-
verbose(env
|
21
|
+
verbose(env) if @options[:verbose]
|
17
22
|
return @app.call(env) unless @options[:routes] and
|
18
23
|
lazy_match @options[:routes], env["PATH_INFO"]
|
19
24
|
|
20
|
-
@options.merge!($mock_server_options)
|
21
|
-
|
22
25
|
@request = Rack::Request.new(env)
|
23
26
|
@data = load_data
|
24
27
|
|
25
28
|
@app.call(env).tap do |status, header, response|
|
26
29
|
record_response(status, header, response)
|
30
|
+
self.mock_server_options_write(@options)
|
27
31
|
response
|
28
32
|
end
|
29
33
|
end
|
@@ -1,99 +1,44 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
#
|
5
|
-
# The basic:
|
6
|
-
#
|
7
|
-
# $mock_server_options = { :path => 'fixtures/records', :filename => 'uploads' }
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# $mock_server_options tell you application which URL to do its magic for both,
|
11
|
-
# record and playback mode.
|
12
|
-
#
|
13
|
-
# Example:
|
14
|
-
#
|
15
|
-
# $mock_server_options[:routes] = [ '**/uploads.json', '**/uploads/*',
|
16
|
-
# '**/folders.json', '**/folders', '**/folders' ]
|
17
|
-
#
|
18
|
-
# $mock_server_options[:matchers] is an array of path, url, and matchers proc
|
19
|
-
#
|
20
|
-
# Example:
|
21
|
-
#
|
22
|
-
# $mock_server_options[:matchers] = [
|
23
|
-
# { :method => :get, :path => '/api/2/projects/1/uploads.json',
|
24
|
-
# :matcher => Proc.new { |request, recorded_request|
|
25
|
-
# request.query == recorded_request.query and
|
26
|
-
# request.query == 'count=0'
|
27
|
-
# }
|
28
|
-
# },
|
29
|
-
# { :method => :put, :path => '/api/2/uploads/20',
|
30
|
-
# :matcher => Proc.new { |request, recorded_request|
|
31
|
-
# request.query == recorded_request.query and
|
32
|
-
# request.query == 'count=0'
|
33
|
-
# }
|
34
|
-
# },
|
35
|
-
# { :method => :delete, :path => '/api/2/uploads/20' },
|
36
|
-
# { :method => :get, :path => '/api/2/projects/1/folders.json' },
|
37
|
-
# { :method => :post, :path => '/api/2/folders',
|
38
|
-
# :matcher => Proc.new { |request, recorded_request|
|
39
|
-
# request.body.project_id == recorded_request.body.project_id and
|
40
|
-
# request.body.project_id == 1 and
|
41
|
-
# recorded_request.body.name == recorded_request.body.name and
|
42
|
-
# recorded_request.body.name == 'Mockup'
|
43
|
-
# }
|
44
|
-
# },
|
45
|
-
# { :method => :put, :path => '/api/2/uploads/20',
|
46
|
-
# :matcher => Proc.new { |request, recorded_request|
|
47
|
-
# request.body.name == recorded_request.body.name
|
48
|
-
# }
|
49
|
-
# },
|
50
|
-
# { :method => :delete, :path => '/api/2/folders/3' }
|
51
|
-
# ]
|
52
|
-
|
53
|
-
|
1
|
+
unless defined? MockServer::Store
|
2
|
+
require_relative 'Store/global'
|
3
|
+
end
|
54
4
|
|
55
5
|
module MockServer
|
56
6
|
module Spec
|
57
7
|
module Helpers
|
8
|
+
include MockServer::Store
|
58
9
|
|
59
10
|
# Inspect
|
60
11
|
def mock_server_inspect
|
61
|
-
|
12
|
+
mock_server_options.inspect
|
62
13
|
end
|
63
14
|
|
64
15
|
# Configuration
|
65
16
|
def mock_server_use_record(*arguments)
|
66
|
-
|
17
|
+
mock_server_options_set(:record_filenames, arguments)
|
67
18
|
end
|
68
19
|
|
69
20
|
def mock_server_add_record(*arguments)
|
70
|
-
config = (
|
71
|
-
|
21
|
+
config = (mock_server_options_fetch(:record_filenames, []) + arguments)
|
22
|
+
mock_server_options_set(:record_filenames, config)
|
72
23
|
end
|
73
24
|
|
74
25
|
def mock_server_set_fixture_path(path)
|
75
|
-
|
26
|
+
mock_server_options_set(:path, path)
|
76
27
|
end
|
77
28
|
|
78
29
|
# Active path config
|
79
30
|
def mock_server_enable_routes(*arguments)
|
80
|
-
|
81
|
-
|
82
|
-
$mock_server_options[:routes] << arguments
|
83
|
-
$mock_server_options[:routes].flatten!
|
31
|
+
routes = mock_server_options_fetch(:routes, []) + arguments
|
32
|
+
mock_server_options_set(:routes, routes)
|
84
33
|
end
|
85
34
|
|
86
35
|
def mock_server_disable_path(path)
|
87
|
-
|
88
|
-
|
89
|
-
path unless path.empty?
|
90
|
-
else
|
91
|
-
$mock_server_options[:routes] - [path]
|
92
|
-
end
|
36
|
+
routes = mock_server_options_fetch(:routes, []) - [path]
|
37
|
+
mock_server_options_set(:routes, routes.flatten!)
|
93
38
|
end
|
94
39
|
|
95
40
|
def mock_server_disable_all_routes!
|
96
|
-
|
41
|
+
mock_server_options_set(:routes, [])
|
97
42
|
end
|
98
43
|
|
99
44
|
# Matchers helpers
|
@@ -114,32 +59,32 @@ module MockServer
|
|
114
59
|
end
|
115
60
|
|
116
61
|
def mock_server_clear_matchers!
|
117
|
-
|
62
|
+
mock_server_options_set(:matchers, [])
|
118
63
|
end
|
119
64
|
|
120
65
|
# Errors / Success stack
|
121
66
|
def mock_server_requests_stack
|
122
|
-
|
67
|
+
mock_server_options_fetch(:requests_stack, [])
|
123
68
|
end
|
124
69
|
|
125
70
|
def mock_server_requests_stack_clear!
|
126
|
-
|
71
|
+
mock_server_options_set(:requests_stack, [])
|
127
72
|
end
|
128
73
|
|
129
74
|
def mock_server_errors_stack
|
130
|
-
|
75
|
+
mock_server_options_fetch(:errors_stack, [])
|
131
76
|
end
|
132
77
|
|
133
78
|
def mock_server_errors_stack_clear!
|
134
|
-
|
79
|
+
mock_server_options_set(:errors_stack, [])
|
135
80
|
end
|
136
81
|
|
137
82
|
def mock_server_success_stack
|
138
|
-
|
83
|
+
mock_server_options_fetch(:success_stack, [])
|
139
84
|
end
|
140
85
|
|
141
86
|
def mock_server_success_stack_clear!
|
142
|
-
|
87
|
+
mock_server_options_set(:success_stack, [])
|
143
88
|
end
|
144
89
|
|
145
90
|
def mock_server_response_stack_clear!
|
@@ -157,34 +102,41 @@ module MockServer
|
|
157
102
|
end
|
158
103
|
|
159
104
|
def mock_server_reraise_matcher_exceptions
|
160
|
-
|
161
|
-
$mock_server_options[:matcher_exceptions].each do |exception|
|
105
|
+
mock_server_options_fetch(:matcher_exceptions, []).each do |exception|
|
162
106
|
raise exception
|
163
107
|
end
|
164
108
|
end
|
165
109
|
|
166
110
|
protected
|
167
111
|
|
168
|
-
# Configuration
|
169
|
-
def mock_server_config_set(key, value)
|
170
|
-
$mock_server_options ||= {}
|
171
|
-
$mock_server_options[key] = value
|
172
|
-
end
|
173
|
-
|
174
|
-
def mock_server_config_get(key)
|
175
|
-
$mock_server_options ||= {}
|
176
|
-
$mock_server_options[key]
|
177
|
-
end
|
178
|
-
|
179
112
|
# Matchers helpers
|
180
113
|
def mock_server_request(method, path, matcher)
|
181
114
|
add_mock_server_matcher({ :method => method, :path => path, :matcher => matcher })
|
182
115
|
end
|
183
116
|
|
184
117
|
def add_mock_server_matcher(matcher)
|
185
|
-
|
186
|
-
|
187
|
-
|
118
|
+
options = self.mock_server_options_read
|
119
|
+
options[:matchers] << matcher
|
120
|
+
mock_server_options_write(options)
|
121
|
+
end
|
122
|
+
|
123
|
+
def mock_server_options_fetch(key, value)
|
124
|
+
if self.mock_server_options_read[key]
|
125
|
+
mock_server_options_get(key)
|
126
|
+
else
|
127
|
+
mock_server_options_set(key, value)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def mock_server_options_set(key, value)
|
132
|
+
hash = self.mock_server_options_read
|
133
|
+
hash[key] = value
|
134
|
+
self.mock_server_options_write(hash)
|
135
|
+
end
|
136
|
+
|
137
|
+
def mock_server_options_get(key)
|
138
|
+
hash = self.mock_server_options_read
|
139
|
+
hash[key]
|
188
140
|
end
|
189
141
|
|
190
142
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module MockServer
|
2
|
+
class State < Hashie::Dash
|
3
|
+
property :path, :default => 'fixtures/records'
|
4
|
+
property :filename, :default => 'record'
|
5
|
+
property :routes, :default => []
|
6
|
+
property :record_filenames, :default => []
|
7
|
+
property :matchers, :default => []
|
8
|
+
property :verbose, :default => false
|
9
|
+
property :requests_stack, :default => []
|
10
|
+
property :success_stack, :default => []
|
11
|
+
property :errors_stack, :default => []
|
12
|
+
property :requests_stack, :default => []
|
13
|
+
property :matcher_exceptions, :default => []
|
14
|
+
|
15
|
+
def merge(hash)
|
16
|
+
new_hash = self
|
17
|
+
hash.each do |k,v|
|
18
|
+
new_hash[k] = v
|
19
|
+
end
|
20
|
+
new_hash
|
21
|
+
end
|
22
|
+
|
23
|
+
def merge!(hash)
|
24
|
+
hash.each do |k,v|
|
25
|
+
self[k] = v
|
26
|
+
end
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
|
3
|
+
module MockServer
|
4
|
+
module Store
|
5
|
+
|
6
|
+
def mock_server_options_merge(opt = {})
|
7
|
+
$mock_server_options ||= MockServer::State.new(opt)
|
8
|
+
$mock_server_options.merge!(opt)
|
9
|
+
end
|
10
|
+
|
11
|
+
def mock_server_options_read
|
12
|
+
$mock_server_options ||= MockServer::State.new
|
13
|
+
$mock_server_options
|
14
|
+
end
|
15
|
+
|
16
|
+
def mock_server_options_write(value)
|
17
|
+
$mock_server_options ||= MockServer::State.new
|
18
|
+
$mock_server_options = value
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/lib/mock_server/utils.rb
CHANGED
@@ -6,11 +6,9 @@ module MockServer
|
|
6
6
|
|
7
7
|
private
|
8
8
|
|
9
|
-
def verbose(env
|
10
|
-
|
11
|
-
|
12
|
-
puts %([MockServer] #{env["PATH_INFO"]} was #{interception}"\n)
|
13
|
-
end
|
9
|
+
def verbose(env)
|
10
|
+
interception = lazy_match(@options[:routes], env["PATH_INFO"]) ? "intercepted!" : "NOT intercepted."
|
11
|
+
puts %([MockServer] #{env["PATH_INFO"]} was #{interception}"\n)
|
14
12
|
end
|
15
13
|
|
16
14
|
def lazy_match(strings, path)
|
metadata
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mock_server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Charles Barbier
|
9
|
+
- Saimon Moore
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
13
|
+
date: 2011-11-21 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rack
|
16
|
-
requirement: &
|
17
|
+
requirement: &70185898662840 !ruby/object:Gem::Requirement
|
17
18
|
none: false
|
18
19
|
requirements:
|
19
20
|
- - ! '>='
|
@@ -21,10 +22,10 @@ dependencies:
|
|
21
22
|
version: '0'
|
22
23
|
type: :runtime
|
23
24
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
+
version_requirements: *70185898662840
|
25
26
|
- !ruby/object:Gem::Dependency
|
26
27
|
name: hashie
|
27
|
-
requirement: &
|
28
|
+
requirement: &70185898662400 !ruby/object:Gem::Requirement
|
28
29
|
none: false
|
29
30
|
requirements:
|
30
31
|
- - ! '>='
|
@@ -32,7 +33,7 @@ dependencies:
|
|
32
33
|
version: '0'
|
33
34
|
type: :runtime
|
34
35
|
prerelease: false
|
35
|
-
version_requirements: *
|
36
|
+
version_requirements: *70185898662400
|
36
37
|
description:
|
37
38
|
email: unixcharles@gmail.com
|
38
39
|
executables: []
|
@@ -45,6 +46,8 @@ files:
|
|
45
46
|
- lib/mock_server/record.rb
|
46
47
|
- lib/mock_server/playback.rb
|
47
48
|
- lib/mock_server/utils.rb
|
49
|
+
- lib/mock_server/state.rb
|
50
|
+
- lib/mock_server/store/global.rb
|
48
51
|
- lib/mock_server/spec/helpers.rb
|
49
52
|
homepage: http://www.github.com/unixcharles/mock_server
|
50
53
|
licenses: []
|