Class: MockServer::AbstractClient

Inherits:
Object
  • Object
show all
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

MockServerClient, ProxyClient

Constant Summary

Constants included from Model

Model::DEFAULT_MISSING_INDEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UtilityMethods

#camelize, #camelized_hash, #parse_string_to_json, #symbolize_keys

Methods included from Model::DSL

#at_least, #body, #cookie, #decode, #delay_by, #exact, #exactly, #expectation, #expectation_from_json, #forward, #header, #once, #parameter, #parameterized, #regex, #request, #request_from_json, #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}", headers: { 'Content-Type' => 'application/json' })
  @logger = ::LoggingFactory::DEFAULT_FACTORY.log(self.class)
end

Instance Attribute Details

#loggerObject

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

Parameters:

  • request (Request)

    the request to use to clear an expectation

Returns:

  • (Object)

    the response from the clear action



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

Parameters:

  • java (Boolean) (defaults to: false)

    true to dump as Java code; false to dump as JSON

Returns:

  • (Object)

    the list of responses processed by the server



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mockserver/abstract_client.rb', line 80

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

#resetObject

Reset the mock server clearing all expectations previously registered

Returns:

  • (Object)

    the response from the reset action



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

Parameters:

  • request (Request) (defaults to: nil)

    to filter requests

Returns:

  • (Object)

    the list of responses processed by the server



63
64
65
66
67
68
69
70
71
72
73
74
75
# 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}")
  requests = Requests.new([])
  parse_string_to_json(response.body).map { |result| requests << request_from_json(result) } unless response.empty?
  requests.code = response.code
  requests
end

#verify(request, times = exactly(1)) ⇒ Object

Verify that the given request is called the number of times expected

Parameters:

  • request (Request)

    to filter requests

  • times (Times) (defaults to: exactly(1))

    expected number of times

Returns:

  • (Object)

    the list of responses processed by the server that match the request



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mockserver/abstract_client.rb', line 97

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