The bdsm Developer Hub

Welcome to the bdsm developer hub. You'll find comprehensive guides and documentation to help you start working with bdsm as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    
 

connect(options)

This method connects Mimic with remote UI

You can connect Mimic to a remote UI url if you wish to run the UI on a separate browser tab.

Please see Remote Debugger Guide on how to use this feature.

Option name
Option value

host

Host to connect to (defaults to localhost)

port

Port to connect to (defaults to 5000)

import connect from 'mimic/remote';

connect();

// or alternatively you can specify the connection options
connect({ host: 'localhost', port: 5000 });
 

setMode(mode)

Allows you to run mimic in different environments.

Currently supported environments: react-native, worker, normal

import API from 'mimic';

API.setMode('react-native')
Suggest Edits

bootstrapWorker

 

bootstrapWorker(worker)

This method registers a web worker with Mimic.

To use web workers with Mimic you must do 2 additional steps so Mimic can capture and mock your requests successfully.
1.In your web worker code, import the mimic worker script

import 'mimic/worker';
  1. In your application when loading the worker, use the bootstrapWorker method to let Mimic know that a worker is running.
import Mimic from 'mimic';

const worker = new Worker('worker.js');
Mimic.bootstrapWorker(worker);
Suggest Edits

capturedRequests

 

get capturedRequests

returns all the network requests Mimic captured.

import Mimic from 'mimic';

console.log(Mimic.capturedRequests);

Output

[
  {
    "id": "d539e560-6728-400c-8f26-e32aa5cf94a8",
    "method": "POST",
    "url": "http://localhost/api/login",
    "params": "{\"user\":\"test\",\"password\":\"test\"}",
    "headers": {
      "Content-Type": "application/json; charset=utf-8",
      "Accept": "application/json, text/javascript, */*; q=0.01"
    },
    "requestHash": "POST|http://localhost/api/login|{\"user\":\"test\",\"password\":\"test\"}",
    "response": {
      "status": 200,
      "delay": 0,
      "headers": {},
      "body": ""
    }
  }
]

get mocks

returns all mocked requests.

import Mimic from 'mimic';

console.log(Mimic.mocks);

Output

[
  {
    "id": "4034db5e-34e1-401c-b023-49ec55cf3028",
    "active": true,
    "method": "GET",
    "url": "http://uinames.com/api/?amount=2&region=russia",
    "headers": {
      "accept": "*/*"
    },
    "response": {
      "delay": 659,
      "status": 500,
      "headers": {
        "pragma": "no-cache",
        "content-type": "application/json; charset=utf-8",
        "cache-control": "no-cache",
        "expires": -1
      },
      "body": "{\n  \"mocked\": true\n}"
    },
    "origin": "http://localhost:8080"
	}
]
Suggest Edits

activeMocks

 

get activeMocks

returns all active mocked requests object definitions.

import Mimic from 'mimic';

console.log(Mimic.activeMocks);

Output

[
  {
    "id": "5741faa6-f8c2-492e-8166-c3925d3bc20c",
    "active": true,
    "method": "GET",
    "url": "http://jsonplaceholder.typicode.com/posts/2",
    "headers": {
      "pragma": "no-cache",
      "content-type": "application/json; charset=utf-8",
      "cache-control": "public, max-age=14400",
      "expires": "Thu, 22 Sep 2016 11:49:01 GMT"
    },
    "response": {
      "delay": 152,
      "status": 200,
      "body": "{\n  \"userId\": 1,\n  \"id\": 2,\n  \"title\": \"qui est esse\",\n  \"body\": \"est rerum tempore vitae\\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\\nqui aperiam non debitis possimus qui neque nisi nulla\"\n}"
    }
  }
]

get groups

returns all groups.

import Mimic from 'mimc';

console.log(Mimic.groups);

Output

[
  {
    "id": "743a78b5-cf0c-4240-bea7-0cc926159876",
    "name": "My Group",
    "active": true
  }
]
Suggest Edits

setAppName

 

setAppName(appName)

Used to set an application context when you want to run multiple applications on the same address e.g localhost:8080.

Once an application name has been set all related mock data will be associated with your application name.

import Mimic from 'mimic';

Mimic.setAppName('MyApp');
Suggest Edits

getAppName

 

getAppName()

Get the current application name, this is used to differentiate between several different applications running on the same address e.g localhost:8080 to have separate mocking data.

If an app name was not defined an empty string will be returned.

Please see setAppName for more details on setting the application name.

import Mimic from 'mimic';

