service_mock 0.7 → 0.7.1

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: babc9682df466187ff0a09472b45f1f08b56e985
4
- data.tar.gz: 556344bb1625cab5e3de9b20537a0a7359533763
3
+ metadata.gz: e9c6b2185d2d52eeda2b5b5a5487d766cdd9d7bc
4
+ data.tar.gz: f4c1fd46633aafffd94ad7345be13785cc6f731a
5
5
  SHA512:
6
- metadata.gz: 0cc09426b3f7fd4c5760684279cc9725568845a2b163f90656b5221097eb9110bee32b6d71ea2ac9a11a8595eb4affa17efb785f03f50b5faf60e23d47b9dcca
7
- data.tar.gz: 71c96c21493db96c7f5563f2350fe5717276b4f56037505001ce03a003de831653a651069284ceb906008ffd93dab452193d98c0f32fe4b196300ad5486f0921
6
+ metadata.gz: f92c202ccdae0f705052dc2b0dfa198d7616bb9e26da55439eea0bb87661a057da039db7f5a800ba532313fdefb32269829565fcc3584da65e457667f8a77c11
7
+ data.tar.gz: 5f8082f11c4bf0c7f1d798ff3d8a0e46ab0e63f6ce0e78bc9215a1f3b490664e8bcce32b593a22e1c5f108e7dc1cb03dbbd26a5500a949d403b615544a8706ef
data/ChangeLog CHANGED
@@ -1,4 +1,10 @@
1
- === Release 0.7
1
+ === Release 0.7.1 / 21-Aug-2017
2
+ * Added ability to specify the stub creator file via Cucumber tags
3
+ * Added count methods to retrieve the count for a specific request - thanks Jarl Friis
4
+ * Fixed issue where rake was required even when rake was not part of bundle - thanks Jarl Friis
5
+ * Added support for the extension option when starting the server - thanks Vinh Bachsy
6
+
7
+ === Release 0.7 / 23-Jan-2017
2
8
  * Added ability to set remote_host and port via WIREMOCK_URL environment variable
3
9
 
4
10
  === Release 0.6 / 9-Jan-2017
@@ -31,4 +37,4 @@
31
37
  * Reorganized the imports to make it easier to utilize the rake tasks
32
38
 
33
39
  === Release 0.1 / 15-Jun-2016
34
- * Initial release with basic functionality
40
+ * Initial release with basic functionality
data/README.md CHANGED
@@ -65,7 +65,7 @@ You will initialize the server like this:
65
65
  ```ruby
66
66
  # uses default working directory
67
67
  my_server = ServiceMock::Server.new('standalone-2.0.10-beta')
68
-
68
+
69
69
  # or this sets the working directory
70
70
  my_server = ServiceMock::Server.new('standalone-2.0.10-beta', '/path/to/jar')
71
71
  ```
@@ -113,18 +113,35 @@ The values that can be set are:
113
113
  | enable_browser_proxy | Run as a browser proxy |
114
114
  | no_request_journal | Disable the request journal, which records incoming requests for later verification |
115
115
  | max_request_journal_entries | Sets maximum number of entries in request journal. When this limit is reached oldest entries will be discarded |
116
+ | classpath | Provides list of dependencies jar files to run with the standalone server, they could be extensions and their dependencies and should be kept in the same working directory with the standalone jar file. |
117
+ | extensions | Provides list of wiremock extensions main class to run with the standalone server. |
116
118
 
117
119
  In addition, as mentioned before, you can set the `inherit_io` and `wait_for_process` options
118
120
  to `true` inside of the block.
119
121
 
122
+ ### Starting the Server with extensions
123
+
124
+ To start WireMock with extensions, we save the extensions and dependencies jar files in the working directory (default is `config/mocks`) and provide the `classpath` and `extensions` as following:
125
+
126
+ ```ruby
127
+ my_server.start do |server|
128
+ server.port = 8081
129
+ server.record_mappings = true
130
+ server.root_dir = /path/to/root
131
+ server.verbose = true
132
+ server.classpath = ['asm-1.0.2.jar', 'json-smart-2.2.1.jar', 'wiremock-body-transformer-1.1.1.jar']
133
+ server.extensions = 'com.opentable.extension.BodyTransformer'
134
+ end
135
+ ```
136
+
120
137
  ### Stubbing service calls
