Mock Api

Overview

A Mock Server can organize thousands of or hundreds of APIs as you need. Each Mock API has two parts:

  • Request:
    The Request part define the methods and path that we will use to match a incoming http request. For any matched request, we will send a response through our Response part definition.

  • Response:
    Response define the http Response we will send back to the incoming request. it could be in two modes:

    • Simple Response:
      In this mode,we staticly define the status code, res headers and res body that we will use to send back to the request client.

    • Advanced Request Handling:
      In this mode, we hanlde the Request by JavaScript (nodejs), Python or Golang Programming. We defined a Callback function HandleRequest , we just need to implement the callback and build up the Response by programming.

Add Mock Api

the ‘hello’ mock server case is created, it time to add a mock api before we launch the mock server.

  1. Switch to “Mock Server” Tab and select the mock server case, then click ”+ New” button to add a mock api.

RestBird Login

  1. Click the api to see the detail, and provide the response in two ways(simple response or advanced response).

RestBird Login

Request Matching

A Mock API’s Request definition is used to match a incoming request.

Architecture

As we see, there is only two key part:

  • Method:
    Define the matching Http Method. Even the request path is the same, but Mthod is not matched, we still consider it as a unmatched request.

  • Url:
    Define the URL Path that will used to match the request Url. For examples: /hello , /hello/world, /hello/:group/:name/:id

    if a request Url, has multile mathes, we will choose the longest one.

    we also support varibles in url path definition by :varible-name. In different programming languages, we have different way to get these varibles.

Simple Response

Here is the virsual view of Simple Response Builder:

Architecture

just add what you want here and it will send back to the matched request client.

Advanced Request Handling

For advanced Request handling, we use programing to build up the Response, which is much more flexible. Three programming languages are supported, you can choose any you like when you create a mocker server case.

JavaScript

The JavaScript HandleRequest callback function is defined as below. There is a bounch of examples on the right side you can referenced to implement your own code.

If this is still not enough for you, you are suggested to reference nodejs express document for more advanced apis. Restbird Mocker Server’s JavaScript callback is implemented based on nodejs express lib.

Architecture

Python

The Python HandleRequest callback function is defined as below. There is also a bounch of examples on the right side you can referenced to implement your own code.

If this is still not enough for you, you are suggested to reference python http.server lib document for more advanced apis. Restbird Mocker Server’s Python callback is implemented based on Python3 http.server lib.

Architecture

Golang

The Golang HandleRequest callback function is defined as below. There is also a bounch of examples on the right side you can referenced to implement your own code.

If this is still not enough for you, you are suggested to reference Golang http package document for more advanced apis. Restbird Mocker Server’s Golang callback is implemented based on Golang http package.

Architecture