// Assuming app name was set before as 'MyApp'
console.log(Mimic.getAppName()) // Outputs: MyApp

import(jsonString)

Import mocks to Mimic. This function accepts a json string of exported output from Mimic.

import Mimic from 'mimic';

Mimic.import(`{
  "version": "2.0.0",
  "mocks": [
    {
      "id": "a4f91a92-fa12-498d-9481-1da3e0458cf0",
      "active": true,
      "method": "GET",
      "url": "http://jsonplaceholder.typicode.com/posts/4",
      "headers": {},
      "response": {
        "status": 200,
        "delay": 106,
        "headers": {
          "content-type": "application/json"
        },
        "body": "{ \"mocked\": true }"
      },
      "origin": null,
      "groupId": "743a78b5-cf0c-4240-bea7-0cc926159876"
    },
    {
      "id": "6c5af819-ab56-4dad-aace-fbac9fb6e676",
      "active": true,
      "method": "GET",
      "url": "http://jsonplaceholder.typicode.com/posts/3",
      "headers": {},
      "params": "",
      "response": {
        "status": 200,
        "delay": 135,
        "headers": {
          "pragma": "no-cache",
          "content-type": "application/json; charset=utf-8",
          "cache-control": "public, max-age=14400",
          "expires": "Sun, 19 Mar 2017 23:35:47 GMT"
        },
        "body": "{ \"mocked\": true }"
      },
      "origin": "http://localhost:8080",
      "groupId": "743a78b5-cf0c-4240-bea7-0cc926159876"
    }
  ],
  "groups": [
    {
      "id": "743a78b5-cf0c-4240-bea7-0cc926159876",
      "name": "My Group",
      "active": true
    }
  ]
}`)
.then(function (status) {
  console.log({ success: status.success, error: status.error });
});

export(prettify)

Exports all saved scenarios and mocks.
By default this function outputs a minified JSON response, If you want a more readable output you can specify the prettify argument to true.

import Mimic from 'mimic';

Mimic.export(); // Default Output
Mimic.export(true); // Prettified Output

Output

{"version":"2.0.0","mocks":[{"id":"4034db5e-34e1-401c-b023-49ec55cf3028","active":true,"method":"GET","url":"http://uinames.com/api/?amount=2&region=russia","headers":{"accept":"*/*"},"response":{"delay":659,"status":500,"headers":{"pragma":"no-cache","content-type":"application/json; charset=utf-8","cache-control":"no-cache","expires":-1},"body":{"mocked":true}},"origin":"http://localhost:8080"},{"id":"a4f91a92-fa12-498d-9481-1da3e0458cf0","active":true,"method":"GET","url":"http://jsonplaceholder.typicode.com/posts/4","headers":{},"response":{"status":200,"delay":106,"headers":{"content-type":"application/json"},"body":{"mocked":true}},"origin":null,"groupId":"743a78b5-cf0c-4240-bea7-0cc926159876"},{"id":"6c5af819-ab56-4dad-aace-fbac9fb6e676","active":true,"method":"GET","url":"http://jsonplaceholder.typicode.com/posts/3","headers":{},"params":"","response":{"status":200,"delay":135,"headers":{"pragma":"no-cache","content-type":"application/json; charset=utf-8","cache-control":"public, max-age=14400","expires":"Sun, 19 Mar 2017 23:35:47 GMT"},"body":{"mocked":true}},"origin":"http://localhost:8080","groupId":"743a78b5-cf0c-4240-bea7-0cc926159876"}],"groups":[{"id":"743a78b5-cf0c-4240-bea7-0cc926159876","name":"My Group","active":true}]}

Prettified Output

