service_mock 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fc036750c9ea274d089cab6ba71e1216884bda1
4
- data.tar.gz: 8760fda0af80a2d062fc25aab4d74e07f5ee8b6f
3
+ metadata.gz: d7fdc7bccb713c57d4015364a05509acc4fbd6c0
4
+ data.tar.gz: b2b8020335b3102cba37d8efd6b8c21baa4b8949
5
5
  SHA512:
6
- metadata.gz: e4ec01c860396bb89a9e32311b78cc3831cdb8de0c480eff8f17d0948d5388570fa2c3a5426461daa0139d70393e72c39bcd1148e5c3c1a0ff2ee562865f9f83
7
- data.tar.gz: 0f48213bb676c2fca31371dac6de2b784e5ce01ac2ddb7328f7a6547e1e77f1ef8ce2b299bdb65c22c33954328b2044fd18aedfecfaca968d416f4119cd7e46d
6
+ metadata.gz: 092ba99d6dd44fd75c64291bd163ea4a1a56b60bbba67e24ecec98b63470d12e79458ac57634bb596247488334bd9cdbfcefa07530261e9d5e396f1027f22513
7
+ data.tar.gz: 56b6032dc77a3ec75c6c7bf32c764b465911f1fd1bae10906b935d03b71c525839a0af154ce449ea7cdaee238299141ccb6fc03a4cc5a3519f409ec337afb824
data/ChangeLog CHANGED
@@ -1,3 +1,7 @@
1
+ === Release 0.3 / 2016-06-27
2
+ * Removed the config directory from the gem thereby reducing the size
3
+ * Added documentation
4
+
1
5
  === Release 0.2 / 2016-06-16
2
6
  * Reorganized the imports to make it easier to utilize the rake tasks
3
7
 
