Universal REST Simulator
Introduction
Universal REST Simulator is a universal simulator for REST APIs. This simulator can be configured by adding some configuration files into the config directory. Universal REST Simulator can read all file extensions.
The configuration is similar to the this file. If the configuration value is more than one line, then the value in the line other than the last must end with \ (backslash).
The tutorial can be read at https://github.com/kamshory/Universal-REST-Simulator/blob/main/tutorial.md
Example:
PARSING_RULE=\
$INPUT.PRODUCT=$REQUEST.product_code\
$INPUT.ACCOUNT=$REQUEST.customer_no\
$INPUT.REF_NUMBER=$REQUEST.refno\
$INPUT.ACCEPT_LANGUAGE=$OUTPUT.HEADER.ACCEPT_LANGUAGE\
$INPUT.AMOUNT=$REQUEST.amount
The PARSING_RULE property above consists of 4 lines. If the \ character is omitted, then PARSING_RULE has no value. If the \ in line 2 is omitted it becomes as follows:
PARSING_RULE=\
$INPUT.PRODUCT=$REQUEST.product_code\
$INPUT.ACCOUNT=$REQUEST.customer_no
$INPUT.REF_NUMBER=$REQUEST.refno\
$INPUT.AMOUNT=$REQUEST.amount
then PARSING_RULE only consists of 2 lines, namely:
$INPUT.PRODUCT=$REQUEST.product_code\
$INPUT.ACCOUNT=$REQUEST.customer_no
Because after the line loses \ at the end, the simulator will not continue reading the data. This configuration method applies to all properties.
Path
Property: PATH
Universal REST Simulator will select the configuration according to the path which is accessed by the same method as the request method. For example: user creates 7 configuration files, Universal REST Simulator will select one file with appropriate path. After getting a file with the appropriate path and method, the simulator will stop looking for other files.
Example configuration file structure:
[root]
[config]
config1.txt
config2.txt
config3.txt
root is the document root of the simulator
config is a directory under document root
The files config1.txt, config2.txt, config3.txt are in the config directory.
If the PATH in the config1.txt file is /biller/config1, then the file will be selected only if the path in the request URL is /biller/config1. If the PATH in the config2.txt file is /bank/config2, then the file will be selected only if the path in the request URL is /bank/config2. If none of the files with path match, then the simulator will give no response.
Method
Property: METHOD
User can choose GET, POST or PUT method. Universal REST Simulator will read the request according to the method used in the configuration and will ignore other requests.
Content Type
Content Type Request
Property: REQUEST_TYPE
Universal REST Simulator supports 3 kinds of content types which are as follows:
- application/x-www-form-urlencoded
- application/json
- application/xml
- application/soap+xml
This content type will affect how to read requests on the simulator.
Content Type Response
Property: RESPONSE_TYPE
Users are free to use any content type for the response because basically the response simulator is pure text.
Input Configuration
Property: PARSING_RULE
$INPUT is an object that can be thought of as a global variable and has properties. $INPUT is always capitalized. The property of $INPUT can be written in both uppercase and lowercase letters and will be case sensitive.
Input comes from 2 sources namely $REQUEST (request body in POST and PUT and query string in GET) and $OUTPUT.HEADER (request header). Both $REQUEST and $OUTPUT.HEADER must be capitalized. The property name of $REQUEST is case sensitive while the property name of $OUTPUT.HEADER is capitalized and - is changed to _. This is because the header properties may have changed and cannot be predicted with certainty.
$REQUEST can come from:
- Query on
GET
- Request body on
POST and PUT
- Wildcard URL
- Basic Authorization
To retrieve $REQUEST from a URL wildcard, simply use {[IDENTIFIER]} in the URL. {[IDENTIFIER]} is case sensitive.
Example:
PATH=/payment/{[PRODUCT_CODE]}/{[CUSTOMER_ID]}/{[REFERENCE_NUMBER]}
If the client requests either GET, POST or PUT with URL /payment/123/456/7890, then $REQUEST.PRODUCT_CODE will be 123, $REQUEST.CUSTOMER_ID will be value 456, $REQUEST.REFERENCE_NUMBER will be 7890. URL wildcards can still be concatenated with query strings. Both the input from the URL wildcard and the query string can be parsed in a single request.
Basic authorization contains a username and password to access a data source. The username and password information is encoded with base 64. The simulator extracts the information and then saves it to the $AUTHORIZATION_BASIC object.
To retrieve the username from basic authorization, use $AUTHORIZATION_BASIC.USERNAME. To retrieve the password from basic authorization, use $AUTHORIZATION_BASIC.PASSWORD. $AUTHORIZATION_BASIC.USERNAME and $AUTHORIZATION_BASIC.PASSWORD must be capitalized.
The simulator reads the input depending on the content type request. For content type application/x-www-form-urlencoded, the simulator directly fetches the value of the appropriate parameter. For application/json and application/xml content types, the simulator will fetch data incrementally. Thus, users are free to provide JSON and XML requests with a nested structure.
The input matrix and methods of Universal REST Simulator are as follows:
| Method |
Content Tpe |
Data Source |
Alternative Object |
GET |
applicatiom/x-www-form-urlencoded |
Header, URL, Basic Authorization, GET |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET |
POST |
applicatiom/x-www-form-urlencoded |
Header, URL, Body, Basic Authorization, GET, POST |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET, $POST |
POST |
applicatiom/json |
Header, URL, Body, Basic Authorization, GET |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET |
POST |
applicatiom/xml |
Header, URL, Body, Basic Authorization, GET |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET |
POST |
applicatiom/soap+xml |
Header, URL, Body, Basic Authorization, GET |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET |
PUT |
applicatiom/x-www-form-urlencoded |
Header, URL, Body, Basic Authorization, GET, PUT |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET, $PUT |
PUT |
applicatiom/json |
Header, URL, Body, Basic Authorization, GET |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET |
PUT |
applicatiom/xml |
Header, URL, Body, Basic Authorization, GET |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET |
PUT |
applicatiom/soap+xml |
Header, URL, Body, Basic Authorization, GET |
$HEADER, $URL, $REQUEST,
$AUTHORIZATION_BASIC,
$GET |
Input from Objects and Arrays
Users may use a combination of array and object as the payload of the request either GET, POST, PUT, or REQUEST. To retrieve values from input which are all object, you can use the dot operator (.) while to retrieve values from input which is a combination of object and array, you can use the square bracket operator [].
Payload Example
{
"items":[
{
"name":"Kopi",
"amount":15000
},
{
"name":"Roti",
"amount":80000
}
],
"customer": {
"name": "Anonim",
"phone": "081111111111111"
}
}
Example Configuration
PATH=/universal-rest-simulator/array
METHOD=POST
REQUEST_TYPE=application/json
RESPONSE_TYPE=text/plain
PARSING_RULE=\
$INPUT.AMOUNT0=$REQUEST[items][0][amount]\
$INPUT.AMOUNT1=$REQUEST[items][1][amount]\
$INPUT.NAME0=$REQUEST[items][0][name]\
$INPUT.NAME1=$REQUEST[items][1][name]
TRANSACTION_RULE=\
{[IF]} (true)\
{[THEN]}\
$OUTPUT.DELAY=0\
$OUTPUT.BODY=\
NAMA ITEM : $INPUT.NAME0\
HARGA ITEM : $INPUT.AMOUNT0\
NAMA ITEM : $INPUT.NAME1\
HARGA ITEM : $INPUT.AMOUNT1\
$INPUT.PARAMS\
{[ENDIF]}
The [] operator can be used to retrieve values from object and array. It is not allowed to combine the . and [] operators in retrieving a value. Thus, writing $INPUT.AMOUNT0=$REQUEST[items][0].amount is not allowed. However, it is permissible to use their combination on different inputs.
Example of Operator Combinations
PATH=/universal-rest-simulator/array
METHOD=POST
REQUEST_TYPE=application/json
RESPONSE_TYPE=text/plain
PARSING_RULE=\
$INPUT.CUSTOMER_NAME=$REQUEST.customer.name\
$INPUT.AMOUNT0=$REQUEST[items][0][amount]\
$INPUT.AMOUNT1=$REQUEST[items][1][amount]\
$INPUT.NAME0=$REQUEST[items][0][name]\
$INPUT.NAME1=$REQUEST[items][1][name]
TRANSACTION_RULE=\
{[IF]} (true)\
{[THEN]}\
$OUTPUT.DELAY=0\
$OUTPUT.BODY=\
NAMA PELANGGAN : $INPUT.CUSTOMER_NAME\
NAMA ITEM : $INPUT.NAME0\
HARGA ITEM : $INPUT.AMOUNT0\
NAMA ITEM : $INPUT.NAME1\
HARGA ITEM : $INPUT.AMOUNT1\
$INPUT.PARAMS\
{[ENDIF]}
$INPUT.CUSTOMER_NAME=$REQUEST.customer.name can also be written as $INPUT.CUSTOMER_NAME=$REQUEST[customer][name] without spaces before [ and after ].
Value of UUID
Universal REST Simulator allows the use of UUIDs. To retrieve the UUID from the system, use $SYSTEM.UUID.
Example:
$INPUT.UUID=$SYSTEM.UUID\
$INPUT.RANDOM_ID=$SYSTEM.UUID\
$INPUT.UNIQ_ID=$SYSTEM.UUID
From the example above, $INPUT.UUID, $INPUT.RANDOM_ID, and $INPUT.UNIQ_ID will have different values.
Examples of Encoded URL Input Configuration
PATH=/biller/config1
METHOD=POST
REQUEST_TYPE=application/x-www-form-urlencoded
RESPONSE_TYPE=application/json
PARSING_RULE=\
$INPUT.PRODUCT=$REQUEST.product_code\
$INPUT.ACCOUNT=$REQUEST.customer_no\
$INPUT.REF_NUMBER=$REQUEST.refno
In the above configuration, $INPUT.PRODUCT will retrieve the value from $REQUEST.product_code. Thus, when a user makes a request with the URL /biller/config1?product_code=10000&customer_no=081266612126&refno=5473248234, then $INPUT.PRODUCT will be worth 10000, and $INPUT.ACCOUNT will be 081266612126 etc.
Examples of JSON Input Configuration
PATH=/bank/config2
METHOD=POST
REQUEST_TYPE=application/json
RESPONSE_TYPE=application/json
PARSING_RULE=\
$INPUT.COMMAND=$REQUEST.command\
$INPUT.PRODUCT=$REQUEST.data.destination_bank_code\
$INPUT.ACCOUNT=$REQUEST.data.beneficiary_account_number\
$INPUT.REF_NUMBER=$REQUEST.data.customer_reference_number
JSON is usually used in POST or PUT methods. In the above configuration, $INPUT.PRODUCT will take the value from $REQUEST.data.destination_bank_code which is ROOT.data.destination_bank_code. where ROOT is a JSON object. Thus, when a user makes a request with
POST /bank/config2 HTTP/1.1
Host: 127.0.0.1
Content-type: application/json
Content-length: 166
{
"command":"inquiry",
"data":{
"destination_bank_code":"002",
"beneficiary_account_number":"1234567890",
"customer_reference_number":"9876544322"
}
}
then $INPUT.PRODUCT will be worth 002, likewise $INPUT.ACCOUNT will be 1234567890 and so on.
Contoh Konfigurasi Input XML
PATH=/bank/config3
METHOD=POST
REQUEST_TYPE=application/xml
RESPONSE_TYPE=application/xml
PARSING_RULE=\
$INPUT.COMMAND=$REQUEST.command\
$INPUT.PRODUCT=$REQUEST.product_code\
$INPUT.ACCOUNT=$REQUEST.customer_no\
$INPUT.AMOUNT=$REQUEST.amount\
$INPUT.REF_NUMBER=$REQUEST.refno
XML is usually used in POST or PUT methods. In the above configuration, $INPUT.PRODUCT will take the value from $REQUEST.product_code which is ROOT.product_code. where ROOT is the first level tag of the XML. Thus, when a user makes a request with
POST /bank/config3 HTTP/1.1
Host: 127.0.0.1
Content-type: application/xml
Content-length: 196
<?xml version="1.0" encoding="UTF-8"?>
<data>
<command>inquiry</command>
<product_code>10001</product_code>
<customer_no>081266612127</customer_no>
<amount>5000000</amount>
<refno>123456443</refno>
</data>
then $INPUT.PRODUCT will be worth 10001, likewise $INPUT.ACCOUNT will be 081266612127