{
  "version": "2.0.0",
  "mocks": [
    {
      "id": "4034db5e-34e1-401c-b023-49ec55cf3028",
      "active": true,
      "method": "GET",
      "url": "http://uinames.com/api/?amount=2&region=russia",
      "headers": {
        "accept": "*/*"
      },
      "response": {
        "delay": 659,
        "status": 500,
        "headers": {
          "pragma": "no-cache",
          "content-type": "application/json; charset=utf-8",
          "cache-control": "no-cache",
          "expires": -1
        },
        "body": {
          "mocked": true
        }
      },
      "origin": "http://localhost:8080"
    },
    {
      "id": "a4f91a92-fa12-498d-9481-1da3e0458cf0",
      "active": true,
      "method": "GET",
      "url": "http://jsonplaceholder.typicode.com/posts/4",
      "headers": {},
      "response": {
        "status": 200,
        "delay": 106,
        "headers": {
          "content-type": "application/json"
        },
        "body": {
          "mocked": true
        }
      },
      "origin": null,
      "groupId": "743a78b5-cf0c-4240-bea7-0cc926159876"
    },
    {
      "id": "6c5af819-ab56-4dad-aace-fbac9fb6e676",
      "active": true,
      "method": "GET",
      "url": "http://jsonplaceholder.typicode.com/posts/3",
      "headers": {},
      "params": "",
      "response": {
        "status": 200,
        "delay": 135,
        "headers": {
          "pragma": "no-cache",
          "content-type": "application/json; charset=utf-8",
          "cache-control": "public, max-age=14400",
          "expires": "Sun, 19 Mar 2017 23:35:47 GMT"
        },
        "body": {
          "mocked": true
        }
      },
      "origin": "http://localhost:8080",
      "groupId": "743a78b5-cf0c-4240-bea7-0cc926159876"
    }
  ],
  "groups": [
    {
      "id": "743a78b5-cf0c-4240-bea7-0cc926159876",
      "name": "My Group",
      "active": true
    }
  ]
}
 

addGroup(Group)

Create a new group

import Mimic from 'mimic';

Mimic.addGroup({ name: 'New Group' active: true });

Output

{
  id: "1d2aa537-f9e7-40d8-af98-1aa7c8cadc3f",
	name: "New Group",
  active: true
}
Suggest Edits

toggleGroup

 

toggleGroup(groupId)

Toggle a group on or off depending on it's current state.

import Mimic from 'mimic';

const group = Mimic.groups[0];

console.log(group.active); // true
Mimic.toggleGroup(group.id);

console.log(group.active); // false
Suggest Edits

updateGroup

 

updateGroup(groupId, group)

Update a group

import Mimic from 'mimic';

const group = Mimic.groups[0];
Mimic.updateGroup(group.id, { name: 'New Name' });
Suggest Edits

removeGroup

 

removeGroup(groupId)

Remove a group

import Mimic from 'mimic';

Mimic.removeGroup('1d2aa537-f9e7-40d8-af98-1aa7c8cadc3f');
Suggest Edits

duplicateGroup

 

duplicateGroup(groupId)

Duplicate an existing group including all of its mocks.

import Mimic from 'mimic';

const group = Mimic.groups[0];
Mimic.duplicateGroup(group.id);
Suggest Edits

mockRequest

 

mockRequest(request: Request)

Mock an http request.

See Request for the request object structure.

import Mimic from 'mimic';

Mimic.mockRequest({
  method: 'get',
  url: 'http://example.com',
  params: { userId: 5 },
  headers: { 'Authorization': 'Basic token' },
  response: {
    status: 200,
    delay: 0,
    headers: { 'Content-Type': 'application/json' },
    body: '{ text: "hello world" }'
  }
});
Suggest Edits

updateMock

 

updateMock(mockId, request)

update a mock.

import Mimic from 'mimic';

Mimic.updateMock('mock-id', {
  method: 'get',
  url: 'http://example.com',
  params: { userId: 5 },
  headers: { 'Authorization': 'Basic token' },
  response: {
    status: 200,
    delay: 0,
    headers: { 'Content-Type': 'application/json' },
    body: '{ text: "hello world" }'
  }
});
Suggest Edits

removeMock

 

removeMock(mockId)

Remove a mock.

import Mimic from 'mimic';

Mimic.removeMock('mock-id');
Suggest Edits

toggleMock

 

toggleMock(mockId)

Toggle a mock on or off based on its current state.

import Mimic from 'mimic';

Mimic.toggleMock('mock-id');
Suggest Edits

clearStorage

 

clearStorage()

Remove all Mimic data for the current application scope (see setAppName).

This action is irreversible

Once data has been cleared it cannot be undone unless you have an exported backup.
To export your data see export

import Mimic from 'mimic';

Mimic.clearStorage();

on(event, callback, context)

Listen to changes in Mimic data, for a list of events please see Events.

import Mimic from 'mimic';

// without context
Mimic.on('REQUEST_CAPTURED', () => console.log('a request has been captured')); 
// outputs: a request has been captured

// with context
const context = { foo: 'bar' };
Mimic.on('REQUEST_CAPTURED', () => console.log(this.foo), context); 
// outputs: bar

once(event, callback, context)

Listen to changes in Mimic data, the callback will only be called one time.
for a list of events please see Events

import Mimic from 'mimic';

