Some feature tests excercise application logic that includes aysnchronous javascript calls to external services (APIs). In particular, searching for proceedings and organisations is achieved this through ajax calls to the legal-framework-api. VCR does not intercept browser calls therefore puffing-billy is used. This provides a "rewriting web proxy" to intercept and stub the calls.
VCR and puffing-billy can be used together to achieve stubbing of any calls to external services from the backend and frontend respectively.
This service is explictly called from javascript to search for proceedings and organisations. Therefore we must ensure .env.test
has LEGAL_FRAMEWORK_API_HOST
pointing to staging environment.
While it is possible to record and playback requests, using puffing-billy, in a manner similar to vcr, stubbing a request is the preferred option. To add a stub you should add or extend helper methods in the features/support/puffing_billy_helper.rb
and then call that in a cucumber step definition that will make the call you wish to stub.
# features/support/puffing_billy_helper.rb
def stub_my_call
proxy
.stub("https://legal-framework-api-staging.cloud-platform.service.justice.gov.uk:443/proceeding_types/searches", method: "post")
.and_return(
headers: {
"Access-Control-Allow-Origin" => "*",
},
code: 200,
body: { key: "value", another_key: "another_value" }.to_json,
)
end
And use
When("I do something that calls a service via ajax") do
stub_my_call
fill_in("some-input-that-makes-ajax-call", with: 'whatever')
end
If you want to record a request and response, either to use in a feature test or to use to create a stub (before deleting it), you can:
-
comment out/remove any proxy.stubs already in use by the feature
-
amend the puffing-billy config in
features/support/puffing_billy.rb
- set
non_whitelisted_requests_disabled = false
- ensure
cache = true
andpersist_cache = true
- set
-
run the feature (with debug to view request being made of billy's proxy)
DEBUG_BILLY=true cucumber features/providers/applicant_details.feature:3
- The requests should appear in the configured folder
features/puffing-billy/request_cache
Warning: you may also need to configure Billy to record certificates temporarily. You SHOULD NOT commit these to the repo.
# features/support/puffing_billy.rb
Billy.configure do |c|
...
c.certs_path = 'features/puffing-billy/request_certs'
...
end
To rerecord a request cache simply delete the file and rerun the feature.
You can display all calls that puffing-billy proxied using an env var as below.
DEBUG_BILLY=true cucumber features/providers/applicant_details.feature:3