121
138
 
122
- Please read the [WireMock stubbing](http://wiremock.org/stubbing.html)
139
+ Please read the [WireMock stubbing](http://wiremock.org/stubbing.html)
123
140
  documentation so you understand the message structures that are used.
124
141
  It is very important that you understand this first. Those details will
125
142
  not be covered here.
126
143
 
127
- There are three methods that can be used to stub services.
144
+ There are three methods that can be used to stub services.
128
145
 
129
146
  ```ruby
130
147
  my_server.stub(string_containing_json_stub)
@@ -136,7 +153,7 @@ my_server.stub_with_erb(erbfile_containing_json_stub, hash_with_values)
136
153
 
137
154
  The first method is pretty self-explanatory. You simply pass a string
138
155
  that contains the json stub and the server contacts the running server
139
- process and sets it up. The second method takes a full path to a file
156
+ process and sets it up. The second method takes a full path to a file
140
157
  that contains the json stub.
141
158
 
142
159
  The third method is where things get interesting. It allows you to provide
@@ -153,8 +170,8 @@ The third method takes the full path to an erb file that contains the
153
170
  json stub and a second parameter that is a `Hash` which provides the
154
171
  data that is used to fill in the data elements in the template.
155
172
 
156
- If you set the port value when you started the server you will need to
157
- do the same via a block when stubbing.
173
+ If you set the port value when you started the server you will need to
174
+ do the same via a block when stubbing.
158
175
 
159
176
  ```ruby
160
177
  my_server.stub(string_containing_json_stub) do |server|
@@ -178,18 +195,31 @@ variable `WIREMOCK_URL`. It should take the form 'http://hostname:port'.
178
195
 
179
196
  ### Stubbing multiple services
180
197
 
181
- It is often necessary to stub multiple service calls in order to
198
+ It is often necessary to stub multiple service calls in order to
182
199
  complete a test. ServiceMock has created a simple way to do this.
183
200
  It is implemented in a class named `ServiceMock::StubCreator`.
184
201
  This class has a single public method `create_stubs_with` which
185
- takes the name of the name of a file that has the data for all
202
+ takes the name of the file that has the data for all
186
203
  of the stubs you wish to create and optional data to merge with
187
- that file. Also, when you create an instance of the class it
204
+ that file. Also, when you create an instance of the class it
188
205
  requires you to pass an instance of `ServiceMock::Server`.
189
206
 
207
+ Another way to specify the file to use for creating the stubs is
208
+ to set a tag on the Cucumber Scenario. Create a tag in the form of
209
+ `@servicemock_FILENAME` where `FILENAME` is the name of the
210
+ file you wish to use. For example, if you add the tag
211
+ `@servicemock_foo` then the file `foo.yml` will be loaded. The next
212
+ thing you will need to do is add the following code in a hook:
213
+
214
+ ```ruby
215
+ Before do |scenario|
216
+ ServiceMock.load_for_scenario(scenario)
217
+ end
218
+ ```
219
+
190
220
  At this time you need to setup everything in a specific directory
191
221
  structure. The directory it looks for is `config/mocks/stubs`.
192
- Inside that directory it looks for two additional directories -
222
+ Inside that directory it looks for two additional directories -
193
223
  `data` and `templates`. The data file needed by the call needs
194
224
  be located in the `data` directory and the `ERB` files (templates)
195
225
  that it references needs to be in the `templates` directory.
@@ -201,7 +231,7 @@ with an example.
201
231
  service1.erb:
202
232
  first_name: Mickey
203
233
  last_name: Mouse
204
-
234
+
205
235
  service2.erb:
206
236
  username: mmouse
207
237
  password: secret
@@ -241,11 +271,11 @@ my_server.stop
241
271
  ```
242
272
 
243
273
  Stops the running WireMock server if it is running locally.
244
-
274
+
245
275
  ```ruby
246
276
  my_server.save
247
277
  ```
248
-
278
+
249
279
  Writes all of the stubs to disk so they are available the next time
250
280
  the server starts.
251
281
 
@@ -333,17 +363,17 @@ Or install it yourself as:
333
363
 
334
364
  ## Development
335
365
 
336
- After checking out the repo, run `bin/setup` to install dependencies.
337
- Then, run `rake spec` to run the tests. You can also run `bin/console`
366
+ After checking out the repo, run `bin/setup` to install dependencies.
367
+ Then, run `rake spec` to run the tests. You can also run `bin/console`
338
368
  for an interactive prompt that will allow you to experiment.
339
369
 
340
370
 
341
371
 
342
372
  ## Contributing
343
373
 
344
- Bug reports and pull requests are welcome on GitHub at
345
- https://github.com/cheezy/service_mock. This project is intended to
346
- be a safe, welcoming space for collaboration, and contributors are
347
- expected to adhere to the [Contributor Covenant](http://contributor-covenant.org)
374
+ Bug reports and pull requests are welcome on GitHub at
375
+ https://github.com/cheezy/service_mock. This project is intended to
376
+ be a safe, welcoming space for collaboration, and contributors are
377
+ expected to adhere to the [Contributor Covenant](http://contributor-covenant.org)
348
378
  code of conduct.
349
379
 
data/lib/service_mock.rb CHANGED
@@ -4,7 +4,7 @@ require 'service_mock/command_line_options'
4
4
  require 'service_mock/server'
5
5
  require 'service_mock/erb_methods'
6
6
  require 'service_mock/stub_creator'
7
- require 'service_mock/rake/rake_tasks'
7
+ require 'service_mock/rake/rake_tasks' if Gem.loaded_specs.has_key? 'rake'
8
8
 
9
9
  #
10
10
  # ServiceMock is a Ruby gem that provides a wrapper over the [WireMock](http://wiremock.org)
@@ -5,9 +5,9 @@ module ServiceMock
5
5
  :match_headers, :proxy_via, :https_keystore, :keystore_password,
6
6
  :enable_browser_proxying, :preserve_host_header, :https_truststore,
7
7
  :truststore_password, :https_require_client_cert, :no_request_journal,
8
- :max_request_journal_entries]
8
+ :max_request_journal_entries, :extensions]
9
9
  NOT_IMPLEMENTED = [:container_threads, :jetty_acceptor_threads, :jetty_accept_queue_size,
10
- :jetty_header_buffer_size, :extensions, :help]
10
+ :jetty_header_buffer_size, :help]
11
11
 
12
12
  attr_accessor *OPTIONS
13
13
 
@@ -85,5 +85,9 @@ module ServiceMock
85
85
  def max_request_journal_entries_command
86
86
  ["--max-request-journal-entries #{max_request_journal_entries}"]
87
87
  end
88
+
89
+ def extensions_command
90
+ ['--extensions', extensions]
91
+ end
88
92
  end
89
- end
93
+ end
@@ -1,5 +1,6 @@
1
1
  require 'childprocess'
2
2
  require 'net/http'
3
+ require 'json'
3
4
  require 'erb'
4
5
  require 'ostruct'
5
6
  require 'service_mock/erb_methods'
@@ -42,7 +43,7 @@ module ServiceMock
42
43
  class Server
43
44
  include CommandLineOptions
44
45
 
45
- attr_accessor :inherit_io, :wait_for_process, :remote_host
46
+ attr_accessor :inherit_io, :wait_for_process, :remote_host, :classpath
46
47
  attr_reader :wiremock_version, :working_directory, :process
47
48
 
48
49
  def initialize(wiremock_version, working_directory = ::ServiceMock.working_directory)
@@ -89,7 +90,8 @@ module ServiceMock
89
90
  #
90
91
  def start
91
92
  yield self if block_given?
92
- start_process
93
+ classpath = self.classpath.is_a?(Array) ? self.classpath : []
94
+ start_process(classpath)
93
95
  end
94
96
 
95
97
  #
@@ -131,6 +133,38 @@ module ServiceMock
131
133
  stub(erb_content)
132
134
  end
133
135
 
136
+ #
137
+ # Get the count for the request criteria
138
+ #
139
+ def count(request_criteria)
140
+ return if ::ServiceMock.disable_stubs
141
+ yield self if block_given?
142
+ return JSON.parse(http.post('/__admin/requests/count', request_criteria).body)['count']
143
+ end
144
+
145
+ #
146
+ # Get the count for the request criteria in the provided filename.
147
+ #
148
+ def count_with_file(filename)
149
+ return if ::ServiceMock.disable_stubs
150
+ yield self if block_given?
151
+ content = File.open(filename, 'rb') { |file| file.read }
152
+ return count(content)
153
+ end
154
+
155
+ #
156
+ # Get the count for the request criteria using the erb template
157
+ # provided. The +Hash+ second parameter contains the values to be
158
+ # inserted into the +ERB+.
159
+ #
160
+ def count_with_erb(filename, hsh={})
161
+ return if ::ServiceMock.disable_stubs
162
+ yield self if block_given?
163
+ template = File.open(filename, 'rb') { |file| file.read }
164
+ erb_content = ERB.new(template).result(data_binding(hsh))
165
+ count(erb_content)
166
+ end
167
+
134
168
  #
135
169
  # Writes all of the stubs to disk so they are available the next time
136
170
  # the server starts.
@@ -164,16 +198,18 @@ module ServiceMock
164
198
  OpenStruct.new(hsh).instance_eval { binding }
165
199
  end
166
200
 
167
- def start_process
168
- @process = ChildProcess.build(*(start_command + command_line_options))
201
+ def start_process(classpath)
202
+ @process = ChildProcess.build(*(start_command(classpath) + command_line_options))
169
203
  @process.cwd = working_directory
170
204
  @process.io.inherit! if inherit_io
171
205
  @process.start
172
206
  @process.wait if wait_for_process
173
207
  end
174
208
 
175
- def start_command
176
- ['java', '-jar', "wiremock-#{wiremock_version}.jar"]
209
+ def start_command(classpath)
210
+ dependency_jars = classpath + ["wiremock-#{wiremock_version}.jar"]
211
+ ['java', '-cp', dependency_jars.join(':'),
212
+ 'com.github.tomakehurst.wiremock.standalone.WireMockServerRunner']
177
213
  end
178
214
 
179
215
  def http
@@ -56,6 +56,11 @@ module ServiceMock
56
56
  create_stubs
57
57
  end
58
58
 
59
+ def load_for_scenario(scenario)
60
+ filename = scenario_filename(scenario)
61
+ create_stubs_with("#{filename.last}.yml")
62
+ end
63
+
59
64
  private
60
65
 
61
66
  def read_data(data_file)
@@ -90,5 +95,10 @@ module ServiceMock
90
95
  raise error_message
91
96
  end
92
97
  end
98
+
99
+ def scenario_filename(scenario)
100
+ tags = scenario.send(scenario.respond_to?(:tags) ? :tags : :source_tags)
101
+ tags.map(&:name).select { |t| t =~ /@servicemock_/ }.map { |t| t.gsub('@servicemock_', '').to_sym }
102
+ end
93
103
  end
94
104
  end
@@ -1,3 +1,3 @@
1
1
  module ServiceMock
2
- VERSION = "0.7"
2
+ VERSION = "0.7.1"
3
3
  end
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.7'
4
+ version: 0.7.1
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-23 00:00:00.000000000 Z
11
+ date: 2017-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: childprocess
@@ -116,9 +116,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  requirements: []
118
118
  rubyforge_project:
119
- rubygems_version: 2.4.3
119
+ rubygems_version: 2.5.2
120
120
  signing_key:
121
121
  specification_version: 4
122
122
  summary: Simple wrapper over WireMock
123
123
  test_files: []
124
- has_rdoc: