Friday, October 16, 2020

Http Api - Mock / Redirect tool

 Http API - Mock Redirect is a light weight developer tool, I recently developed.  

Purpose of the Tool:

  1. When we may want to route urls to different hosts (say Dev, Test, Stage env). For ex a url of app may be routed to a host, another url of same app can be routed to different host. It can be configured  at http method as well
  2. When we may want to mock the response rather than calling the actual service/host
  3. This tools also provides view of all request/response processed

How does it work:

The tools starts a http server on configured local port, and will listen to any incoming request. Based on the url mapping configuration (will see later in the section) it will decide whether the request has to routed to the external endpoint or mock response to be sent. 


How to make it work:

Installation:

1.  Java/Jre 8 or above is installed

2. Download the api-mock-redirect-1.0.0.jar from github , and save it in a local folder. It is executable jar - double click to open the tool.

2. Email support@lite-bean.com


Url Mapping - Configuration

1. Provide a config name (config can be later loaded, so give some meaningful name)

2. Provide the local port, where tool should listen to any incoming http request. (port > 5000 -  is preferred. As initial set of ports are reserved for other purpose) 

3. Configure the url for which tool needs to redirect or mock. The url should start from context, and should be valid regex string. For ex: 

       a. users/.* - will handle all request starting with users/

       b. user/[0-9]* - will handle all request for users/<userid>

4. Configure http method, default is * - to support all methods, you may also select any method

5. Configure the option - i.e., Redirect/Mock  (by default it is redirect).

6.  Click edit/pencil icon, which will display fields to configure redirect/mock details. 

In the below example, i have configured an url for "api/v1/employee/<empl_id_numeric>"








Redirect - Configuration

1. Select Protool - Both http & https are supported

2. Enter the host name

3. Enter the port - at which the host is serving the request. (for https it may be 443, for http it may be 80 or 8080)

In the below example, I have configured the redirect to "https://dummy.restapiexample.com/"


Url Mapping 2 - Configuration

On click of "+" will create another row in url mapping, where next url can be configured.

Here I have configured my next url for "api/v1/employees"






Lets add another url to demonstrate mock feature, select Mock option 


Mock- Configuration

1. Configure Response 

2. Type in response headers (click + to add more headers)

3. Enter response body




Save & Monitor Urls

After all above configuration, click Save & Start Service button, which will start the service on configured port and listen to any incoming request. Incoming request will be logged in the tool.

Here the service is started at http://localhost:5001





Demo URL1 - Redirect


Url - http://localhost:5001/api/v1/employees 
Expected output - it should redirect to  "https://dummy.restapiexample.com/api/v1/employees" and fetch the result.

Here we have got the response as expected



Demo URL 2 - Redirect - power of regex


Url - http://localhost:5001/api/v1/employee/1
Expected output - it should redirect to  "https://dummy.restapiexample.com/api/v1/employee/1" and fetch the result.

Here we have got the response as expected


Demo URL 3 - Mock


Url - Mock url is configured as /api/* - so we will have v2, so that it wont qualify for first two urls - "http://localhost:5001/api/v2/employees"

Expected output - it should return mock output.

Here we have got the response as expected

Logs:

As shown below, all request / response will be logged in the tool - on click on any row will display req/response details (including all headers)





Other Features:

1. Service Stop &Close: click "Stop &Close" button on the listening page, to stop the service, and close the tab
2.  Load Saved Configuration:  Now the config is saved, it can be loaded anytime again - all the existing configuration can be reused.


Notes:

1. This is not limited to REST. SOAP over http can also be handled 

2. This tool is not limited to GET. All http options are supported and can be configured

3. Some understanding of REGEX is required to configure url

4. Url Mapping and request headers requires the cell is keyed in out 

5. Any click on edit, remove, add will automatically save the current state of config

6. If no matching url configured, tool will return "404 - resource not found"

7. For redirect - Few request headers (Content-length, Accept-encoding & Host)  are excluded. This can be overriden by adding a file "lb/repo/ignoreRedirectHeaders". Ensure all headers needs to be excluded need to be added here.



Hope this tool will be useful one. Do let me know your feedback/comments.