-
Notifications
You must be signed in to change notification settings - Fork 109
Creating a dynamic mock that wraps an existing API call
Suppose we are building an application that consumes the GitHub API to show information about repositories via the https://api.github.com/repos/:user/:repository endpoint.Suppose also that GitHub is working to enhance this endpoint with additional information about the last 10 commits of a requested repository.
We want to be able to support this enhancement once GitHub releases the feature. We will use DuckRails to wrap the original response with additional fake data.
Create a new mock and fill in the fields of the General tab as shown here:

As you can see, the route path is pretty much the same with the one that we will consume.
Now, on the Response body tab, select Embedded Ruby as Body type and application/json as Content type.
In the Body content fill in:
<%
# Fetch the response from GitHub
original_response = RestClient.get "https://api.github.com/repos/#{@parameters[:username]}/#{@parameters[:repo_name]}" rescue '{}'
result = JSON.parse(original_response)
# Let's enhance with some random data
result[:last_commits] = 10.times.map{|i| {
changes: Random.rand(1000),
date: (Date.today - Random.rand(10)),
hash: Digest::SHA1.hexdigest("DuckRails-#{i}")
}}
%>
<%= result.to_json %>DuckRails is already configured to require RestClient so that it can be used in dynamic mocks.

Keep in mind that if our application was using any additional parameters or headers when consuming the original GitHub endpoint, we can pass them as is to our RestClient call since we have the request's parameters and headers available at the @parameters and @headers variables respectively. Example:
<%
accept_header = @headers['Accept']
# Fetch the response from GitHub
original_response = RestClient.get("https://api.github.com/repos/#{@parameters[:username]}/#{@parameters[:repo_name]}", headers: {'Accept': accept_header}) rescue '{}'
result = JSON.parse(original_response)
# Let's enhance with some random data
result[:last_commits] = 10.times.map{|i| {
changes: Random.rand(1000),
date: (Date.today - Random.rand(10)),
hash: Digest::SHA1.hexdigest("DuckRails-#{i}")
}}
%>
<%= result.to_json %>Let's see what we created. Given we have DuckRails running on port 8080, request: http://localhost:8080/repos/google/google-authenticator. Tadaaaa

- DuckRails - 2017 Wiki pages.
- Feel free to request for documentation that you believe is missing by opening issues with the label
wiki