// without context
Mimic.once('REQUEST_CAPTURED', () => console.log('a request has been captured')); 
// outputs: a request has been captured

// with context
const context = { foo: 'bar' };
Mimic.once('REQUEST_CAPTURED', () => console.log(this.foo), context); 
// outputs: bar

off(event, callback, context)

Remove a registered event listener. For a list of events please see Events

import Mimic from 'mimic';

const handleCapturedRequest = () => console.log('a request has been captured'));

// first we must register the handler
Mimic.on('REQUEST_CAPTURED', handleCapturedRequest);

// Now we can remove the handler by passing a reference to same function
Mimic.off('REQUEST_CAPTURED', handleCapturedRequest);
Suggest Edits

exportMocks

 

exportMocks(mockIds, groupIds, prettify)

Exports selected mocks and groups.
By default this function outputs a minified JSON response, If you want a more readable output you can specify the prettify argument to true.

import Mimic from 'mimic';

Mimic.exportMocks(['mock-id'], ['group-id']); // Default Output
Mimic.exportMocks(['mock-id'], ['group-id'], true); // Prettified Output

Output

{"version":"2.0.0","mocks":[{"id":"4034db5e-34e1-401c-b023-49ec55cf3028","active":true,"method":"GET","url":"http://uinames.com/api/?amount=2&region=russia","headers":{"accept":"*/*"},"response":{"delay":659,"status":500,"headers":{"pragma":"no-cache","content-type":"application/json; charset=utf-8","cache-control":"no-cache","expires":-1},"body":{"mocked":true}},"origin":"http://localhost:8080"},{"id":"a4f91a92-fa12-498d-9481-1da3e0458cf0","active":true,"method":"GET","url":"http://jsonplaceholder.typicode.com/posts/4","headers":{},"response":{"status":200,"delay":106,"headers":{"content-type":"application/json"},"body":"{ \"mocked\": true }"},"origin":null,"groupId":"743a78b5-cf0c-4240-bea7-0cc926159876"},{"id":"6c5af819-ab56-4dad-aace-fbac9fb6e676","active":true,"method":"GET","url":"http://jsonplaceholder.typicode.com/posts/3","headers":{},"params":"","response":{"status":200,"delay":135,"headers":{"pragma":"no-cache","content-type":"application/json; charset=utf-8","cache-control":"public, max-age=14400","expires":"Sun, 19 Mar 2017 23:35:47 GMT"},"body":"{ \"mocked\": true }"},"origin":"http://localhost:8080","groupId":"743a78b5-cf0c-4240-bea7-0cc926159876"}],"groups":[{"id":"743a78b5-cf0c-4240-bea7-0cc926159876","name":"My Group","active":true}]}

Prettified Output

{
  "version": "2.0.0",
  "mocks": [
    {
      "id": "4034db5e-34e1-401c-b023-49ec55cf3028",
      "active": true,
      "method": "GET",
      "url": "http://uinames.com/api/?amount=2&region=russia",
      "headers": {
        "accept": "*/*"
      },
      "response": {
        "delay": 659,
        "status": 500,
        "headers": {
          "pragma": "no-cache",
          "content-type": "application/json; charset=utf-8",
          "cache-control": "no-cache",
          "expires": -1
        },
        "body": {
          "mocked": true
        }
      },
      "origin": "http://localhost:8080"
    },
    {
      "id": "a4f91a92-fa12-498d-9481-1da3e0458cf0",
      "active": true,
      "method": "GET",
      "url": "http://jsonplaceholder.typicode.com/posts/4",
      "headers": {},
      "response": {
        "status": 200,
        "delay": 106,
        "headers": {
          "content-type": "application/json"
        },
        "body": "{ \"mocked\": true }"
      },
      "origin": null,
      "groupId": "743a78b5-cf0c-4240-bea7-0cc926159876"
    },
    {
      "id": "6c5af819-ab56-4dad-aace-fbac9fb6e676",
      "active": true,
      "method": "GET",
      "url": "http://jsonplaceholder.typicode.com/posts/3",
      "headers": {},
      "params": "",
      "response": {
        "status": 200,
        "delay": 135,
        "headers": {
          "pragma": "no-cache",
          "content-type": "application/json; charset=utf-8",
          "cache-control": "public, max-age=14400",
          "expires": "Sun, 19 Mar 2017 23:35:47 GMT"
        },
        "body": "{ \"mocked\": true }"
      },
      "origin": "http://localhost:8080",
      "groupId": "743a78b5-cf0c-4240-bea7-0cc926159876"
    }
  ],
  "groups": [
    {
      "id": "743a78b5-cf0c-4240-bea7-0cc926159876",
      "name": "My Group",
      "active": true
    }
  ]
}
Suggest Edits

