mockserver-client 1.0.8.pre → 5.3.0
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 +4 -4
- data/Gemfile +5 -1
- data/Gemfile.lock +79 -0
- data/README.md +89 -229
- data/Rakefile +8 -8
- data/docs/Body.md +7 -0
- data/docs/BodyWithContentType.md +7 -0
- data/docs/ConnectionOptions.md +12 -0
- data/docs/ControlApi.md +269 -0
- data/docs/Delay.md +9 -0
- data/docs/Expectation.md +7 -0
- data/docs/ExpectationApi.md +51 -0
- data/docs/Expectations.md +7 -0
- data/docs/HttpClassCallback.md +8 -0
- data/docs/HttpError.md +10 -0
- data/docs/HttpForward.md +11 -0
- data/docs/HttpObjectCallback.md +8 -0
- data/docs/HttpOverrideForwardedRequest.md +9 -0
- data/docs/HttpRequest.md +15 -0
- data/docs/HttpResponse.md +14 -0
- data/docs/HttpTemplate.md +10 -0
- data/docs/KeyToMultiValue.md +7 -0
- data/docs/KeyToValue.md +7 -0
- data/docs/Ports.md +8 -0
- data/docs/TimeToLive.md +10 -0
- data/docs/Times.md +9 -0
- data/docs/Verification.md +9 -0
- data/docs/VerificationSequence.md +8 -0
- data/docs/VerificationTimes.md +9 -0
- data/docs/VerifyApi.md +94 -0
- data/lib/mockserver-client.rb +62 -15
- data/lib/mockserver-client/api/control_api.rb +316 -0
- data/lib/mockserver-client/api/expectation_api.rb +73 -0
- data/lib/mockserver-client/api/verify_api.rb +127 -0
- data/lib/mockserver-client/api_client.rb +388 -0
- data/lib/mockserver-client/api_error.rb +38 -0
- data/lib/mockserver-client/configuration.rb +202 -0
- data/lib/mockserver-client/models/body.rb +174 -0
- data/lib/mockserver-client/models/body_with_content_type.rb +174 -0
- data/lib/mockserver-client/models/connection_options.rb +219 -0
- data/lib/mockserver-client/models/delay.rb +192 -0
- data/lib/mockserver-client/models/expectation.rb +174 -0
- data/lib/mockserver-client/models/expectations.rb +174 -0
- data/lib/mockserver-client/models/http_class_callback.rb +183 -0
- data/lib/mockserver-client/models/http_error.rb +201 -0
- data/lib/mockserver-client/models/http_forward.rb +244 -0
- data/lib/mockserver-client/models/http_object_callback.rb +183 -0
- data/lib/mockserver-client/models/http_override_forwarded_request.rb +192 -0
- data/lib/mockserver-client/models/http_request.rb +246 -0
- data/lib/mockserver-client/models/http_response.rb +237 -0
- data/lib/mockserver-client/models/http_template.rb +235 -0
- data/lib/mockserver-client/models/key_to_multi_value.rb +174 -0
- data/lib/mockserver-client/models/key_to_value.rb +174 -0
- data/lib/mockserver-client/models/ports.rb +185 -0
- data/lib/mockserver-client/models/time_to_live.rb +235 -0
- data/lib/mockserver-client/models/times.rb +192 -0
- data/lib/mockserver-client/models/verification.rb +192 -0
- data/lib/mockserver-client/models/verification_sequence.rb +185 -0
- data/lib/mockserver-client/models/verification_times.rb +192 -0
- data/lib/mockserver-client/version.rb +15 -0
- data/mockserver-client.gemspec +44 -36
- data/spec/api/control_api_spec.rb +102 -0
- data/spec/api/expectation_api_spec.rb +46 -0
- data/spec/api/verify_api_spec.rb +57 -0
- data/spec/api_client_spec.rb +226 -0
- data/spec/configuration_spec.rb +42 -0
- data/spec/models/body_spec.rb +35 -0
- data/spec/models/body_with_content_type_spec.rb +35 -0
- data/spec/models/connection_options_spec.rb +65 -0
- data/spec/models/delay_spec.rb +47 -0
- data/spec/models/expectation_spec.rb +35 -0
- data/spec/models/expectations_spec.rb +35 -0
- data/spec/models/http_class_callback_spec.rb +41 -0
- data/spec/models/http_error_spec.rb +53 -0
- data/spec/models/http_forward_spec.rb +63 -0
- data/spec/models/http_object_callback_spec.rb +41 -0
- data/spec/models/http_override_forwarded_request_spec.rb +47 -0
- data/spec/models/http_request_spec.rb +83 -0
- data/spec/models/http_response_spec.rb +77 -0
- data/spec/models/http_template_spec.rb +57 -0
- data/spec/models/key_to_multi_value_spec.rb +35 -0
- data/spec/models/key_to_value_spec.rb +35 -0
- data/spec/models/ports_spec.rb +41 -0
- data/spec/models/time_to_live_spec.rb +57 -0
- data/spec/models/times_spec.rb +47 -0
- data/spec/models/verification_sequence_spec.rb +41 -0
- data/spec/models/verification_spec.rb +47 -0
- data/spec/models/verification_times_spec.rb +47 -0
- data/spec/spec_helper.rb +103 -53
- metadata +225 -158
- data/.gitignore +0 -21
- data/.rubocop.yml +0 -7
- data/bin/mockserver +0 -9
- data/lib/cli.rb +0 -146
- data/lib/mockserver/abstract_client.rb +0 -111
- data/lib/mockserver/mock_server_client.rb +0 -46
- data/lib/mockserver/model/array_of.rb +0 -85
- data/lib/mockserver/model/body.rb +0 -56
- data/lib/mockserver/model/cookie.rb +0 -36
- data/lib/mockserver/model/delay.rb +0 -34
- data/lib/mockserver/model/enum.rb +0 -47
- data/lib/mockserver/model/expectation.rb +0 -139
- data/lib/mockserver/model/forward.rb +0 -41
- data/lib/mockserver/model/header.rb +0 -43
- data/lib/mockserver/model/parameter.rb +0 -43
- data/lib/mockserver/model/request.rb +0 -81
- data/lib/mockserver/model/response.rb +0 -45
- data/lib/mockserver/model/times.rb +0 -61
- data/lib/mockserver/proxy_client.rb +0 -9
- data/lib/mockserver/utility_methods.rb +0 -59
- data/lib/mockserver/version.rb +0 -5
- data/pom.xml +0 -118
- data/spec/fixtures/forward_mockserver.json +0 -7
- data/spec/fixtures/incorrect_login_response.json +0 -20
- data/spec/fixtures/post_login_request.json +0 -22
- data/spec/fixtures/register_expectation.json +0 -50
- data/spec/fixtures/retrieved_request.json +0 -22
- data/spec/fixtures/search_request.json +0 -6
- data/spec/fixtures/times_once.json +0 -6
- data/spec/integration/mock_client_integration_spec.rb +0 -82
- data/spec/mockserver/builder_spec.rb +0 -90
- data/spec/mockserver/mock_client_spec.rb +0 -80
- data/spec/mockserver/proxy_client_spec.rb +0 -38
data/.gitignore
DELETED
data/.rubocop.yml
DELETED
data/bin/mockserver
DELETED
data/lib/cli.rb
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
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
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
# encoding: UTF-8
|
|
2
|
-
require 'rest-client'
|
|
3
|
-
require 'logging_factory'
|
|
4
|
-
require_relative './model/times'
|
|
5
|
-
require_relative './model/request'
|
|
6
|
-
require_relative './model/expectation'
|
|
7
|
-
require_relative './utility_methods'
|
|
8
|
-
|
|
9
|
-
# An abstract client for making requests supported by mock and proxy client.
|
|
10
|
-
# @author Nayyara Samuel(mailto: nayyara.samuel@opower.com)
|
|
11
|
-
#
|
|
12
|
-
module MockServer
|
|
13
|
-
RESET_ENDPOINT = '/reset'
|
|
14
|
-
CLEAR_ENDPOINT = '/clear'
|
|
15
|
-
RETRIEVE_ENDPOINT = '/retrieve'
|
|
16
|
-
DUMP_LOG_ENDPOINT = '/dumpToLog'
|
|
17
|
-
|
|
18
|
-
# An abstract client for making requests supported by mock and proxy client.
|
|
19
|
-
class AbstractClient
|
|
20
|
-
include Model::DSL
|
|
21
|
-
include Model
|
|
22
|
-
include UtilityMethods
|
|
23
|
-
|
|
24
|
-
attr_accessor :logger
|
|
25
|
-
|
|
26
|
-
def initialize(host, port)
|
|
27
|
-
fail 'Cannot instantiate AbstractClient class. You must subclass it.' if self.class == AbstractClient
|
|
28
|
-
fail 'Host/port must not be nil' unless host && port
|
|
29
|
-
@base = RestClient::Resource.new("http://#{host}:#{port}", headers: { 'Content-Type' => 'application/json' })
|
|
30
|
-
@logger = ::LoggingFactory::DEFAULT_FACTORY.log(self.class)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# Clear all expectations with the given request
|
|
34
|
-
# @param request [Request] the request to use to clear an expectation
|
|
35
|
-
# @return [Object] the response from the clear action
|
|
36
|
-
def clear(request)
|
|
37
|
-
request = camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request)))
|
|
38
|
-
|
|
39
|
-
logger.debug("Clearing expectation with request: #{request}")
|
|
40
|
-
logger.debug("URL: #{CLEAR_ENDPOINT}. Payload: #{request.to_hash}")
|
|
41
|
-
|
|
42
|
-
response = @base[CLEAR_ENDPOINT].put(request.to_json, content_type: :json)
|
|
43
|
-
logger.debug("Got clear response: #{response.code}")
|
|
44
|
-
parse_string_to_json(response)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# Reset the mock server clearing all expectations previously registered
|
|
48
|
-
# @return [Object] the response from the reset action
|
|
49
|
-
def reset
|
|
50
|
-
request = {}
|
|
51
|
-
|
|
52
|
-
logger.debug('Resetting mockserver')
|
|
53
|
-
logger.debug("URL: #{RESET_ENDPOINT}. Payload: #{request.to_hash}")
|
|
54
|
-
|
|
55
|
-
response = @base[RESET_ENDPOINT].put(request.to_json)
|
|
56
|
-
logger.debug("Got reset response: #{response.code}")
|
|
57
|
-
parse_string_to_json(response)
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# Retrieve the list of requests that have been processed by the server
|
|
61
|
-
# @param request [Request] to filter requests
|
|
62
|
-
# @return [Object] the list of responses processed by the server
|
|
63
|
-
def retrieve(request = nil)
|
|
64
|
-
request = request ? camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request))) : {}
|
|
65
|
-
|
|
66
|
-
logger.debug('Retrieving request list from mockserver')
|
|
67
|
-
logger.debug("URL: #{RETRIEVE_ENDPOINT}. Payload: #{request.to_hash}")
|
|
68
|
-
|
|
69
|
-
response = @base[RETRIEVE_ENDPOINT].put(request.to_json)
|
|
70
|
-
logger.debug("Got retrieve response: #{response.code}")
|
|
71
|
-
requests = Requests.new([])
|
|
72
|
-
parse_string_to_json(response.body).map { |result| requests << request_from_json(result) } unless response.empty?
|
|
73
|
-
requests.code = response.code
|
|
74
|
-
requests
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
# Request to dump logs to file
|
|
78
|
-
# @param java [Boolean] true to dump as Java code; false to dump as JSON
|
|
79
|
-
# @return [Object] the list of responses processed by the server
|
|
80
|
-
def dump_log(request = nil, java = false)
|
|
81
|
-
type_params = java ? '?type=java' : ''
|
|
82
|
-
url = "#{DUMP_LOG_ENDPOINT}#{type_params}"
|
|
83
|
-
request = request ? Request.new(symbolize_keys(request)) : {}
|
|
84
|
-
|
|
85
|
-
logger.debug('Sending dump log request to mockserver')
|
|
86
|
-
logger.debug("URL: #{url}. Payload: #{request.to_hash}")
|
|
87
|
-
|
|
88
|
-
response = @base[url].put(request.to_json)
|
|
89
|
-
logger.debug("Got dump to log response: #{response.code}")
|
|
90
|
-
parse_string_to_json(response)
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
# Verify that the given request is called the number of times expected
|
|
94
|
-
# @param request [Request] to filter requests
|
|
95
|
-
# @param times [Times] expected number of times
|
|
96
|
-
# @return [Object] the list of responses processed by the server that match the request
|
|
97
|
-
def verify(request, times = exactly(1))
|
|
98
|
-
logger.debug('Sending query for verify to mockserver')
|
|
99
|
-
results = retrieve(request)
|
|
100
|
-
|
|
101
|
-
# Reusing the times model here so interpreting values here
|
|
102
|
-
times = Times.new(symbolize_keys(times))
|
|
103
|
-
num_times = times.remaining_times
|
|
104
|
-
is_exact = !times.unlimited
|
|
105
|
-
|
|
106
|
-
fulfilled = is_exact ? (num_times == results.size) : (num_times <= results.size)
|
|
107
|
-
fail "Expected request to be present: [#{num_times}] (#{is_exact ? 'exactly' : 'at least'}). But found: [#{results.size}]" unless fulfilled
|
|
108
|
-
results
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
end
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# encoding: UTF-8
|
|
2
|
-
require_relative './model/expectation'
|
|
3
|
-
require_relative './abstract_client'
|
|
4
|
-
require_relative './utility_methods'
|
|
5
|
-
|
|
6
|
-
#
|
|
7
|
-
# The client used to interact with the mock server.
|
|
8
|
-
# @author:: Nayyara Samuel (mailto: nayyara.samuel@opower.com)
|
|
9
|
-
#
|
|
10
|
-
module MockServer
|
|
11
|
-
EXPECTATION_ENDPOINT = '/expectation'
|
|
12
|
-
|
|
13
|
-
# The client used to interact with the mock server.
|
|
14
|
-
class MockServerClient < AbstractClient
|
|
15
|
-
include Model
|
|
16
|
-
include UtilityMethods
|
|
17
|
-
|
|
18
|
-
# Registers an expectation with the mockserver
|
|
19
|
-
# @param expectation [Expectation] the expectation to create the request from
|
|
20
|
-
# @return [Object] the response from the register action
|
|
21
|
-
def register(expectation)
|
|
22
|
-
fail 'Expectation passed in is not valid type' unless expectation.is_a?(Expectation)
|
|
23
|
-
request = create_expectation_request(expectation)
|
|
24
|
-
|
|
25
|
-
logger.debug('Registering new expectation')
|
|
26
|
-
logger.debug("URL: #{EXPECTATION_ENDPOINT} Payload: #{request.to_hash}")
|
|
27
|
-
|
|
28
|
-
response = @base[EXPECTATION_ENDPOINT].put(request.to_json, content_type: :json)
|
|
29
|
-
logger.debug("Got register response: #{response.code}")
|
|
30
|
-
parse_string_to_json(response)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
private
|
|
34
|
-
|
|
35
|
-
# Create an expecation request to send to the expectation endpoint of
|
|
36
|
-
# @param expectation [Expectation] the expectation to create the request from
|
|
37
|
-
# @return [Hash] a hash representing the request to use in registering an expectation with the mock server
|
|
38
|
-
# rubocop:disable Lint/LiteralInInterpolation
|
|
39
|
-
def create_expectation_request(expectation)
|
|
40
|
-
expectation_request = camelized_hash(expectation)
|
|
41
|
-
logger.debug("Expectation JSON: #{expectation_request.to_json}")
|
|
42
|
-
fail "You can only set either of #{[HTTP_RESPONSE, HTTP_FORWARD]}. But not both" if expectation_request[HTTP_RESPONSE] && expectation_request[HTTP_FORWARD]
|
|
43
|
-
expectation_request
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
# encoding: UTF-8
|
|
2
|
-
#
|
|
3
|
-
# The ArrayOf class stores instances of a given class only.
|
|
4
|
-
# It enforces this by intercepting the methods :<<, :[]= and :insert on the Array class
|
|
5
|
-
# and casts objects to the allowed type first. To use in your code, create a subclass of ArrayOf and override the child_class() method to return
|
|
6
|
-
# the class associated with the array.
|
|
7
|
-
#
|
|
8
|
-
# NOTE: You should use this class internally with the contract that you only call :<<. :[]= and :insert method
|
|
9
|
-
# to manipulate the array in use. Can easily be changed to have stricter rules, suffices for internal use in this gem.
|
|
10
|
-
#
|
|
11
|
-
# @author:: Nayyara Samuel (mailto: nayyara.samuel@opower.com)
|
|
12
|
-
#
|
|
13
|
-
module MockServer::Model
|
|
14
|
-
DEFAULT_MISSING_INDEX = -1_000_000_000
|
|
15
|
-
|
|
16
|
-
# The ArrayOf class stores instances of a given class only.
|
|
17
|
-
class ArrayOf < Array
|
|
18
|
-
alias_method :add_element, :<<
|
|
19
|
-
alias_method :set_element, :[]=
|
|
20
|
-
alias_method :insert_element, :insert
|
|
21
|
-
|
|
22
|
-
# Create an array from the elements passed in
|
|
23
|
-
def initialize(items)
|
|
24
|
-
items.each do |item|
|
|
25
|
-
self << item
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# The class/type that this array stores
|
|
30
|
-
def child_class
|
|
31
|
-
fail 'Subclass should override method :child_class'
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# Add the item to the array
|
|
35
|
-
# @param item [Object] an item of the type/class supported by this array
|
|
36
|
-
# @raise [Exception] if the item cannot be converted to the allowed class
|
|
37
|
-
def <<(item)
|
|
38
|
-
add_element(convert_to_child_class(item))
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# Set the given item at the index
|
|
42
|
-
# @param index [Integer] the index for the new element
|
|
43
|
-
# @param item [Object] an item of the type/class supported by this array
|
|
44
|
-
# @raise [Exception] if the item cannot be converted to the allowed class
|
|
45
|
-
def []=(index, item)
|
|
46
|
-
set_element(index, convert_to_child_class(item))
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
# Adds the given item at the index and shifts elements forward
|
|
50
|
-
# @param index [Integer] the index for the new element
|
|
51
|
-
# @param item [Object] an item of the type/class supported by this array
|
|
52
|
-
# @raise [Exception] if the item cannot be converted to the allowed class
|
|
53
|
-
def insert(index, item)
|
|
54
|
-
insert_element(index, convert_to_child_class(item))
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# Method to set the element at the specified index.
|
|
58
|
-
# Will insert at index if there is another object at the index; otherwise will update.
|
|
59
|
-
# If the special DEFAULT_MISSING_INDEX value is given, will insert at the end.
|
|
60
|
-
# @param index [Integer] the index for the new element
|
|
61
|
-
# @param item [Object] an item of the type/class supported by this array
|
|
62
|
-
# @raise [Exception] if the item cannot be converted to the allowed class
|
|
63
|
-
def set(index, item)
|
|
64
|
-
if index == DEFAULT_MISSING_INDEX
|
|
65
|
-
self << item
|
|
66
|
-
elsif self[index]
|
|
67
|
-
insert(index, item)
|
|
68
|
-
else
|
|
69
|
-
self[index] = item
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
# Cast item to target class
|
|
74
|
-
def convert_to_child_class(item)
|
|
75
|
-
if item && item.class != child_class
|
|
76
|
-
begin
|
|
77
|
-
item = child_class.new(item)
|
|
78
|
-
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
79
|
-
raise "Failed to convert element: #{item} to required type #{child_class}. Error: #{e.message}"
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
item
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
end
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
# encoding: UTF-8
|
|
2
|
-
require 'hashie'
|
|
3
|
-
require_relative './parameter'
|
|
4
|
-
require_relative './enum'
|
|
5
|
-
|
|
6
|
-
#
|
|
7
|
-
# A model for a a body in a request object.
|
|
8
|
-
# @author: Nayyara Samuel (mailto: nayyara.samuel@opower.com)
|
|
9
|
-
#
|
|
10
|
-
module MockServer::Model
|
|
11
|
-
# An enum for body type
|
|
12
|
-
class BodyType < SymbolizedEnum
|
|
13
|
-
def allowed_values
|
|
14
|
-
[:STRING, :REGEX, :XPATH, :PARAMETERS, :BINARY]
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
# A model for a a body in a request object.
|
|
19
|
-
class Body < Hashie::Dash
|
|
20
|
-
include Hashie::Extensions::MethodAccess
|
|
21
|
-
include Hashie::Extensions::IgnoreUndeclared
|
|
22
|
-
include Hashie::Extensions::Coercion
|
|
23
|
-
|
|
24
|
-
property :type, required: true
|
|
25
|
-
property :value
|
|
26
|
-
property :parameters
|
|
27
|
-
|
|
28
|
-
coerce_key :type, BodyType
|
|
29
|
-
coerce_key :value, String
|
|
30
|
-
coerce_key :parameters, Parameters
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# DSL methods related to body
|
|
34
|
-
module DSL
|
|
35
|
-
# For response object where body can only be a string
|
|
36
|
-
def body(value)
|
|
37
|
-
value
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def exact(value)
|
|
41
|
-
Body.new(type: :STRING, value: value)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def regex(value)
|
|
45
|
-
Body.new(type: :REGEX, value: value)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def xpath(value)
|
|
49
|
-
Body.new(type: :XPATH, value: value)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def parameterized(*parameters)
|
|
53
|
-
Body.new(type: :PARAMETERS, parameters: parameters)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|