Skip to content

Using the mock form

Lazarus Lazaridis edited this page Nov 16, 2017 · 4 revisions

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. DuckRails home page

General tab

You should now be in the new mock form with the General tab selected. DuckRails new mock form - General tab

Name

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.

Active

This check box defines whether our mock will be active or not. Inactive mocks will not be being server by DuckRails.

Description

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.

Request method

This is an important field since it defines via which HTTP method the mock's endpoint will be being served.

Status

The response status code. For example, fill in 200 for OK, 400 for Bad Request etc.

Route path

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

Response body tab

DuckRails new mock form - Response body tab

Body type

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

Content type

Defines the content type header of the response, a.k.a. the media type of the resource.

Body content

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.

Example values based on the body type

Static

<h1>Hello world!</h1>

Read more on how to create a simple static mock.

Embedded Ruby

<%= "<h1>Hello World! It's #{DateTime.now}</h1>" %>

Read more on how to create a dynamic mock with embedded ruby.

Javascript

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

Headers tab

DuckRails new mock form - Headers tab

Add header

Link to add new header in the response.

Name

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

Advanced tab

DuckRails new mock form - Advanced tab The advanced section of the form gives us the ability to customize

  1. the headers
  2. the status
  3. the content type &
  4. the body

of the response either statically or dynamically.

How it works

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.

Examples

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.

Script type

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

Script

The script that will be evaluated to JSON and used to override properties of the response.

Examples of scripts based on the script type

Embedded ruby script type

{
  "headers": [
    {
      "name": "Server-Time",
      "value": "<%= DateTime.now %>"
    }
  ]
}

Javascript script type

obj = {};
obj['headers'] = [];
obj['headers'].push({"name": "Server-Time", "value":new Date().toString()});

return JSON.stringify(obj);

Static script type

{
  "body": "Hello world!!!"
}

Next: Creating mocks