mockserver-client 0.0.1 → 0.0.3
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 +8 -8
- data/README.md +58 -0
- data/Rakefile +0 -1
- data/bin/mockserver +9 -0
- data/lib/cli.rb +146 -0
- data/lib/mockserver/abstract_client.rb +19 -9
- data/lib/mockserver/mock_server_client.rb +1 -5
- data/lib/mockserver/model/array_of.rb +1 -1
- data/lib/mockserver/model/expectation.rb +17 -42
- data/lib/mockserver/model/mock.rb +123 -0
- data/lib/mockserver/model/response.rb +5 -0
- data/lib/mockserver/utility_methods.rb +16 -3
- data/lib/mockserver/version.rb +1 -1
- data/mockserver-client.gemspec +3 -1
- data/spec/mockserver/builder_spec.rb +8 -0
- data/spec/mockserver/proxy_client_spec.rb +3 -2
- data/spec/spec_helper.rb +1 -1
- metadata +36 -4
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MDRhZTQwOTYwOTZjZjQ3ZThlNWFlZTk0NjIwMjIzZjUwMzRiY2UxMA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MGM5MWMyZWYzYzJjZTU5Y2NjZDhiNjM2YWI0OWM1MTU4ZWE1OWYzNg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzlmZDZlYTcyMzQ0YWFkOWViMmI0ZmFlZDgwODdmM2ZhM2QyYWIwZWRkYThj
|
10
|
+
MmU2YzkyNDEyZDI3NWZiN2NkNjc1Mzk5NGJhYmI1ZTU3YTdiYmEzYTI3NzM5
|
11
|
+
ZGEwMWRkYWUwYWQ2MjQwZjM0YWUwZTQ4MWUzOWYxMTc4MGVmMmQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MzljZWQ0MjNkMGJiOGFkMjM1N2FlM2MzYjczOWQxODZlOGJhYWFmODg4NDg3
|
14
|
+
MjBlMmYyMGI2NGI3ZTA0MjdhNWNhMDUxN2QwYTZmOTBmODI4NWZhZjA1ZDUz
|
15
|
+
NGRjYzhjOGRmN2VlNGQ1YWMxMGEzNjk2NTEwMjg2MGNlZjQ1YzQ=
|
data/README.md
CHANGED
@@ -190,6 +190,64 @@ Expectation (use in register)
|
|
190
190
|
* **expectation**: Create an expectation object. If a block is passed, will configure the object first before returning it. Example: `expectation {|e| e.request {|r| r.path = "index.html} }`.
|
191
191
|
Getter methods for `request`, `response` and `forward` methods will optionally accept a block. If block is passed object is configured before it is returned. The attribute `times` has conventional getter and setter methods.
|
192
192
|
|
193
|
+
## CLI
|
194
|
+
|
195
|
+
This gem comes with a command line interface which allow you to run the Mock Server calls from the command line. When this gem is installed, you will have the `mockserver` executable on your PATH. Type `mockserver --help` and you will get this output:
|
196
|
+
|
197
|
+
```
|
198
|
+
mockserver --help
|
199
|
+
|
200
|
+
Commands:
|
201
|
+
mockserver clear # Clears all stored mock request/responses from server.
|
202
|
+
mockserver dump_log # Dumps the matching request to the mock server logs.
|
203
|
+
mockserver help [COMMAND] # Describe available commands or one specific command
|
204
|
+
mockserver register # Register an expectation with the mock server.
|
205
|
+
mockserver reset # Resets the server clearing all data.
|
206
|
+
mockserver retrieve # Retrieve the list of requests that have been made to the mock/proxy server.
|
207
|
+
mockserver verify # Verify that a request has been made the specified number of times to the server.
|
208
|
+
|
209
|
+
Options:
|
210
|
+
-h, --host=HOST # The host for the MockServer client.
|
211
|
+
# Default: localhost
|
212
|
+
-p, --port=N # The port for the MockServer client.
|
213
|
+
# Default: 8080
|
214
|
+
-d, [--data=DATA] # A JSON or YAML file containing the request payload.
|
215
|
+
```
|
216
|
+
|
217
|
+
To get help for an individual command, e.g. `dump_log`, you would do:
|
218
|
+
|
219
|
+
```
|
220
|
+
mockserver --help dump_log
|
221
|
+
|
222
|
+
Usage:
|
223
|
+
mockserver dump_log
|
224
|
+
|
225
|
+
Options:
|
226
|
+
-j, [--java], [--no-java] # A switch to turn Java format for logs on/off.
|
227
|
+
-h, --host=HOST # The host for the MockServer client.
|
228
|
+
# Default: localhost
|
229
|
+
-p, --port=N # The port for the MockServer client.
|
230
|
+
# Default: 8080
|
231
|
+
-d, [--data=DATA] # A JSON or YAML file containing the request payload.
|
232
|
+
|
233
|
+
Dumps the matching request to the mock server logs.
|
234
|
+
```
|
235
|
+
|
236
|
+
Here is an example on how you would run the command:
|
237
|
+
|
238
|
+
```
|
239
|
+
mockserver dump_log -j true
|
240
|
+
|
241
|
+
Running with parameters:
|
242
|
+
host: localhost
|
243
|
+
port: 8080
|
244
|
+
java: true
|
245
|
+
|
246
|
+
[2014-06-21 09:23:32] DEBUG [MockServerClient] Sending dump log request to mockserver
|
247
|
+
[2014-06-21 09:23:32] DEBUG [MockServerClient] URL: /dumpToLog?type=java. Payload: {}
|
248
|
+
[2014-06-21 09:23:32] DEBUG [MockServerClient] Got dump to log response: 202
|
249
|
+
```
|
250
|
+
|
193
251
|
## Contributing
|
194
252
|
|
195
253
|
1. Fork it ( https://github.com/[my-github-username]/mockserver-client/fork )
|
data/Rakefile
CHANGED
data/bin/mockserver
ADDED
data/lib/cli.rb
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'thor'
|
3
|
+
require 'colorize'
|
4
|
+
require_relative './mockserver-client'
|
5
|
+
|
6
|
+
# CLI for this gem
|
7
|
+
# @author Nayyara Samuel(nayyara.samuel@opower.com)
|
8
|
+
#
|
9
|
+
module CLIHelpers
|
10
|
+
include MockServer
|
11
|
+
|
12
|
+
LOGGER = LoggingFactory::DEFAULT_FACTORY.log('MockServerClient')
|
13
|
+
|
14
|
+
# Prints out the parameters passed to it
|
15
|
+
# @param options [Hash] a hash of parameters
|
16
|
+
def print_parameters(options)
|
17
|
+
puts "\nRunning with parameters:".bold
|
18
|
+
options.each { |k, v| puts "\t#{k}: #{v}".yellow }
|
19
|
+
puts ''
|
20
|
+
end
|
21
|
+
|
22
|
+
# Create a mockserver client
|
23
|
+
# @param options [Struct] with host and port set
|
24
|
+
# @return [MockServerClient] the mockserver client with the host and port
|
25
|
+
def mockserver_client(options)
|
26
|
+
client = MockServerClient.new(options.host, options.port)
|
27
|
+
client.logger = LOGGER
|
28
|
+
client
|
29
|
+
end
|
30
|
+
|
31
|
+
# Create a proxy client
|
32
|
+
# @param options [Struct] with host and port set
|
33
|
+
# @return [ProxyClient] the proxy client with the host and port
|
34
|
+
def proxy_client(options)
|
35
|
+
client = ProxyClient.new(options.host, options.port)
|
36
|
+
client.logger = LOGGER
|
37
|
+
client
|
38
|
+
end
|
39
|
+
|
40
|
+
# Convert a hash to a struct
|
41
|
+
# @param hash [Hash] a hash
|
42
|
+
# @return [Struct] a struct constructed from the hash
|
43
|
+
def to_struct(hash)
|
44
|
+
hash = symbolize_keys(hash)
|
45
|
+
Struct.new(*hash.keys).new(*hash.values)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Process a block using options extracted into a struct
|
49
|
+
# @param mockserver [Boolean] true to use mockserver, false to use proxy
|
50
|
+
# @yieldparam [AbstractClient] a mockserver or a proxy client
|
51
|
+
# @yieldparam [Struct] a struct created from options hash
|
52
|
+
def execute_command(mockserver = false, data_required = false, error_msg = '--data option must be provided', &_)
|
53
|
+
print_parameters(options)
|
54
|
+
struct_options = to_struct({ data: nil }.merge(options))
|
55
|
+
if data_required && !options['data']
|
56
|
+
error(error_msg)
|
57
|
+
else
|
58
|
+
client = mockserver ? mockserver_client(struct_options) : proxy_client(struct_options)
|
59
|
+
yield client, struct_options if block_given?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Prints an error message
|
64
|
+
# @param message [String] an error message
|
65
|
+
def error(message)
|
66
|
+
puts message.red
|
67
|
+
end
|
68
|
+
|
69
|
+
# Read a file
|
70
|
+
# @param file [String] a file to read
|
71
|
+
def read_file(file)
|
72
|
+
YAML.load_file(file)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# CLI for mock server and proxy clients
|
77
|
+
class MockServerCLI < Thor
|
78
|
+
include CLIHelpers
|
79
|
+
include MockServer::UtilityMethods
|
80
|
+
include MockServer::Model::DSL
|
81
|
+
|
82
|
+
class_option :host, type: :string, aliases: '-h', required: true, default: 'localhost', desc: 'The host for the MockServer client.'
|
83
|
+
class_option :port, type: :numeric, aliases: '-p', required: true, default: 8080, desc: 'The port for the MockServer client.'
|
84
|
+
class_option :data, type: :string, aliases: '-d', desc: 'A JSON or YAML file containing the request payload.'
|
85
|
+
|
86
|
+
desc 'retrieve', 'Retrieve the list of requests that have been made to the mock/proxy server.'
|
87
|
+
|
88
|
+
def retrieve
|
89
|
+
execute_command do |client, _|
|
90
|
+
result = options.data ? client.retrieve(read_file(options.data)) : client.retrieve
|
91
|
+
puts "RESULT:\n".bold + "#{result.to_json}".green
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
desc 'register', 'Register an expectation with the mock server.'
|
96
|
+
|
97
|
+
def register
|
98
|
+
execute_command(true, true) do |client, options|
|
99
|
+
payload = read_file(options.data)
|
100
|
+
mock_expectation = expectation do |expectation|
|
101
|
+
expectation.populate_from_payload(payload)
|
102
|
+
end
|
103
|
+
client.register(mock_expectation)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
desc 'dump_log', 'Dumps the matching request to the mock server logs.'
|
108
|
+
option :java, type: :boolean, aliases: '-j', default: false, desc: 'A switch to turn Java format for logs on/off.'
|
109
|
+
|
110
|
+
def dump_log
|
111
|
+
execute_command do |client, options|
|
112
|
+
options.data ? client.dump_log(read_file(options.data), options.java) : client.dump_log(nil, options.java)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
desc 'clear', 'Clears all stored mock request/responses from server.'
|
117
|
+
|
118
|
+
def clear
|
119
|
+
error_message = 'ERROR: No request provided. HINT: Use `clear` to selectively clear requests. Use `reset` to clear all.'
|
120
|
+
execute_command(false, true, error_message) do |client, _|
|
121
|
+
payload = read_file(options.data)
|
122
|
+
client.clear(payload)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
desc 'reset', 'Resets the server clearing all data.'
|
127
|
+
|
128
|
+
def reset
|
129
|
+
execute_command do |client, _|
|
130
|
+
client.reset
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
desc 'verify', 'Verify that a request has been made the specified number of times to the server.'
|
135
|
+
|
136
|
+
def verify
|
137
|
+
execute_command(false, true) do |client, _|
|
138
|
+
payload = read_file(options.data)
|
139
|
+
mock_request = payload[HTTP_REQUEST]
|
140
|
+
mock_times = payload[HTTP_TIMES]
|
141
|
+
|
142
|
+
error 'No request found for verifying against' unless mock_request
|
143
|
+
mock_times ? client.verify(mock_request, mock_times) : client.verify(mock_request)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -3,16 +3,13 @@ require 'rest-client'
|
|
3
3
|
require 'logging_factory'
|
4
4
|
require_relative './model/times'
|
5
5
|
require_relative './model/request'
|
6
|
+
require_relative './model/expectation'
|
7
|
+
require_relative './utility_methods'
|
6
8
|
|
7
9
|
# An abstract client for making requests supported by mock and proxy client.
|
8
10
|
# @author Nayyara Samuel(mailto: nayyara.samuel@opower.com)
|
9
11
|
#
|
10
12
|
module MockServer
|
11
|
-
HTTP_REQUEST = 'httpRequest'
|
12
|
-
HTTP_RESPONSE = 'httpResponse'
|
13
|
-
HTTP_FORWARD = 'httpForward'
|
14
|
-
HTTP_TIMES = 'times'
|
15
|
-
|
16
13
|
RESET_ENDPOINT = '/reset'
|
17
14
|
CLEAR_ENDPOINT = '/clear'
|
18
15
|
RETRIEVE_ENDPOINT = '/retrieve'
|
@@ -22,6 +19,7 @@ module MockServer
|
|
22
19
|
class AbstractClient
|
23
20
|
include Model::DSL
|
24
21
|
include Model
|
22
|
+
include UtilityMethods
|
25
23
|
|
26
24
|
attr_accessor :logger
|
27
25
|
|
@@ -36,7 +34,7 @@ module MockServer
|
|
36
34
|
# @param request [Request] the request to use to clear an expectation
|
37
35
|
# @return [Object] the response from the clear action
|
38
36
|
def clear(request)
|
39
|
-
request =
|
37
|
+
request = camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request)))
|
40
38
|
url = CLEAR_ENDPOINT
|
41
39
|
|
42
40
|
logger.debug("Clearing expectation with request: #{request}")
|
@@ -65,7 +63,7 @@ module MockServer
|
|
65
63
|
# @param request [Request] to filter requests
|
66
64
|
# @return [Object] the list of responses processed by the server
|
67
65
|
def retrieve(request = nil)
|
68
|
-
request = request ?
|
66
|
+
request = request ? camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request))) : {}
|
69
67
|
url = RETRIEVE_ENDPOINT
|
70
68
|
|
71
69
|
logger.debug('Retrieving request list from mockserver')
|
@@ -73,7 +71,18 @@ module MockServer
|
|
73
71
|
|
74
72
|
response = @base[RETRIEVE_ENDPOINT].put(request.to_json)
|
75
73
|
logger.debug("Got retrieve response: #{response.code}")
|
76
|
-
parse_response(response)
|
74
|
+
results = parse_response(response)
|
75
|
+
mocks = Mocks.new([])
|
76
|
+
unless results.empty?
|
77
|
+
results.each do |result|
|
78
|
+
mock = Mock.new
|
79
|
+
mock.populate_from_payload(result)
|
80
|
+
mocks << mock
|
81
|
+
end
|
82
|
+
end
|
83
|
+
mocks.code = response.code
|
84
|
+
|
85
|
+
mocks
|
77
86
|
end
|
78
87
|
|
79
88
|
# Request to dump logs to file
|
@@ -82,7 +91,7 @@ module MockServer
|
|
82
91
|
def dump_log(request = nil, java = false)
|
83
92
|
type_params = java ? '?type=java' : ''
|
84
93
|
url = "#{DUMP_LOG_ENDPOINT}#{type_params}"
|
85
|
-
request = Request.new(request)
|
94
|
+
request = request ? Request.new(symbolize_keys(request)) : {}
|
86
95
|
|
87
96
|
logger.debug('Sending dump log request to mockserver')
|
88
97
|
logger.debug("URL: #{url}. Payload: #{request.to_hash}")
|
@@ -101,6 +110,7 @@ module MockServer
|
|
101
110
|
results = retrieve(request)
|
102
111
|
|
103
112
|
# Reusing the times model here so interpreting values here
|
113
|
+
times = Times.new(symbolize_keys(times))
|
104
114
|
num_times = times.remaining_times
|
105
115
|
is_exact = !times.unlimited
|
106
116
|
|
@@ -37,11 +37,7 @@ module MockServer
|
|
37
37
|
# @param expectation [Expectation] the expectation to create the request from
|
38
38
|
# @return [Hash] a hash representing the request to use in registering an expectation with the mock server
|
39
39
|
def create_expectation_request(expectation)
|
40
|
-
expectation_request =
|
41
|
-
HTTP_RESPONSE => expectation.response,
|
42
|
-
HTTP_FORWARD => expectation.forward,
|
43
|
-
HTTP_TIMES => expectation.times)
|
44
|
-
|
40
|
+
expectation_request = camelized_hash(expectation)
|
45
41
|
logger.debug("Expectation JSON: #{expectation_request.to_json}")
|
46
42
|
fail "You can only set either of #{[HTTP_RESPONSE, HTTP_FORWARD]}. But not both" if expectation_request[HTTP_RESPONSE] && expectation_request[HTTP_FORWARD]
|
47
43
|
expectation_request
|
@@ -75,7 +75,7 @@ module MockServer::Model
|
|
75
75
|
if item && item.class != child_class
|
76
76
|
begin
|
77
77
|
item = child_class.new(item)
|
78
|
-
rescue e
|
78
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
79
79
|
raise "Failed to convert element: #{item} to required type #{child_class}. Error: #{e.message}"
|
80
80
|
end
|
81
81
|
end
|
@@ -3,58 +3,33 @@ require_relative './request'
|
|
3
3
|
require_relative './response'
|
4
4
|
require_relative './forward'
|
5
5
|
require_relative './times'
|
6
|
-
|
6
|
+
require_relative '../utility_methods'
|
7
|
+
require_relative './mock'
|
7
8
|
#
|
8
9
|
# A class to model an expectation sent to the Mockserver instance.
|
9
10
|
# See http://www.mock-server.com/#create-expectations for details.
|
10
11
|
#
|
11
12
|
# @author:: Nayyara Samuel (mailto: nayyara.samuel@opower.com)
|
12
13
|
#
|
13
|
-
module MockServer
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
# Method to setup the request on the expectation object
|
19
|
-
# @yieldparam [Request] the request that this expectation references
|
20
|
-
# @return [Expectation] this object according to the the builder pattern
|
21
|
-
def request(&_)
|
22
|
-
if block_given?
|
23
|
-
@request ||= Request.new
|
24
|
-
yield @request
|
25
|
-
end
|
26
|
-
@request
|
27
|
-
end
|
14
|
+
module MockServer
|
15
|
+
HTTP_REQUEST = 'httpRequest'
|
16
|
+
HTTP_RESPONSE = 'httpResponse'
|
17
|
+
HTTP_FORWARD = 'httpForward'
|
18
|
+
HTTP_TIMES = 'times'
|
28
19
|
|
29
|
-
|
30
|
-
|
31
|
-
#
|
32
|
-
|
33
|
-
if block_given?
|
34
|
-
@response ||= Response.new
|
35
|
-
yield @response
|
36
|
-
end
|
37
|
-
@response
|
20
|
+
# Models
|
21
|
+
module Model
|
22
|
+
# Expectation model
|
23
|
+
class Expectation < Mock
|
38
24
|
end
|
39
25
|
|
40
|
-
#
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
yield @forward
|
26
|
+
# DSL method for creating expectation
|
27
|
+
module DSL
|
28
|
+
def expectation(&_)
|
29
|
+
expectation = Expectation.new
|
30
|
+
yield expectation if block_given?
|
31
|
+
expectation
|
47
32
|
end
|
48
|
-
@forward
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
# DSL methods related to expectation
|
53
|
-
module DSL
|
54
|
-
def expectation(&_)
|
55
|
-
expectation = Expectation.new
|
56
|
-
yield expectation if block_given?
|
57
|
-
expectation
|
58
33
|
end
|
59
34
|
end
|
60
35
|
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require_relative './array_of'
|
3
|
+
require_relative './request'
|
4
|
+
require_relative './response'
|
5
|
+
require_relative './forward'
|
6
|
+
require_relative './times'
|
7
|
+
require_relative '../utility_methods'
|
8
|
+
|
9
|
+
#
|
10
|
+
# A class to model a mock object used to create and retrieve mock requests.
|
11
|
+
# See http://www.mock-server.com/#create-expectations for details.
|
12
|
+
#
|
13
|
+
# @author:: Nayyara Samuel (mailto: nayyara.samuel@opower.com)
|
14
|
+
#
|
15
|
+
module MockServer
|
16
|
+
HTTP_REQUEST = 'httpRequest'
|
17
|
+
HTTP_RESPONSE = 'httpResponse'
|
18
|
+
HTTP_FORWARD = 'httpForward'
|
19
|
+
HTTP_TIMES = 'times'
|
20
|
+
|
21
|
+
# Models
|
22
|
+
module Model # rubocop:disable Style/ClassAndModuleChildren
|
23
|
+
# Mock object model
|
24
|
+
class Mock
|
25
|
+
include MockServer::UtilityMethods
|
26
|
+
attr_accessor :times
|
27
|
+
|
28
|
+
# Creates an expectation from a hash
|
29
|
+
# @param payload [Hash] a hash representation of the expectation
|
30
|
+
def populate_from_payload(payload)
|
31
|
+
@request = payload[MockServer::HTTP_REQUEST]
|
32
|
+
@request = Request.new(symbolize_keys(@request)) if @request
|
33
|
+
|
34
|
+
@response = payload[MockServer::HTTP_RESPONSE]
|
35
|
+
@response = Response.new(symbolize_keys(@response)) if @response
|
36
|
+
|
37
|
+
@forward = payload[MockServer::HTTP_FORWARD]
|
38
|
+
@forward = Forward.new(symbolize_keys(@forward)) if @forward
|
39
|
+
|
40
|
+
@times = payload[MockServer::HTTP_TIMES]
|
41
|
+
@times = Times.new(symbolize_keys(@times)) if @times
|
42
|
+
end
|
43
|
+
|
44
|
+
# Method to setup the request on the expectation object
|
45
|
+
# @yieldparam [Request] the request that this expectation references
|
46
|
+
# @return [Expectation] this object according to the the builder pattern
|
47
|
+
def request(&_)
|
48
|
+
if block_given?
|
49
|
+
@request ||= Request.new
|
50
|
+
yield @request
|
51
|
+
end
|
52
|
+
@request
|
53
|
+
end
|
54
|
+
|
55
|
+
# Method to setup the response on the expectation object
|
56
|
+
# @yieldparam [Response] the response that this expectation references
|
57
|
+
# @return [Expectation] this object according to the the builder pattern
|
58
|
+
def response(&_)
|
59
|
+
if block_given?
|
60
|
+
@response ||= Response.new
|
61
|
+
yield @response
|
62
|
+
end
|
63
|
+
@response
|
64
|
+
end
|
65
|
+
|
66
|
+
# Method to setup the request on the expectation object
|
67
|
+
# @yieldparam [Forward] the forward object that this expectation references
|
68
|
+
# @return [Expectation] this object according to the the builder pattern
|
69
|
+
def forward(&_)
|
70
|
+
if block_given?
|
71
|
+
@forward ||= Forward.new
|
72
|
+
yield @forward
|
73
|
+
end
|
74
|
+
@forward
|
75
|
+
end
|
76
|
+
|
77
|
+
# Setter for request
|
78
|
+
# @param request [Request] a request object
|
79
|
+
def request=(request)
|
80
|
+
@request = Request.new(request)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Setter for response
|
84
|
+
# @param response [Response] a response object
|
85
|
+
def response=(response)
|
86
|
+
@response = Response.new(response)
|
87
|
+
end
|
88
|
+
|
89
|
+
# Setter for forward
|
90
|
+
# @param forward [Forward] a forward object
|
91
|
+
def forward=(forward)
|
92
|
+
@forward = Forward.new(forward)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Override to_json method
|
96
|
+
# @return [String] the json representation for this object
|
97
|
+
def to_json(*p)
|
98
|
+
to_hash.to_json(*p)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Convert to hash
|
102
|
+
# @return [Hash] the hash representation for this object
|
103
|
+
def to_hash
|
104
|
+
{
|
105
|
+
MockServer::HTTP_REQUEST => @request,
|
106
|
+
MockServer::HTTP_RESPONSE => @response,
|
107
|
+
MockServer::HTTP_FORWARD => @forward,
|
108
|
+
MockServer::HTTP_TIMES => @times
|
109
|
+
}
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Class to store a list of mocks - useful for modeling retrieve endpoint result
|
114
|
+
class Mocks < ArrayOf
|
115
|
+
# Code is used to store HTTP status code returned from retrieve endpoint
|
116
|
+
attr_accessor :code
|
117
|
+
|
118
|
+
def child_class
|
119
|
+
Mock
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
require_relative './body'
|
3
3
|
require_relative './delay'
|
4
4
|
require_relative './parameter'
|
5
|
+
require 'base64'
|
5
6
|
|
6
7
|
#
|
7
8
|
# A class to model a response in an expectation.
|
@@ -34,6 +35,10 @@ module MockServer::Model
|
|
34
35
|
obj
|
35
36
|
end
|
36
37
|
|
38
|
+
def decode(string)
|
39
|
+
Base64.decode64(string)
|
40
|
+
end
|
41
|
+
|
37
42
|
alias_method :http_response, :response
|
38
43
|
end
|
39
44
|
end
|
@@ -12,21 +12,34 @@ module MockServer::UtilityMethods
|
|
12
12
|
# - camelize the keys of the hash
|
13
13
|
# @param hash [Object] an object which will be used to create the hash. Must support :to_hash method
|
14
14
|
# @return [Hash] the transformed hash
|
15
|
-
def
|
15
|
+
def camelized_hash(obj)
|
16
16
|
obj = obj && obj.respond_to?(:to_hash) ? obj.to_hash : obj
|
17
17
|
|
18
18
|
if obj.is_a?(Hash)
|
19
19
|
obj.each_with_object({}) do |(k, v), acc|
|
20
20
|
is_empty = v.nil? || (v.respond_to?(:empty?) ? v.empty? : false)
|
21
|
-
acc[camelize(k)] =
|
21
|
+
acc[camelize(k)] = camelized_hash(v) unless is_empty
|
22
22
|
end
|
23
23
|
elsif obj.respond_to?(:map)
|
24
|
-
obj.map { |element|
|
24
|
+
obj.map { |element| camelized_hash(element) }
|
25
25
|
else
|
26
26
|
obj
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
# Converts keys to symbols
|
31
|
+
# @param hash [Hash] a hash
|
32
|
+
# @return [Hash] a copy of the hash where keys are symbols
|
33
|
+
def symbolize_keys(hash)
|
34
|
+
if hash.is_a?(Hash)
|
35
|
+
Hash[hash.map { |k, v| [k.to_s.underscore.to_sym, symbolize_keys(v)] }]
|
36
|
+
elsif hash.respond_to?(:map)
|
37
|
+
hash.map { |obj| symbolize_keys(obj) }
|
38
|
+
else
|
39
|
+
hash
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
30
43
|
# @param [Object] an object to camelize the string representation of
|
31
44
|
# @return [String] the string converted to camelcase with first letter in lower case
|
32
45
|
def camelize(str)
|
data/lib/mockserver/version.rb
CHANGED
data/mockserver-client.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = 'mockserver-client'
|
8
8
|
spec.version = MockServer::VERSION
|
9
9
|
spec.authors = ['Nayyara Samuel']
|
10
|
-
spec.email = ['nayyara.samuel@
|
10
|
+
spec.email = ['nayyara.samuel@opower.com']
|
11
11
|
spec.summary = %q(A Ruby client for MockServer)
|
12
12
|
spec.required_ruby_version = '~> 1.9.3'
|
13
13
|
|
@@ -28,4 +28,6 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_dependency 'activesupport', '~> 4.1.1'
|
29
29
|
spec.add_dependency 'rest-client', '~> 1.6.7'
|
30
30
|
spec.add_dependency 'logging_factory', '~> 0.0.2'
|
31
|
+
spec.add_dependency 'thor', '~> 0.19.1'
|
32
|
+
spec.add_dependency 'colorize', '~> 0.7.0'
|
31
33
|
end
|
@@ -69,6 +69,14 @@ describe MockServer::Model::DSL do
|
|
69
69
|
expect(to_camelized_hash(HTTP_TIMES => mock_times)).to eq(FIXTURES.read('times_once.json'))
|
70
70
|
end
|
71
71
|
|
72
|
+
it 'generates expectation correctly from file' do
|
73
|
+
payload = FIXTURES.read('register_expectation.json')
|
74
|
+
mock_expectation = expectation
|
75
|
+
mock_expectation.populate_from_payload(payload)
|
76
|
+
|
77
|
+
expect(to_camelized_hash(mock_expectation.to_hash)).to eq(payload)
|
78
|
+
end
|
79
|
+
|
72
80
|
it 'generates body correctly' do
|
73
81
|
expect(to_camelized_hash(regex('*/login'))).to eq('type' => 'REGEX', 'value' => '*/login')
|
74
82
|
expect(to_camelized_hash(xpath('/login[1]'))).to eq('type' => 'XPATH', 'value' => '/login[1]')
|
@@ -14,8 +14,8 @@ describe MockServer::ProxyClient do
|
|
14
14
|
|
15
15
|
# Stub requests
|
16
16
|
stub_request(:put, /.+\/retrieve/).with(body: search_request_json).to_return(
|
17
|
-
|
18
|
-
|
17
|
+
body: "[#{register_expectation_json}, #{register_expectation_json}]",
|
18
|
+
status: 200
|
19
19
|
)
|
20
20
|
stub_request(:put, /.+\/dumpToLog$/).to_return(status: 202).once
|
21
21
|
stub_request(:put, /.+\/dumpToLog\?type=java$/).to_return(status: 202).once
|
@@ -23,6 +23,7 @@ describe MockServer::ProxyClient do
|
|
23
23
|
|
24
24
|
it 'verifies requests correctly' do
|
25
25
|
response = client.verify(request(:POST, '/login'), exactly(2))
|
26
|
+
response = response.map { |mock| to_camelized_hash(mock.to_hash) }
|
26
27
|
expect(response).to eq([register_expectation, register_expectation])
|
27
28
|
end
|
28
29
|
|
data/spec/spec_helper.rb
CHANGED
@@ -41,7 +41,7 @@ module HelperMethods
|
|
41
41
|
|
42
42
|
# Reparse hash from json to make keys strings and ensure consistency of keys for tests
|
43
43
|
def to_camelized_hash(hash)
|
44
|
-
JSON.parse(
|
44
|
+
JSON.parse(camelized_hash(hash).to_json)
|
45
45
|
end
|
46
46
|
|
47
47
|
FIXTURES = FileAccessor.new(:spec, :fixtures)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mockserver-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nayyara Samuel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -164,10 +164,39 @@ dependencies:
|
|
164
164
|
- - ~>
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: 0.0.2
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: thor
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ~>
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: 0.19.1
|
174
|
+
type: :runtime
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ~>
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: 0.19.1
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: colorize
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - ~>
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: 0.7.0
|
188
|
+
type: :runtime
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ~>
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: 0.7.0
|
167
195
|
description:
|
168
196
|
email:
|
169
|
-
- nayyara.samuel@
|
170
|
-
executables:
|
197
|
+
- nayyara.samuel@opower.com
|
198
|
+
executables:
|
199
|
+
- mockserver
|
171
200
|
extensions: []
|
172
201
|
extra_rdoc_files: []
|
173
202
|
files:
|
@@ -177,6 +206,8 @@ files:
|
|
177
206
|
- Gemfile
|
178
207
|
- README.md
|
179
208
|
- Rakefile
|
209
|
+
- bin/mockserver
|
210
|
+
- lib/cli.rb
|
180
211
|
- lib/mockserver-client.rb
|
181
212
|
- lib/mockserver/abstract_client.rb
|
182
213
|
- lib/mockserver/mock_server_client.rb
|
@@ -186,6 +217,7 @@ files:
|
|
186
217
|
- lib/mockserver/model/enum.rb
|
187
218
|
- lib/mockserver/model/expectation.rb
|
188
219
|
- lib/mockserver/model/forward.rb
|
220
|
+
- lib/mockserver/model/mock.rb
|
189
221
|
- lib/mockserver/model/parameter.rb
|
190
222
|
- lib/mockserver/model/request.rb
|
191
223
|
- lib/mockserver/model/response.rb
|