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, #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.



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

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
end

Instance Attribute Details

#inherit_ioObject

Returns the value of attribute inherit_io.



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

def inherit_io
  @inherit_io
end

#processObject (readonly)

Returns the value of attribute process.



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

def process
  @process
end

#remote_hostObject

Returns the value of attribute remote_host.



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

def remote_host
  @remote_host
end

#wait_for_processObject

Returns the value of attribute wait_for_process.



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

def wait_for_process
  @wait_for_process
end

#wiremock_versionObject (readonly)

Returns the value of attribute wiremock_version.



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

def wiremock_version
  @wiremock_version
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



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

def working_directory
  @working_directory
end

Instance Method Details

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

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

Yields:

  • (_self)

Yield Parameters:



155
156
157
158
# File 'lib/service_mock/server.rb', line 155

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:



146
147
148
149
# File 'lib/service_mock/server.rb', line 146

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:



138
139
140
141
# File 'lib/service_mock/server.rb', line 138

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:



90
91
92
93
# File 'lib/service_mock/server.rb', line 90

def start
  yield self if block_given?
  start_process
end

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

Stops the running WireMock server if it is running locally.

Yields:

  • (_self)

Yield Parameters:



98
99
100
101
# File 'lib/service_mock/server.rb', line 98

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:



106
107
108
109
110
# File 'lib/service_mock/server.rb', line 106

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:



126
127
128
129
130
131
132
# File 'lib/service_mock/server.rb', line 126

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:



115
116
117
118
119
120
# File 'lib/service_mock/server.rb', line 115

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