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_response, #symbolize_keys
Methods included from Model::DSL
#at_least, #body, #decode, #delay_by, #exact, #exactly, #expectation, #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 46 |
# File 'lib/mockserver/abstract_client.rb', line 36 def clear(request) request = camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request))) url = CLEAR_ENDPOINT logger.debug("Clearing expectation with request: #{request}") logger.debug("URL: #{url}. Payload: #{request.to_hash}") response = @base[url].put(request.to_json, content_type: :json) logger.debug("Got clear response: #{response.code}") parse_response(response) end |
#dump_log(request = nil, java = false) ⇒ Object
Request to dump logs to file
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mockserver/abstract_client.rb', line 91 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_response(response) end |
#reset ⇒ Object
Reset the mock server clearing all expectations previously registered
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mockserver/abstract_client.rb', line 50 def reset request = {} url = RESET_ENDPOINT logger.debug('Resetting mockserver') logger.debug("URL: #{url}. Payload: #{request.to_hash}") response = @base[url].put(request.to_json) logger.debug("Got reset response: #{response.code}") parse_response(response) end |
#retrieve(request = nil) ⇒ Object
Retrieve the list of requests that have been processed by the server
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/mockserver/abstract_client.rb', line 65 def retrieve(request = nil) request = request ? camelized_hash(HTTP_REQUEST => Request.new(symbolize_keys(request))) : {} url = RETRIEVE_ENDPOINT logger.debug('Retrieving request list from mockserver') logger.debug("URL: #{url}. Payload: #{request.to_hash}") response = @base[RETRIEVE_ENDPOINT].put(request.to_json) logger.debug("Got retrieve response: #{response.code}") results = parse_response(response) mocks = Mocks.new([]) unless results.empty? results.each do |result| mock = Mock.new mock.populate_from_payload(result) mocks << mock end end mocks.code = response.code mocks end |
#verify(request, times = exactly(1)) ⇒ Object
Verify that the given request is called the number of times expected
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mockserver/abstract_client.rb', line 108 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 |