Class: MockServer::AbstractClient
- Inherits:
-
Object
- Object
- MockServer::AbstractClient
- Includes:
- Model, Model::DSL, UtilityMethods
- Defined in:
- lib/mockserver/abstract_client.rb
Overview
An abstract client for making requests supported by mock and proxy client.
Direct Known Subclasses
Constant Summary
Constants included from Model
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#clear(request) ⇒ Object
Clear all expectations with the given request.
-
#dump_log(request = nil, java = false) ⇒ Object
Request to dump logs to file.
-
#initialize(host, port) ⇒ AbstractClient
constructor
A new instance of AbstractClient.
-
#reset ⇒ Object
Reset the mock server clearing all expectations previously registered.
-
#retrieve(request = nil) ⇒ Object
Retrieve the list of requests that have been processed by the server.
-
#verify(request, times = exactly(1)) ⇒ Object
Verify that the given request is called the number of times expected.
Methods included from UtilityMethods
#camelize, #camelized_hash, #parse_string_to_json, #symbolize_keys
Methods included from Model::DSL
#at_least, #body, #decode, #delay_by, #exact, #exactly, #expectation, #expectation_from_json, #forward, #once, #parameter, #parameterized, #regex, #request, #response, #times, #unlimited, #xpath
Constructor Details
#initialize(host, port) ⇒ AbstractClient
Returns a new instance of AbstractClient.
26 27 28 29 30 31 |
# File 'lib/mockserver/abstract_client.rb', line 26 def initialize(host, port) fail 'Cannot instantiate AbstractClient class. You must subclass it.' if self.class == AbstractClient fail 'Host/port must not be nil' unless host && port @base = RestClient::Resource.new("http://#{host}:#{port}") @logger = ::LoggingFactory::DEFAULT_FACTORY.log(self.class) end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
24 25 26 |
# File 'lib/mockserver/abstract_client.rb', line 24 def logger @logger end |
Instance Method Details
#clear(request) ⇒ Object
Clear all expectations with the given request
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mockserver/abstract_client.rb', line 36 def clear(request) request = camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request))) logger.debug("Clearing expectation with request: #{request}") logger.debug("URL: #{CLEAR_ENDPOINT}. Payload: #{request.to_hash}") response = @base[CLEAR_ENDPOINT].put(request.to_json, content_type: :json) logger.debug("Got clear response: #{response.code}") parse_string_to_json(response) end |
#dump_log(request = nil, java = false) ⇒ Object
Request to dump logs to file
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mockserver/abstract_client.rb', line 81 def dump_log(request = nil, java = false) type_params = java ? '?type=java' : '' url = "#{DUMP_LOG_ENDPOINT}#{type_params}" request = request ? Request.new(symbolize_keys(request)) : {} logger.debug('Sending dump log request to mockserver') logger.debug("URL: #{url}. Payload: #{request.to_hash}") response = @base[url].put(request.to_json) logger.debug("Got dump to log response: #{response.code}") parse_string_to_json(response) end |
#reset ⇒ Object
Reset the mock server clearing all expectations previously registered
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mockserver/abstract_client.rb', line 49 def reset request = {} logger.debug('Resetting mockserver') logger.debug("URL: #{RESET_ENDPOINT}. Payload: #{request.to_hash}") response = @base[RESET_ENDPOINT].put(request.to_json) logger.debug("Got reset response: #{response.code}") parse_string_to_json(response) end |
#retrieve(request = nil) ⇒ Object
Retrieve the list of requests that have been processed by the server
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mockserver/abstract_client.rb', line 63 def retrieve(request = nil) request = request ? camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request))) : {} logger.debug('Retrieving request list from mockserver') logger.debug("URL: #{RETRIEVE_ENDPOINT}. Payload: #{request.to_hash}") response = @base[RETRIEVE_ENDPOINT].put(request.to_json) logger.debug("Got retrieve response: #{response.code}") expectations = Expectations.new([]) parse_string_to_json(response).map { |result| expectations << expectation_from_json(result) } unless response.empty? # expectations = Expectations.new([]).insert(0, (parse_string_to_json(response).map { |result| expectation_from_json(result) } unless response.empty?)) expectations.code = response.code expectations end |
#verify(request, times = exactly(1)) ⇒ Object
Verify that the given request is called the number of times expected
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mockserver/abstract_client.rb', line 98 def verify(request, times = exactly(1)) logger.debug('Sending query for verify to mockserver') results = retrieve(request) # Reusing the times model here so interpreting values here times = Times.new(symbolize_keys(times)) num_times = times.remaining_times is_exact = !times.unlimited fulfilled = is_exact ? (num_times == results.size) : (num_times <= results.size) fail "Expected request to be present: [#{num_times}] (#{is_exact ? 'exactly' : 'at least'}). But found: [#{results.size}]" unless fulfilled results end |