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": ""
    }
  }
]
Suggest Edits

mockedRequests

 

get mockedRequests

returns all mocked requests.

import Mimic from 'mimic';

console.log(Mimic.mockedRequests);

Output

[
  {
    "id": "5741faa6-f8c2-492e-8166-c3925d3bc20c",
    "active": false,
    "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}"
    }
  }
]
Suggest Edits

activeMockedRequests

 

get activeMockedRequests

returns all active mocked requests object definitions.

import Mimic from 'mimic';

console.log(Mimic.mockedRequests);

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}"
    }
  }
]
Suggest Edits

scenarios

 

get scenarios

returns all scenarios.

import Mimic from 'mimc';

console.log(Mimic.scenarios);

Output

[
  {
    "id": "default-scenario",
    "name": "Default Scenario",
    "active": true,
    "mockedRequests": [
      {
        "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}"
        }
      }
    ]
  }
]
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": "1.0.2",
  "scenarios": [
    {
      "id": "default-scenario",
      "name": "Default Scenario",
      "active": true,
      "mockedRequests": [
        {
          "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": {
              "userId": 1,
              "id": 2,
              "title": "qui est esse",
              "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"
            }
          }
        }
      ]
    }
  ]
}`);

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":"1.0.2","scenarios":[{"id":"default-scenario","name":"Default Scenario","active":true,"mockedRequests":[{"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":{"userId":1,"id":2,"title":"qui est esse","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"}}}]}]}

Prettified Output

{
  "version": "1.0.2",
  "scenarios": [
    {
      "id": "default-scenario",
      "name": "Default Scenario",
      "active": true,
      "mockedRequests": [
        {
          "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": {
              "userId": 1,
              "id": 2,
              "title": "qui est esse",
              "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"
            }
          }
        }
      ]
    }
  ]
}
Suggest Edits

addScenario

 

addScenario(name)

Create a new scenario

import Mimic from 'mimic';

Mimic.addScenario('New Scenario');
Suggest Edits

toggleScenario

 

toggleScenario(id)

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

import Mimic from 'mimic';

const scenario = Mimic.scenarios[0];

console.log(scenario.active); // true
Mimic.toggleScenario(scenario.id);

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

renameScenario

 

renameScenario(scenarioId, newName)

Rename an existing scenario name

import Mimic from 'mimic';

const scenario = Mimic.scenarios[0];
Mimic.renameScenario(scenario.id, 'My Awesome Scenario');
Suggest Edits

removeScenario

 

removeScenario(scenarioId)

Remove a scenario

import Mimic from 'mimic';

Mimic.removeScenario('my-scenario-id');
Suggest Edits

duplicateScenario

 

duplicateScenario(scenarioId)

Duplicate an existing scenario including all of its mocked requests.

import Mimic from 'mimic';

const scenario = Mimic.scenarios[0];
Mimic.duplicateScenario(scenario.id);
Suggest Edits

mockRequest

 

mockRequest(scenarioId, request: Request)

Mock an http request and assign it to a scenario.

See Request for the request object structure.

import Mimic from 'bdsm';

Mimic.mockRequest('scenario-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

updateMockedRequest

 

updateMockedRequest(scenarioId, mockRequestId, request)

update a currently mocked request.

import Mimic from 'mimic';

Mimic.updateMockedRequest('scenario-id', '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

removeMockedRequest

 

removeMockedRequest(scenarioId, mockedRequestId)

Remove a mocked request.

import Mimic from 'mimic';

Mimic.removeMockedRequest('scenario-id', 'mock-id');
Suggest Edits

toggleMockedRequest

 

toggleMockedRequest(scenarioId, mockedRequestId)

Toggle a mocked request on or off.

import Mimic from 'mimic';

Mimic.toggleMockedRequest('scenario-id', '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

exportMock

 

exportMock(scenarioId, mockId, prettify)

Exports a single mock within a scenario.
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.exportMock('default-scenario', 'a342-asd324'); // Default Output
Mimic.exportMock('default-scenario', 'a342-asd324', true); // Prettified Output

Output

{"version":"1.0.2","scenarios":[{"id":"default-scenario","name":"Default Scenario","active":true,"mockedRequests":[{"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":{"userId":1,"id":2,"title":"qui est esse","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"}}}]}]}

Prettified Output

{
  "version": "1.0.2",
  "scenarios": [
    {
      "id": "default-scenario",
      "name": "Default Scenario",
      "active": true,
      "mockedRequests": [
        {
          "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": {
              "userId": 1,
              "id": 2,
              "title": "qui est esse",
              "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"
            }
          }
        }
      ]
    }
  ]
}
Suggest Edits

exportMocks

 

exportMocks(scenarioId, mockIds, prettify)

Exports selected mocks within a scenario.
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('default-scenario', ['a342-asd324']); // Default Output
Mimic.exportMocks('default-scenario', ['a342-asd324'], true); // Prettified Output

Output

{"version":"1.0.2","scenarios":[{"id":"default-scenario","name":"Default Scenario","active":true,"mockedRequests":[{"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":{"userId":1,"id":2,"title":"qui est esse","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"}}}]}]}

Prettified Output

{
  "version": "1.0.2",
  "scenarios": [
    {
      "id": "default-scenario",
      "name": "Default Scenario",
      "active": true,
      "mockedRequests": [
        {
          "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": {
              "userId": 1,
              "id": 2,
              "title": "qui est esse",
              "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"
            }
          }
        }
      ]
    }
  ]
}
Suggest Edits

exportScenario

 

exportScenario(scenarioId, prettify)

Exports selected mocks within a scenario.
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.exportScenario('default-scenario'); // Default Output
Mimic.exportScenario('default-scenario', true); // Prettified Output

Output

{"version":"1.0.2","scenarios":[{"id":"default-scenario","name":"Default Scenario","active":true,"mockedRequests":[{"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":{"userId":1,"id":2,"title":"qui est esse","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"}}}]}]}

Prettified Output

{
  "version": "1.0.2",
  "scenarios": [
    {
      "id": "default-scenario",
      "name": "Default Scenario",
      "active": true,
      "mockedRequests": [
        {
          "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": {
              "userId": 1,
              "id": 2,
              "title": "qui est esse",
              "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"
            }
          }
        }
      ]
    }
  ]
}
Suggest Edits

exportScenarios

 

exportScenarios(scenarioIds, prettify)

Exports selected mocks within a scenario.
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.exportScenarios(['default-scenario']); // Default Output
Mimic.exportScenarios(['default-scenario'], true); // Prettified Output

Output

{"version":"1.0.2","scenarios":[{"id":"default-scenario","name":"Default Scenario","active":true,"mockedRequests":[{"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":{"userId":1,"id":2,"title":"qui est esse","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"}}}]}]}

Prettified Output

{
  "version": "1.0.2",
  "scenarios": [
    {
      "id": "default-scenario",
      "name": "Default Scenario",
      "active": true,
      "mockedRequests": [
        {
          "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": {
              "userId": 1,
              "id": 2,
              "title": "qui est esse",
              "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"
            }
          }
        }
      ]
    }
  ]
}

record(scenarioId)

Start automatic mocking of all outgoing requests, recorded requests will be assigned to the specified scenarioId.

See stopRecording for stopping automatic recording.

import Mimic from 'mimic';

Mimic.record('default-scenario');
Suggest Edits

stopRecording

 

stopRecording()

Stop automatic mocking of outgoing requests. See record

import Mimic from 'mimic';

Mimic.stopRecording();
 

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

 

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