-
Notifications
You must be signed in to change notification settings - Fork 108
Using the mock form
One of the key pages of DuckRails is the mock form.
Here we describe the purpose and the functionality of each field of the form.
Navigate to DuckRails' home page and press the button Create new mock
.
You should now be in the new mock form with the General
tab selected.
The name of the mock. This field does not affect the mock behavior. We use it so that we can locate the mock later on in the View all mocks
page.
This check box defines whether our mock will be active or not. Inactive mocks will not be being server by DuckRails.
A description for this mock. This field does not affect the mock behavior. Adding something here will help you or your colleagues understand the purpose of this mock.
This is an important field since it defines via which HTTP method the mock's endpoint will be being served.
The response status code. For example, fill in 200
for OK, 400
for Bad Request etc.
This is one of the most important fields too since it defines the endpoint of the mock. Fill in a relative path here, for example /my-mocks
Defines the way that the response's body will be resolved. There are three options:
-
Static: whatever filled in in the
Body content
field, will be rendered as the response body as is -
Embedded Ruby: whatever filled in in the
Body content
field, will be evaluated as ruby code and the result of the script will be the response's body -
Javascript: whatever filled in in the
Body content
field, will be evaluated as javascript code and the result of the script will be the response's body
Defines the content type header of the response, a.k.a. the media type of the resource.
The content of the response. What filled in in this field, will either be served as is (static content) or it will be dynamically evaluated based on the body type
selection.
<h1>Hello world!</h1>
Read more on how to create a simple static mock.
<%= "<h1>Hello World! It's #{DateTime.now}</h1>" %>
Read more on how to create a dynamic mock with embedded ruby.
// Code taken from https://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
return "<h1>Hello world! It's {0}.</h1>".format(new Date().toDateString());
Read more on how to create a dynamic mock with javascript.
Link to add new header in the response.
The name of the header. Example value: Content-Type
.
Note: Adding a content type here, will override the Content type value that was selected in the Response body tab
The advanced section of the form gives us the ability to customize
- the headers
- the status
- the content type &
- the body
of the response either statically or dynamically.
Whatever filled in in the Script
field will be parsed as JSON and depending on the resolved object's keys, the related previously defined values will be potentially overriden.
If the script evaluates to the following JSON object
{
"body": "<h1>Quack</h1>",
"status_code": 201
}
the values that were filled in in the Body content
field of the Response body
and in the Status
field of the General
tab will be overridden.
If the script evaluates to the following JSON object
{
"headers": [
{
"name": "Server-Time",
"value": "<%= DateTime.now %>"
}
]
}
the response is going to have a header with name "Server-Time" and value the time of the server computed the moment the request took place.
Read more on how to create an advanced mock.
The way the script will be evaluated as JSON.
- Embedded ruby: the script will be evaluated to JSON as embedded ruby
- Javascript the script will be evaluated to JSON as Javascript
- Static the script will be evaluated to JSON as is
The script that will be evaluated to JSON and used to override properties of the response.
{
"headers": [
{
"name": "Server-Time",
"value": "<%= DateTime.now %>"
}
]
}
obj = {};
obj['headers'] = [];
obj['headers'].push({"name": "Server-Time", "value":new Date().toString()});
return JSON.stringify(obj);
{
"body": "Hello world!!!"
}
Next: Creating mocks
- DuckRails - 2017 Wiki pages.
- Feel free to request for documentation that you believe is missing by opening issues with the label
wiki