Skip to main content

Advanced Mock

View details for advanced mock here.

Mock Priority

Mock rule priority: Expectations in Advanced Mock > Custom Mock Scripts.

The custom Mock script is not called if the Advanced Mock expectation is met.

Mock Expectation

Configuration:

  1. Expectation: Depending on the request parameters, different data is returned. For example, you can create two expectations:

    • Returns the sales status of available if the request parameter id is 1.
    • Returns the sales status of sold if the request parameter id is 2.
  2. The expectation condition supports multiple parameters. The expectation will only be matched when all parameters are matched concurrently.

  3. Expectations condition supports comparison relationships between parameter name and parameter value, including equal to, less than, greater than, exist, belong to, etc.

  4. The actual request body must match the API definition if the body is chosen as the parameter location for expectation criteria. If the body request type is form-data in API definition, the parameter needs to be placed in form-data when mocking.

  5. Expectation Rule: Apidog supports JSON Path matching for JSON body types.

    • The first level of properties can be matched by filling in the property name, or by using JSON Path.
    • Deep level of properties can only be matched by using JSON Path.
  6. Return data: Apidog supports Faker.js and Nunjucks for data returned from the API request. Apidog supports returning dynamic data based on certain rules.

  1. Apidog supports custom return Header, return HTTP status code, delayed response.

Mock Custom Scripts

Apidog supports using custom scripting to get the parameters from user requests and modify the return content.

You can use custom scripts to get parameters for user requests and modify the return message.

tip

Custom scripts can only be used in advanced mock, not in preprocessor and postprocessor scripts.

How to Use Custom Scripts

  1. Turn on Advanced Mock
  2. Use Javascript to modify returned JSON.

Example

Set paged data

// Get mock data from Smart Mock
var responseJson = $$.mockResponse.json();

// Modify the paged data from responseJson
// Set page as the page in request parameter
responseJson.page = parseInt($$.mockRequest.getParam("page"));
// Set total as 120
responseJson.total = 120;

// Write the modified json into $$.mockResponse
$$.mockResponse.setBody(responseJson);

Request: $$.mockRequest

  • $$.mockRequest.headers: Mock request headers.
  • $$.mockRequest.cookies: Mock request cookies.
  • $$.mockRequest.getParam(key: string): Get mock request parameters, including path, body, and query parameters.

Response: $$.mockResponse

  • $$.mockResponse.headers: Mock response header
  • $$.mockResponse.code: HTTP code generated by the system
  • $$.mockResponse.json(): JSON response generated by the system
  • $$.mockResponse.setBody(body: any): Set body for mock response (Apidog supports JSON or string).
  • $$.mockResponse.setCode(code: number): set HTTP code return by the mock response.
  • $$.mockResponse.setDelay(duration: number): set delay (in ms) for mock response.