Class: ServiceMock::Server
- Inherits:
-
Object
- Object
- ServiceMock::Server
- Includes:
- CommandLineOptions
- Defined in:
- lib/service_mock/server.rb
Constant Summary
Constants included from CommandLineOptions
CommandLineOptions::NOT_IMPLEMENTED, CommandLineOptions::OPTIONS
Instance Attribute Summary collapse
-
#classpath ⇒ Object
Returns the value of attribute classpath.
-
#inherit_io ⇒ Object
Returns the value of attribute inherit_io.
-
#process ⇒ Object
readonly
Returns the value of attribute process.
-
#proxy_addr ⇒ Object
Returns the value of attribute proxy_addr.
-
#proxy_pass ⇒ Object
Returns the value of attribute proxy_pass.
-
#proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
#proxy_user ⇒ Object
Returns the value of attribute proxy_user.
-
#remote_host ⇒ Object
Returns the value of attribute remote_host.
-
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
-
#wait_for_process ⇒ Object
Returns the value of attribute wait_for_process.
-
#wiremock_version ⇒ Object
readonly
Returns the value of attribute wiremock_version.
-
#working_directory ⇒ Object
readonly
Returns the value of attribute working_directory.
Instance Method Summary collapse
-
#count(request_criteria) {|_self| ... } ⇒ Object
Get the count for the request criteria.
-
#count_with_erb(filename, hsh = {}) {|_self| ... } ⇒ Object
Get the count for the request criteria using the erb template provided.
-
#count_with_file(filename) {|_self| ... } ⇒ Object
Get the count for the request criteria in the provided filename.
-
#initialize(wiremock_version, working_directory = ::ServiceMock.working_directory) ⇒ Server
constructor
A new instance of Server.
-
#reset_all {|_self| ... } ⇒ Object
Removes all stubs, file based stubs, and request logs from the WireMock server.
-
#reset_mappings {|_self| ... } ⇒ Object
Removes the stubs that have been created since WireMock was started.
-
#save {|_self| ... } ⇒ Object
Writes all of the stubs to disk so they are available the next time the server starts.
-
#start {|_self| ... } ⇒ Object
You start the server by calling the ‘start` method but it doesn’t end there.
-
#stop {|_self| ... } ⇒ Object
Stops the running WireMock server if it is running locally.
-
#stub(message) {|_self| ... } ⇒ Object
Create a stub based on the value provided.
-
#stub_with_erb(filename, hsh = {}) {|_self| ... } ⇒ Object
Create a stub using the erb template provided.
-
#stub_with_file(filename) {|_self| ... } ⇒ Object
Create a stub using the information in the provided filename.
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
#classpath ⇒ Object
Returns the value of attribute classpath.
46 47 48 |
# File 'lib/service_mock/server.rb', line 46 def classpath @classpath end |
#inherit_io ⇒ Object
Returns the value of attribute inherit_io.
46 47 48 |
# File 'lib/service_mock/server.rb', line 46 def inherit_io @inherit_io end |
#process ⇒ Object (readonly)
Returns the value of attribute process.
47 48 49 |
# File 'lib/service_mock/server.rb', line 47 def process @process end |
#proxy_addr ⇒ Object
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_pass ⇒ Object
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_port ⇒ Object
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_user ⇒ Object
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_host ⇒ Object
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_ssl ⇒ Object
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_process ⇒ Object
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_version ⇒ Object (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_directory ⇒ Object (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
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.
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.
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.
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.
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.
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
trueorfalse - root_dir
-
Sets the root directory under which
mappingsand__filesreside. 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.
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.
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.
110 111 112 113 114 |
# File 'lib/service_mock/server.rb', line 110 def stub() return if ::ServiceMock.disable_stubs yield self if block_given? http.post('/__admin/mappings/new', ) 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.
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.
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 |