Class: ServiceMock::Server

Inherits:
Object
  • Object
show all
Includes:
CommandLineOptions
Defined in:
lib/service_mock/server.rb

Constant Summary

Constants included from CommandLineOptions

CommandLineOptions::NOT_IMPLEMENTED, CommandLineOptions::OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandLineOptions

#command_line_options, #enable_browser_proxying_command, #extensions_command, #https_keystore_command, #https_port_command, #https_require_client_cert_command, #https_truststore_command, #keystore_password_command, #match_headers_command, #max_request_journal_entries_command, #no_request_journal_command, #port_command, #preserve_host_header_command, #proxy_all_command, #proxy_via_command, #record_mappings_command, #root_dir_command, #truststore_password_command, #verbose_command

Constructor Details

#initialize(wiremock_version, working_directory = ::ServiceMock.working_directory) ⇒ Server

Returns a new instance of Server.



50
51
52
53
54
55
56
# File 'lib/service_mock/server.rb', line 50

def initialize(wiremock_version, working_directory = ::ServiceMock.working_directory)
  @wiremock_version = wiremock_version
  @working_directory = working_directory
  self.inherit_io = false
  self.wait_for_process = false
  self.use_ssl = false
end

Instance Attribute Details

#classpathObject

Returns the value of attribute classpath.



46
47
48
# File 'lib/service_mock/server.rb', line 46

def classpath
  @classpath
end

#inherit_ioObject

Returns the value of attribute inherit_io.



46
47
48
# File 'lib/service_mock/server.rb', line 46

def inherit_io
  @inherit_io
end

#processObject (readonly)

Returns the value of attribute process.



47
48
49
# File 'lib/service_mock/server.rb', line 47

def process
  @process
end

#proxy_addrObject

Returns the value of attribute proxy_addr.



48
49
50
# File 'lib/service_mock/server.rb', line 48

def proxy_addr
  @proxy_addr
end

#proxy_passObject

Returns the value of attribute proxy_pass.



48
49
50
# File 'lib/service_mock/server.rb', line 48

def proxy_pass
  @proxy_pass
end

#proxy_portObject

Returns the value of attribute proxy_port.



48
49
50
# File 'lib/service_mock/server.rb', line 48

def proxy_port
  @proxy_port
end

#proxy_userObject

Returns the value of attribute proxy_user.



48
49
50
# File 'lib/service_mock/server.rb', line 48

def proxy_user
  @proxy_user
end

#remote_hostObject

Returns the value of attribute remote_host.



46
47
48
# File 'lib/service_mock/server.rb', line 46

def remote_host
  @remote_host
end

#use_sslObject

Returns the value of attribute use_ssl.



46
47
48
# File 'lib/service_mock/server.rb', line 46

def use_ssl
  @use_ssl
end

#wait_for_processObject

Returns the value of attribute wait_for_process.



46
47
48
# File 'lib/service_mock/server.rb', line 46

def wait_for_process
  @wait_for_process
end

#wiremock_versionObject (readonly)

Returns the value of attribute wiremock_version.



47
48
49
# File 'lib/service_mock/server.rb', line 47

def wiremock_version
  @wiremock_version
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



47
48
49
# File 'lib/service_mock/server.rb', line 47

def working_directory
  @working_directory
end

Instance Method Details

#count(request_criteria) {|_self| ... } ⇒ Object

Get the count for the request criteria

Yields:

  • (_self)

Yield Parameters:



141
142
143
144
145
# File 'lib/service_mock/server.rb', line 141

def count(request_criteria)
  return if ::ServiceMock.disable_stubs
  yield self if block_given?
  JSON.parse(http.post('/__admin/requests/count', request_criteria).body)['count']
end

#count_with_erb(filename, hsh = {}) {|_self| ... } ⇒ Object

Get the count for the request criteria using the erb template provided. The Hash second parameter contains the values to be inserted into the ERB.

Yields:

  • (_self)

Yield Parameters:



162
163
164
165
166
167
168
# File 'lib/service_mock/server.rb', line 162

def count_with_erb(filename, hsh={})
  return if ::ServiceMock.disable_stubs
  yield self if block_given?
  template = File.open(filename, 'rb') { |file| file.read }
  erb_content = ERB.new(template).result(data_binding(hsh))
  count(erb_content)
end

#count_with_file(filename) {|_self| ... } ⇒ Object

Get the count for the request criteria in the provided filename.

Yields:

  • (_self)

Yield Parameters:



150
151
152
153
154
155
# File 'lib/service_mock/server.rb', line 150

def count_with_file(filename)
  return if ::ServiceMock.disable_stubs
  yield self if block_given?
  content = File.open(filename, 'rb') { |file| file.read }
  count(content)
