mock_server 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mock_server/playback.rb +21 -2
- data/lib/mock_server/record.rb +4 -4
- data/lib/mock_server/spec/helpers.rb +17 -1
- data/lib/mock_server/utils.rb +23 -10
- metadata +6 -6
data/lib/mock_server/playback.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'mock_server/utils'
|
2
|
+
require 'erb'
|
3
|
+
require 'hashie'
|
2
4
|
|
3
5
|
module MockServer
|
4
6
|
class Playback
|
@@ -18,6 +20,10 @@ module MockServer
|
|
18
20
|
lazy_match @options[:routes], env["PATH_INFO"]
|
19
21
|
|
20
22
|
@request = Rack::Request.new(env)
|
23
|
+
|
24
|
+
$mock_server_options[:requests_stack] ||= []
|
25
|
+
$mock_server_options[:requests_stack] << @request.path
|
26
|
+
|
21
27
|
@data = load_data
|
22
28
|
|
23
29
|
record = match_request
|
@@ -57,7 +63,15 @@ module MockServer
|
|
57
63
|
|
58
64
|
matchers.detect { |matcher|
|
59
65
|
if matcher[:matcher]
|
60
|
-
|
66
|
+
result = true
|
67
|
+
begin
|
68
|
+
matcher[:matcher].call(request, recorded_request)
|
69
|
+
rescue => matcher_err
|
70
|
+
store_matcher_exception(matcher_err)
|
71
|
+
result = false
|
72
|
+
ensure
|
73
|
+
result
|
74
|
+
end
|
61
75
|
else
|
62
76
|
true
|
63
77
|
end
|
@@ -65,6 +79,11 @@ module MockServer
|
|
65
79
|
}
|
66
80
|
end
|
67
81
|
|
82
|
+
def store_matcher_exception(exception)
|
83
|
+
$mock_server_options[:matcher_exceptions] ||= []
|
84
|
+
$mock_server_options[:matcher_exceptions] << exception
|
85
|
+
end
|
86
|
+
|
68
87
|
def load_data
|
69
88
|
FileUtils.mkdir_p(@options[:path]) unless File.exists? @options[:path]
|
70
89
|
|
@@ -82,4 +101,4 @@ module MockServer
|
|
82
101
|
end
|
83
102
|
|
84
103
|
end
|
85
|
-
end
|
104
|
+
end
|
data/lib/mock_server/record.rb
CHANGED
@@ -22,19 +22,19 @@ module MockServer
|
|
22
22
|
@request = Rack::Request.new(env)
|
23
23
|
@data = load_data
|
24
24
|
|
25
|
-
@app.call(env).tap do |response|
|
26
|
-
record_response(response)
|
25
|
+
@app.call(env).tap do |status, header, response|
|
26
|
+
record_response(status, header, response)
|
27
27
|
response
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
def record_response(response)
|
33
|
+
def record_response(status, header, response)
|
34
34
|
request = hashified_request
|
35
35
|
@data.reject! { |record| record[:request] == request }
|
36
36
|
|
37
|
-
@data << { :request => request, :response => hashify_response(response) }
|
37
|
+
@data << { :request => request, :response => hashify_response(status, header, response) }
|
38
38
|
save_data(@data)
|
39
39
|
end
|
40
40
|
|
@@ -118,6 +118,13 @@ module MockServer
|
|
118
118
|
end
|
119
119
|
|
120
120
|
# Errors / Success stack
|
121
|
+
def mock_server_requests_stack
|
122
|
+
$mock_server_options[:requests_stack] || []
|
123
|
+
end
|
124
|
+
|
125
|
+
def mock_server_requests_stack_clear!
|
126
|
+
$mock_server_options[:requests_stack] = []
|
127
|
+
end
|
121
128
|
|
122
129
|
def mock_server_errors_stack
|
123
130
|
$mock_server_options[:errors_stack] || []
|
@@ -136,6 +143,7 @@ module MockServer
|
|
136
143
|
end
|
137
144
|
|
138
145
|
def mock_server_response_stack_clear!
|
146
|
+
mock_server_requests_stack_clear!
|
139
147
|
mock_server_success_stack_clear!
|
140
148
|
mock_server_errors_stack_clear!
|
141
149
|
end
|
@@ -148,7 +156,15 @@ module MockServer
|
|
148
156
|
mock_server_disable_all_routes!
|
149
157
|
end
|
150
158
|
|
159
|
+
def mock_server_reraise_matcher_exceptions
|
160
|
+
$mock_server_options[:matcher_exceptions] ||= []
|
161
|
+
$mock_server_options[:matcher_exceptions].each do |exception|
|
162
|
+
raise exception
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
151
166
|
protected
|
167
|
+
|
152
168
|
# Configuration
|
153
169
|
def mock_server_config_set(key, value)
|
154
170
|
$mock_server_options ||= {}
|
@@ -173,4 +189,4 @@ module MockServer
|
|
173
189
|
|
174
190
|
end
|
175
191
|
end
|
176
|
-
end
|
192
|
+
end
|
data/lib/mock_server/utils.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'json'
|
3
|
+
|
1
4
|
module MockServer
|
2
5
|
module Utils
|
3
6
|
|
@@ -22,27 +25,37 @@ module MockServer
|
|
22
25
|
end
|
23
26
|
|
24
27
|
def hashified_request
|
25
|
-
|
28
|
+
#rewind to ensure we read from the start
|
29
|
+
@request.body.rewind
|
30
|
+
|
31
|
+
#read body
|
32
|
+
body = @request.body.read
|
33
|
+
|
34
|
+
#rewind in case upstream expects it rewound
|
35
|
+
@request.body.rewind
|
36
|
+
|
37
|
+
json = JSON.parse(body) rescue body
|
38
|
+
|
26
39
|
{
|
27
40
|
:method => @request.request_method,
|
28
41
|
:path => @request.path,
|
29
42
|
:query => @request.query_string,
|
30
|
-
:body =>
|
43
|
+
:body => json
|
31
44
|
}
|
32
45
|
end
|
33
46
|
|
34
|
-
def hashify_response(response)
|
35
|
-
status = response[0]
|
36
|
-
headers = response[1]
|
37
|
-
body = response[2].body rescue ''
|
38
|
-
|
47
|
+
def hashify_response(status, header, response)
|
39
48
|
{
|
40
49
|
:method => @request.request_method,
|
41
50
|
:path => @request.path,
|
42
51
|
:status => status,
|
43
|
-
:headers =>
|
44
|
-
:body => body
|
52
|
+
:headers => header,
|
53
|
+
:body => if response.respond_to? :body
|
54
|
+
response.body
|
55
|
+
else
|
56
|
+
response.join
|
57
|
+
end
|
45
58
|
}
|
46
59
|
end
|
47
60
|
end
|
48
|
-
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mock_server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
16
|
-
requirement: &
|
16
|
+
requirement: &70339623889120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70339623889120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: hashie
|
27
|
-
requirement: &
|
27
|
+
requirement: &70339623902520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70339623902520
|
36
36
|
description:
|
37
37
|
email: unixcharles@gmail.com
|
38
38
|
executables: []
|