department-of-veterans-affairs/betamocks

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Betamocks

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).

Installation

Add this line to your application's Gemfile:

'betamocks'

And then execute:

$ bundle

Or install it yourself as:

$ gem install betamocks
Usage
Configuration

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:

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

Services config

The services config is YAML file containing a list (array) of services. Each item in the services list contains:

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/*"
Special considerations for request bodies with timestamps

Betamocks automatically records multiple unique responses per endpoint. A response is considered unique if any of the following differ:

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})"
Development

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.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.