end

#reset_all {|_self| ... } ⇒ Object

Removes all stubs, file based stubs, and request logs from the WireMock server.

Yields:

  • (_self)

Yield Parameters:



191
192
193
194
# File 'lib/service_mock/server.rb', line 191

def reset_all
  yield self if block_given?
  http.post('/__admin/reset', '')
end

#reset_mappings {|_self| ... } ⇒ Object

Removes the stubs that have been created since WireMock was started.

Yields:

  • (_self)

Yield Parameters:



182
183
184
185
# File 'lib/service_mock/server.rb', line 182

def reset_mappings
  yield self if block_given?
  http.post('/__admin/mappings/reset', '')
end

#save {|_self| ... } ⇒ Object

Writes all of the stubs to disk so they are available the next time the server starts.

Yields:

  • (_self)

Yield Parameters:



174
175
176
177
# File 'lib/service_mock/server.rb', line 174

def save
  yield self if block_given?
  http.post('/__admin/mappings/save', '')
end

#start {|_self| ... } ⇒ Object

You start the server by calling the ‘start` method but it doesn’t end there. There are a large number of parameters you can set to control the way that WireMock runs. These are set via a block that is passed to the ‘start` method.

my_server.start do |server|
  server.port = 8081
  server.record_mappings = true
  server.root_dir = /path/to/root
  server.verbose = true
end

The values that can be set are:

port

The port to listen on for http request

https_port

The port to listen on for https request

https_keystore

Path to the keystore file containing an SSL certificate to use with https

keystore_password

Password to the keystore if something other than “password”

https_truststore

Path to a keystore file containing client certificates

truststore_password

Optional password to the trust store. Defaults to “password” if not specified

https_reuire_client_cert

Force clients to authenticate with a client certificate

verbose

print verbose output from the running process. Values are true or false

root_dir

Sets the root directory under which mappings and __files reside. This defaults to the current directory

record_mappings

Record incoming requests as stub mappings

match_headers

When in record mode, capture request headers with the keys specified

proxy_all

proxy all requests through to another URL. Typically used in conjunction with record_mappings such that a session on another service can be recorded

preserve_host_header

When in proxy mode, it passes the Host header as it comes from the client through to the proxied service

proxy_via

When proxying requests, route via another proxy server. Useful when inside a corporate network that only permits internet access via an opaque proxy

enable_browser_proxy

Run as a browser proxy

no_request_journal

Disable the request journal, which records incoming requests for later verification

max_request_journal_entries

Sets maximum number of entries in request journal. When this limit is reached oldest entries will be discarded

In addition, as mentioned before, you can set the inherit_io and wait_for_process options to true inside of the block.

Yields:

  • (_self)

Yield Parameters:



93
94
95
96
97
# File 'lib/service_mock/server.rb', line 93

def start
  yield self if block_given?
  classpath = self.classpath.is_a?(Array) ? self.classpath : []
  start_process(classpath)
end

#stop {|_self| ... } ⇒ Object

Stops the running WireMock server if it is running locally.

Yields:

  • (_self)

Yield Parameters:



102
103
104
105
# File 'lib/service_mock/server.rb', line 102

def stop
  yield self if block_given?
  http.post('/__admin/shutdown', '')
end

#stub(message) {|_self| ... } ⇒ Object

Create a stub based on the value provided.

Yields:

  • (_self)

Yield Parameters:



110
111
112
113
114
# File 'lib/service_mock/server.rb', line 110

def stub(message)
  return if ::ServiceMock.disable_stubs
  yield self if block_given?
  http.post('/__admin/mappings/new', message)
end

#stub_with_erb(filename, hsh = {}) {|_self| ... } ⇒ Object

Create a stub using the erb template provided. The Hash second parameter contains the values to be inserted into the ERB.

Yields:

  • (_self)

Yield Parameters:



130
131
132
133
134
135
136
# File 'lib/service_mock/server.rb', line 130

def stub_with_erb(filename, hsh={})
  return if ::ServiceMock.disable_stubs
  yield self if block_given?
  template = File.open(filename, 'rb') { |file| file.read }
  erb_content = ERB.new(template).result(data_binding(hsh))
  stub(erb_content)
end

#stub_with_file(filename) {|_self| ... } ⇒ Object

Create a stub using the information in the provided filename.

Yields:

  • (_self)

Yield Parameters:



119
120
121
122
123
124
# File 'lib/service_mock/server.rb', line 119

def stub_with_file(filename)
  return if ::ServiceMock.disable_stubs
  yield self if block_given?
  content = File.open(filename, 'rb') { |file| file.read }
  stub(content)
end