startRecording

 

startRecording()

Start automatic mocking of all outgoing requests.

See stopRecording for stopping automatic recording.

import Mimic from 'mimic';

Mimic.startRecording();
Suggest Edits

stopRecording

 

stopRecording()

Stop automatic mocking of outgoing requests. See record

import Mimic from 'mimic';

Mimic.stopRecording();
Suggest Edits

getCapturedRequest

 

getCapturedRequest(requestId)

Returns a captured request by Id

import Mimic from 'mimic';

Mimic.getCapturedRequest('request-id');

Output

{
  "id": "609bdfcd-eb83-4d57-8e84-8823258fcd9a",
  "method": "GET",
  "url": "http://jsonplaceholder.typicode.com/posts/1",
  "headers": {
    "accept": "*/*"
  },
  "origin": "http://localhost:8080",
  "startTime": 1495787559607,
  "response": {
    "status": 200,
    "delay": 754,
    "headers": {
      "pragma": "no-cache",
      "content-type": "application/json; charset=utf-8",
      "cache-control": "public, max-age=14400",
      "expires": "Fri, 26 May 2017 12:32:40 GMT"
    },
    "body": "{\n  \"userId\": 1,\n  \"id\": 1,\n  \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",\n  \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto\"\n}"
  }
}
 

getMock(mockId)

returns a mock by Id

import Mimic from 'mimic';

Mimic.getMock('mock-id')

Output

{
  "id": "4034db5e-34e1-401c-b023-49ec55cf3028",
  "active": true,
  "method": "GET",
  "url": "http://uinames.com/api/?amount=2&region=russia",
  "headers": {
    "accept": "*/*"
  },
  "response": {
    "delay": 659,
    "status": 500,
    "headers": {
      "pragma": "no-cache",
      "content-type": "application/json; charset=utf-8",
      "cache-control": "no-cache",
      "expires": -1
    },
    "body": "{\n  \"mocked\": true\n}"
  },
  "origin": "http://localhost:8080"
}
 

getGroup(groupId)

returns a group by id

import Mimic from 'mimic';

Mimic.getGroup('group-id');

Output

{
  "id": "743a78b5-cf0c-4240-bea7-0cc926159876",
  "name": "My Group",
  "active": true
}
Suggest Edits

importUrl

 

importUrl(url, options, done)

Imports a mock from an external url

import Mimic from 'mimic';

function importCallback(status) {
 console.log(status.success, status.error)
}

mimic.importUrl('http://example.com/mock.json', { append: true }, importCallback)
Suggest Edits

duplicateMock

 

duplicateMock

Duplicate a mock by id

import Mimic from 'mimic';

Mimic.duplicateMock('mock-id')
Suggest Edits

getMatchingMocks

 

getMatchingMocks(request)

Get all matching mocks for a request

import Mimic from 'mimic';

const request = Mimic.capturedRequests[0];

Mimic.getMatchingMocks(request);
 

The Request class defines HTTP requests that BDSM has captured

Property
Type
Example Value
Required

method

string

get

Yes

url

string

Yes

params

object

{ userId: 5 }

No

headers

object

{ Authorization: 'Basic token' }

No

Response

Yes

origin

string

No

startTime

number

1495786794

No

 

The response class defines how Mimic should respond to a request

Property
Type
Example Value
Required

status

number

200

Yes

Delay

number

0

Yes

headers

object

{ 'Content-Type': 'application/json' }

No

body

string

Hello World

Yes

Here you can find a list of all the events Mimic will output, you can listen to these events using the on or once methods.

Event Name
Description

SCENARIO_ADD

A scenario has been created

SCENARIO_RENAME

A scenario has been renamed

SCENARIO_REMOVE

A scenario has been removed

SCENARIO_DUPLICATE

A scenario has been duplicated

SCENARIO_SET_ACTIVE

A scenario has been set to active

SCENARIO_CHANGE

A scenario data has changed

SCENARIO_ADD_MOCKED_REQUEST

A scenario was added a new mock

REQUEST_CAPTURED

Mimic has captured an HTTP request

MOCKED_REQUESTS_ADD

A mocked request has been created

IMPORT

Mimic has imported additional data

STORAGE_PERSIST

Mimic is persisting it's data