service_mock 0.6 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog +12 -9
- data/README.md +90 -18
- data/lib/service_mock/server.rb +4 -2
- data/lib/service_mock/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: babc9682df466187ff0a09472b45f1f08b56e985
|
4
|
+
data.tar.gz: 556344bb1625cab5e3de9b20537a0a7359533763
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cc09426b3f7fd4c5760684279cc9725568845a2b163f90656b5221097eb9110bee32b6d71ea2ac9a11a8595eb4affa17efb785f03f50b5faf60e23d47b9dcca
|
7
|
+
data.tar.gz: 71c96c21493db96c7f5563f2350fe5717276b4f56037505001ce03a003de831653a651069284ceb906008ffd93dab452193d98c0f32fe4b196300ad5486f0921
|
data/ChangeLog
CHANGED
@@ -1,31 +1,34 @@
|
|
1
|
-
=== Release 0.
|
1
|
+
=== Release 0.7
|
2
|
+
* Added ability to set remote_host and port via WIREMOCK_URL environment variable
|
3
|
+
|
4
|
+
=== Release 0.6 / 9-Jan-2017
|
2
5
|
* Added value_with_default method to be used in templates
|
3
6
|
|
4
|
-
=== Release 0.5.2
|
7
|
+
=== Release 0.5.2 / 17-Aug-2016
|
5
8
|
* Fixed defect with merging data where key doesn't exist
|
6
9
|
|
7
|
-
=== Release 0.5.1
|
10
|
+
=== Release 0.5.1 / 10-Aug-2016
|
8
11
|
* Added ability to turn off all stubbing by setting disable_stubs
|
9
12
|
|
10
|
-
=== Release 0.5
|
13
|
+
=== Release 0.5 / 31-Jul-2016
|
11
14
|
* Added new helper method to remove all whitespace
|
12
15
|
* Added ability to merge data when creating a set of stubs with StubCreator
|
13
16
|
* Added ability to change default working directory
|
14
17
|
|
15
|
-
=== Release 0.4 /
|
18
|
+
=== Release 0.4 / 21-Jul-2016
|
16
19
|
* Added new class (StubCreator) to create a set of stubs from a yaml file
|
17
20
|
|
18
|
-
=== Release 0.3.1 /
|
21
|
+
=== Release 0.3.1 / 28-Jun-2016
|
19
22
|
* Added remove_newlines method to String as a helper for rendering subtemplates
|
20
23
|
* Added escape_double_quotes method to String as a helper for rendering subtemplates
|
21
24
|
* Added empty Hash as default parameter for stub_with_erb
|
22
25
|
|
23
|
-
=== Release 0.3 /
|
26
|
+
=== Release 0.3 / 27-Jun-2016
|
24
27
|
* Removed the config directory from the gem thereby reducing the size
|
25
28
|
* Added documentation
|
26
29
|
|
27
|
-
=== Release 0.2 /
|
30
|
+
=== Release 0.2 / 16-Jun-2016
|
28
31
|
* Reorganized the imports to make it easier to utilize the rake tasks
|
29
32
|
|
30
|
-
=== Release 0.1 /
|
33
|
+
=== Release 0.1 / 15-Jun-2016
|
31
34
|
* Initial release with basic functionality
|
data/README.md
CHANGED
@@ -15,24 +15,48 @@ ServiceMock also provides two built-in rake task that can be used to start and s
|
|
15
15
|
instance of WireMock. The rake task need to be executed on the machine that the server
|
16
16
|
will run - there is no ability for remote start and stop.
|
17
17
|
|
18
|
+
## Getting Started
|
19
|
+
|
20
|
+
This section is a high-level overview of how I typically use this gem in my projects.
|
21
|
+
I will skip over some details here but you can find them below.
|
22
|
+
|
23
|
+
Let's say that I have an application that is using many restful services. I would
|
24
|
+
begin by starting up WireMock in proxy mode and make my application request the services
|
25
|
+
from the WireMock url (usually with a Rake task I would create). WireMock will create
|
26
|
+
two files for each service call - one that has the request / response structure and
|
27
|
+
one that has the response body.
|
28
|
+
|
29
|
+
Next I would take the files created by WireMock and turn them into my templates. I do
|
30
|
+
this by updating values in the files to data elements. For example, if I have some place
|
31
|
+
that had a persons last name I would put `<%= last_name %>` there. If I wanted to provide
|
32
|
+
a default that could be used I would use one of this gems ERB helper methods that allows
|
33
|
+
me to provide a default - `<%= value_with_default(last_name, 'Smith') %>`. If there
|
34
|
+
is part of the message that repeats itself I might create a loop. I can use any Ruby
|
35
|
+
code I want.
|
36
|
+
|
37
|
+
Once I have the templates created I then turn to my tests. For each test I specify
|
38
|
+
the data that I need to merge with the templates or override the defaults. I put this
|
39
|
+
data in my yml files. Once I have the data file created I simply make the call in my
|
40
|
+
code to stub out the services.
|
41
|
+
|
18
42
|
## Usage
|
19
43
|
|
20
44
|
### Using the API
|
21
45
|
|
22
46
|
All interaction with the WireMock services is via calls to an instance of
|
23
47
|
`ServiceMock::Server`. On your local machine you can `start` and `stop` an
|
24
|
-
instance of the server process. On local and
|
48
|
+
instance of the server process. On local and remote machines you can also
|
25
49
|
use one of several methods to create stubs (specify request/response data)
|
26
50
|
as well as tell the server to `save` in-memory stubs, `reset_mappings` to
|
27
51
|
remove all of the stubs you have provided and `reset_all` which completely
|
28
52
|
clears all mappings and files that you have setup for WireMock to return
|
29
53
|
as your system under test interacts with it.
|
30
54
|
|
31
|
-
When you create
|
32
|
-
some information. The
|
55
|
+
When you create an instance of `ServiceMock::Server` you often need to provide
|
56
|
+
some additional information. The _required_ piece of data is the version of WireMock
|
33
57
|
you will be using. If the name of the WireMock jar file you will be using
|
34
|
-
is `wiremock-standalone-2.0.
|
35
|
-
is `standalone-2.0
|
58
|
+
is `wiremock-standalone-2.5.0.jar` then the version you should provide
|
59
|
+
is `standalone-2.5.0`. In other words, take off the initial `wiremock-`
|
36
60
|
and the trailing `.jar` and this is your version. The other optional value
|
37
61
|
you can provide is the working directory - the location where the WireMock
|
38
62
|
jar is located. By default the working directory is set to `config/mocks`.
|
@@ -47,8 +71,8 @@ my_server = ServiceMock::Server.new('standalone-2.0.10-beta', '/path/to/jar')
|
|
47
71
|
```
|
48
72
|
|
49
73
|
There are two additional values (inherit_io and wait_for_process) that
|
50
|
-
|
51
|
-
instance to 'inherit' the standard out and in for the running WireMock
|
74
|
+
have default values of `false`. If set to `true`, `inherit_io` will cause the
|
75
|
+
server instance to 'inherit' the standard out and in for the running WireMock
|
52
76
|
process. When `wait_for_process` is set to `true` it will cause the
|
53
77
|
call to `start` to block until the underlying WireMock process exits.
|
54
78
|
These values can be overwritten in the call to `start` as described below.
|
@@ -63,7 +87,7 @@ WireMock runs. These are set via a block that is passed to the `start` method.
|
|
63
87
|
my_server.start do |server|
|
64
88
|
server.port = 8081
|
65
89
|
server.record_mappings = true
|
66
|
-
server.root_dir = /path/to/root
|
90
|
+
server.root_dir = '/path/to/root'
|
67
91
|
server.verbose = true
|
68
92
|
end
|
69
93
|
```
|
@@ -119,15 +143,15 @@ The third method is where things get interesting. It allows you to provide
|
|
119
143
|
an [ERB](https://docs.puppet.com/puppet/latest/reference/lang_template_erb.html)
|
120
144
|
file that will become your json stub. ERB stands for Embedded Ruby and
|
121
145
|
allows you to insert Ruby code inside any type of file - including json.
|
122
|
-
Using this ability
|
123
|
-
It is quite common to mock a service many times
|
124
|
-
is the same with each mock the data supplied and
|
125
|
-
Using templates allows us to simply write the
|
126
|
-
supply the data for each mock.
|
146
|
+
Using this ability allows us to build templates of the service messages.
|
147
|
+
It is quite common to mock a service many times in your test suite and
|
148
|
+
while the structure is the same with each mock the data supplied and
|
149
|
+
returned is different. Using templates allows us to simply write the
|
150
|
+
template once and then supply the data for each mock.
|
127
151
|
|
128
152
|
The third method takes the full path to an erb file that contains the
|
129
|
-
json stub and a
|
130
|
-
fill in the data elements in the template.
|
153
|
+
json stub and a second parameter that is a `Hash` which provides the
|
154
|
+
data that is used to fill in the data elements in the template.
|
131
155
|
|
132
156
|
If you set the port value when you started the server you will need to
|
133
157
|
do the same via a block when stubbing.
|
@@ -138,6 +162,20 @@ my_server.stub(string_containing_json_stub) do |server|
|
|
138
162
|
end
|
139
163
|
```
|
140
164
|
|
165
|
+
If the WireMock instance is running on another server you will need to
|
166
|
+
provide the url so it can be found. This is often necessary when you
|
167
|
+
are running your tests in a build pipeline or a continuous integration
|
168
|
+
server. You can do this by setting the `remote_host` value like this:
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
my_server.stub_with_erb(erbfile, values) do |server|
|
172
|
+
server.remote_host = 'other_host_name'
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
Another way to set both the port and remote host is to set an instance
|
177
|
+
variable `WIREMOCK_URL`. It should take the form 'http://hostname:port'.
|
178
|
+
|
141
179
|
### Stubbing multiple services
|
142
180
|
|
143
181
|
It is often necessary to stub multiple service calls in order to
|
@@ -169,10 +207,28 @@ service2.erb:
|
|
169
207
|
password: secret
|
170
208
|
```
|
171
209
|
|
172
|
-
With the above file the method call will mock
|
210
|
+
With the above file the method call will mock two services. It will first
|
173
211
|
of all read the file `service1.erb` from the `templates` directory and
|
174
212
|
stub it passing the data that is associated. Next it will read the next
|
175
|
-
template file, etc.
|
213
|
+
template file (`service2.erb`), etc.
|
214
|
+
|
215
|
+
The `create_stubs_with` method has an optional second parameter. It is
|
216
|
+
a `Hash` that can be used to override a value that is contained within
|
217
|
+
our data file. For example, if I wanted to override just the password
|
218
|
+
used in service2 from the example above I do this:
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
stub_creater.create_stubs_with(filename, {'service2' => {'password' => 'updated'}})
|
222
|
+
```
|
223
|
+
|
224
|
+
### Disabling Stubbing
|
225
|
+
|
226
|
+
There might be some tests that you want to run sometimes against mocked
|
227
|
+
services and sometimes against the real services. This is very simple
|
228
|
+
to do. If you simply set the value of `ServieMock.disable_stubs` to
|
229
|
+
true all of the `stub_*` methods return immediately without stubbing
|
230
|
+
anything. In practice you would simply point your application to the
|
231
|
+
real services and disable the stubs via this flag.
|
176
232
|
|
177
233
|
### Other capabilities
|
178
234
|
|
@@ -206,6 +262,22 @@ my_server.reset_all
|
|
206
262
|
Removes all stubs, file based stubs, and request logs from the WireMock
|
207
263
|
server.
|
208
264
|
|
265
|
+
|
266
|
+
There are two methods that can be used in your ERB templates. They are:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
render(path)
|
270
|
+
```
|
271
|
+
This will read the file at `path` and place its' contents in place of the call.
|
272
|
+
I use this to insert the body inside the response.
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
value_with_default(value, default)
|
276
|
+
```
|
277
|
+
This call allows you to provide a default for the data that will be inserted into
|
278
|
+
the ERB template. It can be used like this -
|
279
|
+
`<%= value_with_default(city, 'Toronto') %>`
|
280
|
+
|
209
281
|
### Using the Rake Tasks
|
210
282
|
|
211
283
|
There are two rake tasks that are provided to make it easy to start and
|
@@ -228,7 +300,7 @@ to the rake task.
|
|
228
300
|
ServiceMock::Rake::StartServerTask.new(:start_server, WIREMOCK_VERSION) do |server|
|
229
301
|
server.port = 8081
|
230
302
|
server.record_mappings = true
|
231
|
-
server.root_dir = /path/to/root
|
303
|
+
server.root_dir = '/path/to/root'
|
232
304
|
server.verbose = true
|
233
305
|
end
|
234
306
|
```
|
data/lib/service_mock/server.rb
CHANGED
@@ -115,7 +115,7 @@ module ServiceMock
|
|
115
115
|
def stub_with_file(filename)
|
116
116
|
return if ::ServiceMock.disable_stubs
|
117
117
|
yield self if block_given?
|
118
|
-
content = File.open(filename, 'rb') {|file| file.read}
|
118
|
+
content = File.open(filename, 'rb') { |file| file.read }
|
119
119
|
stub(content)
|
120
120
|
end
|
121
121
|
|
@@ -126,7 +126,7 @@ module ServiceMock
|
|
126
126
|
def stub_with_erb(filename, hsh={})
|
127
127
|
return if ::ServiceMock.disable_stubs
|
128
128
|
yield self if block_given?
|
129
|
-
template = File.open(filename, 'rb') {|file| file.read}
|
129
|
+
template = File.open(filename, 'rb') { |file| file.read }
|
130
130
|
erb_content = ERB.new(template).result(data_binding(hsh))
|
131
131
|
stub(erb_content)
|
132
132
|
end
|
@@ -181,10 +181,12 @@ module ServiceMock
|
|
181
181
|
end
|
182
182
|
|
183
183
|
def admin_host
|
184
|
+
return ENV['WIREMOCK_URL'].match(/http\:\/\/(.+)\:\d+/)[1] if ENV['WIREMOCK_URL']
|
184
185
|
"#{remote_host ? remote_host : 'localhost'}"
|
185
186
|
end
|
186
187
|
|
187
188
|
def admin_port
|
189
|
+
self.port = ENV['WIREMOCK_URL'].match(/http\:\/\/.+\:(\d*)/)[1] if ENV['WIREMOCK_URL']
|
188
190
|
"#{port ? port.to_s : '8080'}"
|
189
191
|
end
|
190
192
|
|
data/lib/service_mock/version.rb
CHANGED
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.
|
4
|
+
version: '0.7'
|
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: 2017-01-
|
11
|
+
date: 2017-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: childprocess
|
@@ -116,8 +116,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
116
|
version: '0'
|
117
117
|
requirements: []
|
118
118
|
rubyforge_project:
|
119
|
-
rubygems_version: 2.
|
119
|
+
rubygems_version: 2.4.3
|
120
120
|
signing_key:
|
121
121
|
specification_version: 4
|
122
122
|
summary: Simple wrapper over WireMock
|
123
123
|
test_files: []
|
124
|
+
has_rdoc:
|