data/README.md CHANGED
@@ -1,8 +1,210 @@
1
1
  # ServiceMock
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/service_mock`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ ServiceMock is a Ruby gem that provides a wrapper over the [WireMock](http://wiremock.org)
4
+ library. The gem provides the ability to perform many actions on a WireMock instance
5
+ including starting, stopping, and several ways to stub out calls. It is highly
6
+ recommended that you take the time to read the WireMock documentation and understand
7
+ the how the library works.
4
8
 
9
+ This gem can be used to help you virtualize services for your tests. For example, if you
10
+ are unable to achieve effective test data management with the backend systems you can
11
+ use this gem (with WireMock) to simulate (or mock) the services while controlling the
12
+ data that is returned.
5
13
 
14
+ ServiceMock also provides two built-in rake task that can be used to start and stop an
15
+ instance of WireMock. The rake task need to be executed on the machine that the server
16
+ will run - there is no ability for remote start and stop.
17
+
18
+ ## Usage
19
+
20
+ ### Using the API
21
+
22
+ All interaction with the WireMock services is via calls to an instance of
23
+ `ServiceMock::Server`. On your local machine you can `start` and `stop` an
24
+ instance of the server process. On local and remove machines you can also
25
+ use one of several methods to create stubs (specify request/response data)
26
+ as well as tell the server to `save` in-memory stubs, `reset_mappings` to
27
+ remove all of the stubs you have provided and `reset_all` which completely
28
+ clears all mappings and files that you have setup for WireMock to return
29
+ as your system under test interacts with it.
30
+
31
+ When you create your instance of `ServiceMock::Server` you need to provide
32
+ some information. The required piece of data is the version of WireMock
33
+ you will be using. If the name of the WireMock jar file you will be using
34
+ is `wiremock-standalone-2.0.10-beta.jar` then the version you should provide
35
+ is `standalone-2.0.10-beta`. In other words, take off the initial `wiremock-`
36
+ and the trailing `.jar` and this is your version. The other optional value
37
+ you can provide is the working directory - the location where the WireMock
38
+ jar is located. By default the working directory is set to `config/mocks`.
39
+ You will initialize the server like this:
40
+
41
+ ```ruby
42
+ # uses default working directory
43
+ my_server = ServiceMock::Server.new('standalone-2.0.10-beta')
44
+
45
+ # or this sets the working directory
46
+ my_server = ServiceMock::Server.new('standalone-2.0.10-beta', '/path/to/jar')
47
+ ```
48
+
49
+ There are two additional values (inherit_io and wait_for_process) that
50
+ are defaulted to `false`. If set to `true`, `inherit_io` will cause our
51
+ instance to 'inherit' the standard out and in for the running WireMock
52
+ process. When `wait_for_process` is set to `true` it will cause the
53
+ call to `start` to block until the underlying WireMock process exits.
54
+ These values can be overwritten in the call to `start` as described below.
55
+
56
+ #### Starting the Server
57
+
58
+ You start the server by calling the `start` method but it doesn't end there.
59
+ There are a large number of parameters you can set to control the way that
60
+ WireMock runs. These are set via a block that is passed to the `start` method.
61
+
62
+ ```ruby
63
+ my_server.start do |server|
64
+ server.port = 8081
65
+ server.record_mappings = true
66
+ server.root_dir = /path/to/root
67
+ server.verbose = true
68
+ end
69
+ ```
70
+
71
+ The values that can be set are:
72
+
73
+ | value | description |
74
+ |--------|--------------|
75
+ | port | The port to listen on for http request |
76
+ | https_port | The port to listen on for https request |
77
+ | https_keystore | Path to the keystore file containing an SSL certificate to use with https |
78
+ | keystore_password | Password to the keystore if something other than "password" |
79
+ | https_truststore | Path to a keystore file containing client certificates |
80
+ | truststore_password | Optional password to the trust store. Defaults to "password" if not specified |
81
+ | https_reuire_client_cert | Force clients to authenticate with a client certificate |
82
+ | verbose | print verbose output from the running process. Values are `true` or `false` |
83
+ | root_dir | Sets the root directory under which `mappings` and `__files` reside. This defaults to the current directory |
84
+ | record_mappings | Record incoming requests as stub mappings |
85
+ | match_headers | When in record mode, capture request headers with the keys specified |
86
+ | 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 |
87
+ | preserve_host_header | When in proxy mode, it passes the Host header as it comes from the client through to the proxied service |
88
+ | 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 |
89
+ | enable_browser_proxy | Run as a browser proxy |
90
+ | no_request_journal | Disable the request journal, which records incoming requests for later verification |
91
+ | max_request_journal_entries | Sets maximum number of entries in request journal. When this limit is reached oldest entries will be discarded |
92
+
93
+ In addition, as mentioned before, you can set the `inherit_io` and `wait_for_process` options
94
+ to `true` inside of the block.
95
+
96
+ ### Stubbing service calls
97
+
98
+ Please read the [WireMock stubbing](http://wiremock.org/stubbing.html)
99
+ documentation so you understand the message structures that are used.
100
+ It is very important that you understand this first. Those details will
101
+ not be covered here.
102
+
103
+ There are three methods that can be used to stub services.
104
+
105
+ ```ruby
106
+ my_server.stub(string_containing_json_stub)
107
+
108
+ my_server.stub_with_file(file_containing_json_stub)
109
+
110
+ my_server.stub_with_erb(erbfile_containing_json_stub, hash_with_values)
111
+ ```
112
+
113
+ The first method is pretty self-explanatory. You simply pass a string
114
+ that contains the json stub and the server contacts the running server
115
+ process and sets it up. The second method takes a full path to a file
116
+ that contains the json stub.
117
+
118
+ The third method is where things get interesting. It allows you to provide
119
+ an [ERB](https://docs.puppet.com/puppet/latest/reference/lang_template_erb.html)
120
+ file that will become your json stub. ERB stands for Embedded Ruby and
121
+ allows you to insert Ruby code inside any type of file - including json.
122
+ Using this ability to allows us to build templates of the service messages.
123
+ It is quite common to mock a service many times and while the structure
124
+ is the same with each mock the data supplied and returned is different.
125
+ Using templates allows us to simply write the template once and then
126
+ supply the data for each mock.
127
+
128
+ The third method takes the full path to an erb file that contains the
129
+ json stub and a `Hash` that contains `key=>value` combinations that will
130
+ fill in the data elements in the template.
131
+
132
+ If you set the port value when you started the server you will need to
133
+ do the same via a block when stubbing.
134
+
135
+ ```ruby
136
+ my_server.stub(string_containing_json_stub) do |server|
137
+ server.port = 8081
138
+ end
139
+ ```
140
+
141
+ ### Other capabilities
142
+
143
+ There are additional methods that provide access to the running WireMock
144
+ server. If you changed the port during startup then you will need to do
145
+ so here as well. The methods are:
146
+
147
+ ```ruby
148
+ my_server.stop
149
+ ```
150
+
151
+ Stops the running WireMock server if it is running locally.
152
+
153
+ ```ruby
154
+ my_server.save
155
+ ```
156
+
157
+ Writes all of the stubs to disk so they are available the next time
158
+ the server starts.
159
+
160
+ ```ruby
161
+ my_server.reset_mappings
162
+ ```
163
+
164
+ Removes the stubs that have been created since WireMock was started.
165
+
166
+ ```ruby
167
+ my_server.reset_all
168
+ ```
169
+
170
+ Removes all stubs, file based stubs, and request logs from the WireMock
171
+ server.
172
+
173
+ ### Using the Rake Tasks
174
+
175
+ There are two rake tasks that are provided to make it easy to start and
176
+ stop the WireMock server when working locally. You can simply add
177
+ the following to your `Rakefile`:
178
+
179
+ ```ruby
180
+ require 'service_mock'
181
+ require 'service_mock/rake/rake_tasks'
182
+
183
+ WIREMOCK_VERSION = 'standalone-2.0.10-beta'
184
+ ServiceMock::Rake::StartServerTask.new(:start_server, WIREMOCK_VERSION)
185
+ ServiceMock::Rake::StopServerTask.new(:stop_server, WIREMOCK_VERSION)
186
+ ```
187
+
188
+ Any parameter that can be passed to the `start` method can also be passed
189
+ to the rake task.
190
+
191
+ ```ruby
192
+ ServiceMock::Rake::StartServerTask.new(:start_server, WIREMOCK_VERSION) do |server|
193
+ server.port = 8081
194
+ server.record_mappings = true
195
+ server.root_dir = /path/to/root
196
+ server.verbose = true
197
+ end
198
+ ```
199
+
200
+ If you set the port in the start task then you will also need to set it
201
+ in the stop task.
202
+
203
+ ```ruby
204
+ ServiceMock::Rake::StopServerTask.new(:stop_server, WIREMOCK_VERSION) do |server|
205
+ server.port = 8081
206
+ end
207
+ ```
6
208
 
7
209
  ## Installation
8
210
 
@@ -20,17 +222,20 @@ Or install it yourself as:
20
222
 
21
223
  $ gem install service_mock
22
224
 
23
- ## Usage
24
-
25
-
26
225
 
27
226
  ## Development
28
227
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
228
+ After checking out the repo, run `bin/setup` to install dependencies.
229
+ Then, run `rake spec` to run the tests. You can also run `bin/console`
230
+ for an interactive prompt that will allow you to experiment.
231
+
30
232
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
233
 
33
234
  ## Contributing
34
235
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/service_mock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
236
+ Bug reports and pull requests are welcome on GitHub at
237
+ https://github.com/cheezy/service_mock. This project is intended to
238
+ be a safe, welcoming space for collaboration, and contributors are
239
+ expected to adhere to the [Contributor Covenant](http://contributor-covenant.org)
240
+ code of conduct.
36
241
 
@@ -4,6 +4,40 @@ require 'erb'
4
4
  require 'ostruct'
5
5
  require 'service_mock/render_subtemplate'
6
6
 
7
+ #
8
+ # All interaction with the WireMock services is via calls to an instance of
9
+ # +ServiceMock::Server+. On your local machine you can +start+ and +stop+ an
10
+ # instance of the server process. On local and remove machines you can also
11
+ # use one of several methods to create stubs (specify request/response data)
12
+ # as well as tell the server to +save+ in-memory stubs, +reset_mappings+ to
13
+ # remove all of the stubs you have provided and +reset_all+ which completely
14
+ # clears all mappings and files that you have setup for WireMock to return
15
+ # as your system under test interacts with it.
16
+ #
17
+ # When you create your instance of +ServiceMock::Server+ you need to provide
18
+ # some information. The required piece of data is the version of WireMock
19
+ # you will be using. If the name of the WireMock jar file you will be using
20
+ # is +wiremock-standalone-2.0.10-beta.jar+ then the version you should provide
21
+ # is +standalone-2.0.10-beta+. In other words, take off the initial +wiremock-+
22
+ # and the trailing +.jar+ and this is your version. The other optional value
23
+ # you can provide is the working directory - the location where the WireMock
24
+ # jar is located. By default the working directory is set to +config/mocks+.
25
+ # You will initialize the server like this:
26
+ #
27
+ # # uses default working directory
28
+ # my_server = ServiceMock::Server.new('standalone-2.0.10-beta')
29
+ #
30
+ # # or this sets the working directory
31
+ # my_server = ServiceMock::Server.new('standalone-2.0.10-beta', '/path/to/jar')
32
+ #
33
+ # There are two additional values (inherit_io and wait_for_process) that
34
+ # are defaulted to +false+. If set to +true+, +inherit_io+ will cause our
35
+ # instance to 'inherit' the standard out and in for the running WireMock
36
+ # process. When +wait_for_process+ is set to +true+ it will cause the
37
+ # call to +start+ to block until the underlying WireMock process exits.
38
+ # These values can be overwritten in the call to +start+.
39
+ #
40
+
7
41
  module ServiceMock
8
42
  class Server
9
43
  include CommandLineOptions
@@ -18,27 +52,75 @@ module ServiceMock
18
52
  self.wait_for_process = false
19
53
  end
20
54
 
55
+ #
56
+ # You start the server by calling the `start` method but it doesn't end there.
57
+ # There are a large number of parameters you can set to control the way that
58
+ # WireMock runs. These are set via a block that is passed to the `start` method.
59
+ #
60
+ # my_server.start do |server|
61
+ # server.port = 8081
62
+ # server.record_mappings = true
63
+ # server.root_dir = /path/to/root
64
+ # server.verbose = true
65
+ # end
66
+ #
67
+ # The values that can be set are:
68
+ #
69
+ # [port] The port to listen on for http request
70
+ # [https_port] The port to listen on for https request
71
+ # [https_keystore] Path to the keystore file containing an SSL certificate to use with https
72
+ # [keystore_password] Password to the keystore if something other than "password"
73
+ # [https_truststore] Path to a keystore file containing client certificates
74
+ # [truststore_password] Optional password to the trust store. Defaults to "password" if not specified
75
+ # [https_reuire_client_cert] Force clients to authenticate with a client certificate
76
+ # [verbose] print verbose output from the running process. Values are +true+ or +false+
77
+ # [root_dir] Sets the root directory under which +mappings+ and +__files+ reside. This defaults to the current directory
78
+ # [record_mappings] Record incoming requests as stub mappings
79
+ # [match_headers] When in record mode, capture request headers with the keys specified
80
+ # [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
81
+ # [preserve_host_header] When in proxy mode, it passes the Host header as it comes from the client through to the proxied service
82
+ # [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
83
+ # [enable_browser_proxy] Run as a browser proxy
84
+ # [no_request_journal] Disable the request journal, which records incoming requests for later verification
85
+ # [max_request_journal_entries] Sets maximum number of entries in request journal. When this limit is reached oldest entries will be discarded
86
+ #
87
+ # In addition, as mentioned before, you can set the +inherit_io+ and +wait_for_process+ options
88
+ # to +true+ inside of the block.
89
+ #
21
90
  def start
22
91
  yield self if block_given?
23
92
  start_process
24
93
  end
25
94
 
95
+ #
96
+ # Stops the running WireMock server if it is running locally.
97
+ #
26
98
  def stop
27
99
  yield self if block_given?
28
100
  http.post('/__admin/shutdown', '')
29
101
  end
30
102
 
103
+ #
104
+ # Create a stub based on the value provided.
105
+ #
31
106
  def stub(message)
32
107
  yield self if block_given?
33
108
  http.post('/__admin/mappings/new', message)
34
109
  end
35
110
 
111
+ #
112
+ # Create a stub using the information in the provided filename.
113
+ #
36
114
  def stub_with_file(filename)
37
115
  yield self if block_given?
38
116
  content = File.open(filename, 'rb') {|file| file.read}
39
117
  stub(content)
40
118
  end
41
119
 
120
+ #
121
+ # Create a stub using the erb template provided. The +Hash+ second
122
+ # parameter contains the values to be inserted into the +ERB+.
123
+ #
42
124
  def stub_with_erb(filename, hsh)
43
125
  yield self if block_given?
44
126
  template = File.open(filename, 'rb') {|file| file.read}
@@ -46,16 +128,27 @@ module ServiceMock
46
128
  stub(erb_content)
47
129
  end
48
130
 
131
+ #
132
+ # Writes all of the stubs to disk so they are available the next time
133
+ # the server starts.
134
+ #
49
135
  def save
50
136
  yield self if block_given?
51
137
  http.post('/__admin/mappings/save', '')
52
138
  end
53
139
 
140
+ #
141
+ # Removes the stubs that have been created since WireMock was started.
142
+ #
54
143
  def reset_mappings
55
144
  yield self if block_given?
56
145
  http.post('/__admin/mappings/reset', '')
57
146
  end
58
147
 
148
+ #
149
+ # Removes all stubs, file based stubs, and request logs from the WireMock
150
+ # server.
151
+ #
59
152
  def reset_all
60
153
  yield self if block_given?
61
154
  http.post('/__admin/reset', '')
@@ -63,7 +156,6 @@ module ServiceMock
63
156
 
64
157
  private
65
158
 
66
-
67
159
  def data_binding(hsh)
68
160
  OpenStruct.include ::ServiceMock::RenderSubTemplate
69
161
  OpenStruct.new(hsh).instance_eval { binding }
@@ -1,3 +1,3 @@
1
1
  module ServiceMock
2
- VERSION = "0.2"
2
+ VERSION = "0.3"
3
3
  end
data/lib/service_mock.rb CHANGED
@@ -4,6 +4,22 @@ require 'service_mock/server'
4
4
  require 'service_mock/render_subtemplate'
5
5
  require 'service_mock/rake/rake_tasks'
6
6
 
7
+ #
8
+ # ServiceMock is a Ruby gem that provides a wrapper over the [WireMock](http://wiremock.org)
9
+ # library. The gem provides the ability to perform many actions on a WireMock instance
10
+ # including starting, stopping, and several ways to stub out calls. It is highly
11
+ # recommended that you take the time to read the WireMock documentation and understand
12
+ #the how the library works.
13
+ #
14
+ # This gem can be used to help you virtualize services for your tests. For example, if you
15
+ # are unable to achieve effective test data management with the backend systems you can
16
+ # use this gem (with WireMock) to simulate (or mock) the services while controlling the
17
+ # data that is returned.
18
+ #
19
+ # ServiceMock also provides two built-in rake task that can be used to start and stop an
20
+ # instance of WireMock. The rake task need to be executed on the machine that the server
21
+ # will run - there is no ability for remote start and stop.
22
+ #
7
23
  module ServiceMock
8
24
 
9
25
  end
data/service_mock.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.homepage = "https://github.com/cheezy/service_mock"
15
15
 
16
16
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|config)/}) }
18
18
  spec.bindir = "exe"
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeffrey S. Morgan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-16 00:00:00.000000000 Z
11
+ date: 2016-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: childprocess
@@ -85,8 +85,6 @@ files:
85
85
  - Rakefile
86
86
  - bin/console
87
87
  - bin/setup
88
- - config/mocks/wiremock-1.58-standalone.jar
89
- - config/mocks/wiremock-standalone-2.0.10-beta.jar
90
88
  - lib/service_mock.rb
91
89
  - lib/service_mock/command_line_options.rb
92
90
  - lib/service_mock/rake/base_task.rb