Name: betamocks
Owner: Department of Veterans Affairs
Description: Better mocking solution inspired by VCR
Created: 2017-08-03 19:50:00.0
Updated: 2017-08-14 17:16:01.0
Pushed: 2018-03-26 14:46:25.0
Homepage: null
Size: 81
Language: Ruby
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Betamocks is Faraday middleware that mocks APIs by recording and replaying requests. It's especially useful for local development to mock out APIs that are behind a VPN (government), often go down (government), or when an API may not have a corresponding dev or staging environment (also government).
Add this line to your application's Gemfile:
'betamocks'
And then execute:
$ bundle
Or install it yourself as:
$ gem install betamocks
In a location that gets loaded before your Faraday connections are initialized (e.g. a Rails initializer)
configure the Betamocks enabled
, cache_dir
, and services_config
settings:
true
or off false
true
or false
, defaults to false
. When true
, unmatched requests are sent out and responses recorded as new mock data. Otherwise, unmatched requests fall back to a default response defined in default.yml
.mocks.configure do |config|
nfig.enabled = true
nfig.cache_dir = File.join(Rails.root, 'config', 'betamocks', 'cache')
nfig.services_config = File.join(Rails.root, 'config', 'betamocks', 'betamocks.yml')
nfig.recording = false
The services config is YAML file containing a list (array) of services. Each item in the services list contains:
/v0/users
.
Wildcards are allowed for varying parameters within a URL e.g. /v0/users/*/forms
will record both /v0/users/42/forms
and /v0/users/101/forms
.vices:
ase_urls:
va.service.that.timesout
int.va.service.that.timesout
ndpoints:
:method: :get
:path: "/v0/users/*/forms"
ase_urls:
bnb.data.bl.uk
ndpoints:
:method: :get
:path: "/doc/resource/*"
Betamocks automatically records multiple unique responses per endpoint. A response is considered unique if any of the following differ:
/v0/users/42/forms
vs /v0/users/101/forms
If the body contains a timestamp that changes on every request, even though the rest of the body remains the same, it will cause Betamocks to record a new cache file rather than loading the existing file. To get around this you can add one or more regular expressions to strip out the timestamp.
For example SOAP request bodies often include a timestamp to ensure that a request is recent.
sionCode code="3.0"/>
ationTime value="20161028101201"/>
eractionId extension="PRPA_IN201306UV02" root="2.16.840.1.113883.1.6"/>
cessingCode code="T"/>
To remove the timestamp in creationTime
include a regular expression that captures the value in the service config file
in this case 14 digits \d{14}
that follow creationTime value=
or creationTime value="(\d{14})"
:
ase_urls:
api.vets.gov
ndpoints:
:method: :post
:path: "/v0/stuffs"
:timestamp_regex:
- creationTime value="(\d{14})"
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.
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/betamocks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.